C# UserControl Inheritance - c#

Working with VS.NET 2008, output type Class Library, Target Framework .NET 2.0
I've come up with a simplified scenario to ask this question.
I have a Button user control, its a simple panel with a single big button on it.
I want to create a RedButton control that extends Button, and similarly, a GreenButton.e.g. Class RedButton : Button
Ideally, when I open up RedButton's designer I will see the button that I created in Button and be able to modify it, for example make it Red, or change font, etc.
I've tried to do this once, but when I open up the RedButton's designer I just get a bunch of errors.
In this case, doing all this work programatically isn't an option for us, as in the real case this would be a pain.
Could someone shed some light on this?
Thanks Very Much.

Truthfully, your example should work just fine. Just ensure that you provide a default constructor for your derived class. Also ensure that you do not use generic controls as the designer will have no clue how to create an instance of it.

Is your assembly setup to be delay signed? Look for the delay signing attribute as well as the checkbox in project properties. I've seen delay signing cause this sort of problem with VS2005 perhaps its still a problem in VS2008.

I had to deal with this problem for years at an old company. I researched it a little back then. I don't think that there is a solution for this.
I don't know how much you want to extend the base class in your real example, but the changes you mentioned in your example would be trivial. Just something like
btnTheButton.BackGround=Color.Red;
In reality, probably whatever changes you need to make to button could be done in a few minutes. It is unfortunate that this will be a few minutes everytime you need to inherit a new control, but I think it's the only option

If the control you inherit from is from a DLL and not just another class in the solution, then your designer will render the inherited control properly. My guess is that the VS Design View needs the DLL to draw the control. There may be other ways around this as well.

Since VS.NET 2008 the root designer is able to present "bunch of errors" as you mentioned. In general the described scenario should “just work”.
What kind of errors are you facing?

Follow this example if you aren't already:
public class RedButton : Button
then in the XAML instead of
<UserControl></UserControl>
Start/end your XAML file with
<Button></Button>
Note that it is okay to inherit from something in another namespace even though you didn't define the namespace yet. Example:
<radDock:RadPane ...
xmlns:radDock="clr-namespace:Telerik.Windows.Controls;assembly=Telerik.Windows.Controls.Docking"
...
Title="{Binding Path=StudyTitle}"...

Related

How to make custom form control in c#

I was trying to do something in visual studio the other day when I realized, if I could just make a form control to do it for me it would be allot easier, except I have no idea how to do that, I want the form control to have grids, each square having its own color property, if anyone knows how to make form controls, or even better knows how to make something like what I just described, I would be very happy :D
This MSDN article is a basic step by step outline of how you can write a customer control.
Unfortunatly MS has not figured out how to do avoid link rot -- so you may need to search creating custom winform controls to find this if you come in the future.
You are usually best servered by subclassing an existing control and customizing it.
You might also find some of the freely available winform control projects a gold-mine of useful info if you get serious about this.
However, it sounds likely what you should consider doing is creating a "User Control", this is usually simpler for a composite of few existing controls. This
article on the types of controls for winforms may be a useful overview for you.
Beyond that you really should use S/O if you are trying to resolve a specific problem you are having when you are coding. Google is a more appropriate tool for finding tutorials, etc.
1) Inside your project: Solution Explorer --> Right Click the .csproj --> Add UserControl
2) Drag and drop gridBox or any control you want into your custom control.
3) Check the ToolBox, your custom control should be located at the very first selection

Breaking code into manageable chunks

