views:

1326

answers:

4

I'm used to working in visual C++. In learning C#.NET I tend to try to find equivalent methods to how things are done in VC++.

When I have a button in C++, to get full control over it I create a class derived from CButton (the equivalent of System.Windows.Forms.Button in .NET) and then set this class as the type for my button in order to get all events and overrides routed into my code.

In C# I haven't really found an equivalent way to do this. There seems to be no way to set which class a control should be created from, and I haven't even bothered trying to replace the auto-generated code where the object is created since it is likely to be discarded the next minute.

What's the correct way of using my own class to front a user control?

EDIT: I should clarify that I'm interested in adding my custom class to the existing project. Most of the responses so far either provide non working ways of adding it to the existing project, or working ways of creating an external library with the component (DLL), but that's a bit overkill for my particular case.

A: 

If you derive from System.Windows.Forms.Button, for instance, your new control will be available in the toolbox. A new tab should appear at the top and will be named [Project Name] Components. You can just drag and drop it on a form.

As far as changing the type of a control already on a form, I've had success with changing the type in the designer generated code .cs file.

Here's a great article on creating custom .net controls:

The article discusses evey possible scenario, including using inheritance, and provides lots of sample code.

EDIT:

Add this to your project:

public class DerivedTextBox: System.Windows.Forms.TextBox
{
}

And then build the project. DerivedTextBox should then be available in the ToolBox.

Joepro
It actually didn't happen for me. Even though I derived from a control, nothing appeared in the toolbox. The article you provided seems to only cover creation of custom controls, i.e a new type of control. This is not what I want, but rather to just inherit from an existing control to get events etc. routed into my code.
sharkin
Re-read the article and look for "Format Mask Control". This shows you how to use inheritance. They are extending a regular .net TextBox to add extra functionality.
Joepro
Also, before your derived classes are available in the toolbox, you'll have to build your project.
Joepro
It's not appearing in the toolbox no matter what. In the "Format Mask Control" section, this text appears: "Add a reference to the Edit Mask User Control DLL named: FormatMask.dll".. - something you forgot to mention?
sharkin
That will permanently add it to the toolbox, once your derived class has been built into a dll. This way, every other winforms project will be able to use your new control. The way I explained was for your current solution only.
Joepro
Well I'm sorry it doesn't seem to work. I even tried with a new fresh project to rule out potential issues with my project settings.
sharkin
A: 

You may either override the functions you need with your own code, or simply create a new user control which derives from the System.Windows.Forms.Button.

It depends on if you need to create a new control which will be used many times or if this is something you need to do for specific cases only.

Edit: Have a look at the MSDN documentation:

http://msdn.microsoft.com/en-us/library/5h0k2e6x(VS.71).aspx

Anax
Thanks for the link, but your own words are of no help what so ever.
sharkin
A: 
abstract class MyClass :  Windows.Form
{
....
}


public class WindowsFormClass : MyClass
{
....
}

Similary you can create any custom class and derive it from the Control Class (Button, Textbox, Grid etc.), override its methods to add your functionality, and then create a custom object out of it. It will available to you in the toolbox and you can start using it in your project.

Bhaskar
What are the steps of "create a custom object out of it"?
sharkin
See this --> http://ondotnet.com/pub/a/dotnet/2002/03/18/customcontrols.html. You can also refer to the site posted by other people.
Bhaskar
That link describes how to create custom type controls, in external libraries. This is not what I want if you read the question.
sharkin
+1  A: 

There isn't any trick to it. Create a new .cs source file or class using Visual Studio, remember to include the proper using System.Windows.Forms statement at the top of the file, then inherit as you normally would (note: in .NET, there are no classes with the naming convention CWhatever).

Jon Seigel