Subclassing controls

I want to modify the bahavior of a standard control by changing some of the methods. I know I can do this by subclassing the control. However, it seems like I need to create a new control as a seperate project and reference the created dll in my main project. This seems like a lot of work for a simple task; what I'd really like to do is have everything in the same project (although not the same file).

What I'm used to in Visual C++ is to use the base control as usual (eg. drag an edit control onto a dialog), and then use DDX_Control in my DoDataExchange to subclass the control.

Is there a similar way in C#


Answer this question

Subclassing controls

  • Bailey58642

    So the designer parses the .cs file directly I was cautious about editing the *.designer.cs file, thinking the designer used some kind of resource file, and rebuilt the .cs file when the changes to that file were saved.

    EDIT: I've just tried it, and after rebuilding the project, everything seems to work fine.

    Exactly what I wanted, thanks :)

  • PravinD

    Hi,
    to extend an existent control you don't have to create a new control in a separate project. On the other hand whenever you use a control (or any other thing that's outside you're project), you'll have to reference the dll. If you notice the code generated by the designer in c#, when you create a form, it just extends the Form object:

    public class Form1 : Form
    {
    // your code
    }


    Next, you can override some methods to adapt to your behaviour.


  • Jackey Cheung

    (Side note: this different isn't "C++ vs. C#" but "MFC vs. .Net")

    ANyway, there's no need to create a separate assembly. Don't even need a separate source file. Just start typing:

    public class MyEditBox : TextBox
    {

    }

    I'd drag a TextBox onto the Form in design mode, then edit the form.cs file to replace TextBox with MyEditBox.



  • roeschda

    Thanks, is there a FAQ anywhere for people learning c# from a c++ background

    Also, is it possible to subclass as mentioned in my original post - ie. place a System.Windows.Forms.TextBox on the form, and then change it to your derived class in software (using some C# equivalent to VC++'s DDX_Control macro maybe) so your overridden methods are called, and you don't have the create the control manually.

    I suppose I could place the normal control in the designer, copy the created code from the .cs, delete the control from the designer, change the type to my own and place the modified code in my own initialisation, but that seems like a lot of work.

  • Mark Phelps

    Hi,
    Microsoft was kind enough to make that "FAQ" :) You can find it here: http://msdn.microsoft.com/msdnmag/issues/01/07/ctocsharp/

    As for your other question, yes, You can change the System.Windows.Forms.TextBox to your control manually (I've done it several times) but be careful when "playing" with the automatic generated code.


  • Subclassing controls