I have a Windows Forms application written in C# and one of the forms has an enourmous amount of code with it. I have made extensive use of classes to keep the forms code to a minimum, but because the form has a number of tab pages and hundreds of controls and datagrids, etc., the code on the form itself is still extensive.
Is there any way to break this code into more manageable and smaller items, perhaps one item in the solution for each tab page, whilst keeping all the code in the same scope?
If your looking for better readability and maintainance without moving the code around too much, you could use:
#region Tab 1
#region Variables
#endregion
#region Properties
#endregion
#region Methods
#endregion
#endregion
This would allow you to minimise parts of the code you are not interested in while making changes to a certain tab. It's not perfect, but it may help.
If you are looking for the restructuring the code and its framework then you must be aware of the SOLID principles. A good article for it is S.O.L.I.D. Software Development, One Step at a Time.
You could use Partial Classes to split code into several cs files, but I really don't see to much benefit in that, only solution is to do full refactoring and remove code that has nothing to do with UI into separate classes.
You basically need to refactor. This is a common problem with classic Windows Forms applications, so you have to be disciplined and decide how to tidy up. It's not going to be an instantaneous fix.
Lookup MVC/MVP, even MVVM and learn how others break their code up. From there you can introduce a tiered architecture that suits you.
However, you aren't alone. The refactoring tools in Visual Studio or even better in ReSharper can automate a lot of the copy paste cycle, eliminating errors and automatically keeping variable names, etc. in sync.
Code which belongs to the presentation layer of a user control you can put into a class which extends the control.
Extract the code out of each tab and create a user control to contain the code for each tab. (you could even inherit from TabPage and add these to the form on init)
Then the user controls can be added in to each tab and should reduce code significantly.
The grids etc can also be turned in to user controls exposing the minimum number of methods and properties required for the other controls to access.
If you are defining form controls over and over, just create a new instance of them on your other tab.
Create getter and setter to access this.
But really, there is no problem with having lots of code for form controls, its just the way it is I think. I thought Visual Studio was supposed to generate all this for you using Windows Forms?
I personally have trouble with regions. It sometimes throws the editor and you need to close and re-open the file to reset it (I am working with Boo in SharpDevelop, and it may be specific to that), so for large forms with tabs I tend to use partial classes.
A neat trick you can do in Visual Studio/SharpDevelop file explorer is to drag the file for the partial class onto the main file for the class, so they all sit nested under MainForm (next to the .designer and .resx files) which just keeps things neater.

Is it possible to generate code properly through a toolbox custom control?

Ok, so long story short, I was working on a .NET WEBFORMS project which was capable of creating ASPX pages through an ASPX form, which was basically a drag and drop controls page which, we could say, seemed to be a basic but functionality acceptable page/wizard/aspx-creator.
Well, now my higher-ups decided that approach is not a worthy solution and, I don't know why, they got into their heads that this, instead of this ASPX creator form, could be implemented through custom controls added to the toolbox.
So, my doubt is... is that even possible? Or better, is that a reasonable solution? I mean, the first cons I've already found is the fact It seems that it's impossible to yield CodeBehind code by the drag and drop method.
Thanks in advance!
Custom Controls
This MDSN walkthrough teaches you aboutmaking custom web controlls for asp. these would be able to appear in your tool box and could be dragged into its respective place, is this waht you are looking for?
EDIT:
Re-reading it looks like you want to be able to drag in bits of code and have certain regions prompted for edits, this CAN be done, using snippets. if you type propg or propfull and tab twice it constructs that code and tells you what bits to edit right? you can make your own snippets!
Creating custom snippets
failful msdn tutorials to the rescue once more!

Available class events C#

in c# how can I get the available events of a class in order to begin programming code inside of it. I know that in form or usercontrol you can select the control and click the events button and click on the selected event to begin coding, but I mean a derived class which I want to code its methods or events.
Thanks!
The simplest way is to look in the documentation, to be honest.
If you're within the designer, your approach of using the properties window will work for other controls (buttons, textboxes etc) as well... and if you're in the IDE, you can type this. from any instance context (e.g. the constructor) and get a list of members up, including the events.
I would still suggest reading the documentation though - MSDN allows you to look at all members, or just the events (or methods, or whatever) at any one time. It's not terribly hard to peruse the list that way - and you can then check the details of the event so you can make sure you're using it properly, rather than just by guessing based on the name.
EDIT: As Aren mentions, there's also the Object Browser. Not my personal preference, but it's an option.
The events are listed in the IntelliSense popup window you'll get when you type the class reference variable name followed by a dot. They have a lightning bolt icon.
Many more organized ways to know what a class can do. You could read its documentation. Or browse its source code. Or use Reflector if there isn't any.
in Visual Studio 2010, they have got this new Help Library Manager where you can check for MSDN updates and download any updates or go to online directly. (you can also install Vendor API docs quite easily as well)

Change Control Types (but not names) on Form with Minimal Impact

I need to change a lot of textboxes to NumericUpDowns and other similar changes on some forms in my multiform c# app. I'd like to keep the name of each control the same as I make the change. There's also code for events associated with some of the controls that I'd like to change as little as possible.
How do I do this without screwing things up badly in Visual Studio? It's version 2008. I'm worried I'll surely run into the dreaded designer errors.
Make the changes in the designer.cs file, and keep your fingers crossed :)
This might make it slightly less painful:
Create a new class, derived from TextBox (let's call it MyClass). Change all of the occurrences of TextBox that you want to change, via the search and replace method. Then you can change MyClass and derive it from NumericUpDown, and see if anything breaks.
Your only alternative is to create a class that is derived directly from either TextBox or NumericUpdown, and then implement methods compatible with the other one. So in effect you would have a control that is compatible with both NumericUpDown and TextBox.

Categories