Sub-classing windows forms controls

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Richard Carpenter

    Sub-classing windows forms controls

    I have added a Windows Forms Control Library project to my solution and
    created a form control (let's call it libForm) in this new project which
    inherits from System.Windows. Forms.Form. I set the Text property of this
    form to the name of the application ("MyApp"). When the control library is
    built and a reference added to the main project, I am able to inherit from
    libForm in my main project, but the Text property on these new forms remains
    the default. I have two questions:

    1) Should I not expect the Text property value to be inherited from the
    libForm control class?
    2) With most Windows control types (textbox, label, etc.) they can be added
    to the toolbox. I assume that the conventional method for implementing a
    custom form class is just through the inherits statement. Is that correct?

    --
    Richard Carpenter

  • Peter Duniho

    #2
    Re: Sub-classing windows forms controls

    On Sat, 12 Apr 2008 16:59:44 -0700, Richard Carpenter
    <rumbledor@hotm ail.comwrote:
    [...]
    1) Should I not expect the Text property value to be inherited from the
    libForm control class?
    I think not. For the same reason that writing a new class that inherits
    Form still has the Text property's initial value explicitly overridden by
    the Designer, so too would a new class that inherits from your custom
    Form-derived class. That is, the Designer treats that property specially,
    and adds explicit code in the .Designer.cs file to initialize it according
    to the Designer's default.
    2) With most Windows control types (textbox, label, etc.) they can be
    added to the toolbox. I assume that the conventional method for
    implementing a custom form class is just through the inherits statement.
    Is that correct?
    I'm sorry, I don't really understand this question. I don't see the
    connection between the two different sentences, nor does C# have an
    "inherits" statement (do you simply mean the ":" used in the class
    declaration?).

    A custom control that shows up in the Toolbox is simply the custom class
    itself. You don't get new class inheriting that custom control when you
    use something from the Toolbox. You just get a new instance of that class
    itself (added to whatever container you drag it to).

    You can create a custom form class with the designer by using the "Add New
    Form..." menu item, or similar commands. Regardless of the specific route
    you take, you'll get a whole new class added to your project, already
    declared to inherit the Form class (e.g. "class Form1 : Form").

    The syntax for inheriting the Form class isn't just "the conventional
    method". It's the only way to do it. If you want to create a custom form
    class -- that is, a new class that inherits the existing Form class -- you
    have to use that syntax and inherit the Form class (either directly or via
    a class that itself inherits the Form class).

    Pete

    Comment

    • Richard Carpenter

      #3
      Re: Sub-classing windows forms controls

      On Apr 12, 8:20 pm, "Peter Duniho" <NpOeStPe...@nn owslpianmk.com>
      wrote:
      On Sat, 12 Apr 2008 16:59:44 -0700, Richard Carpenter  
      >
      <rumble...@hotm ail.comwrote:
      [...]
      1) Should I not expect the Text property value to be inherited from the  
      libForm control class?
      >
      I think not.  For the same reason that writing a new class that inherits 
      Form still has the Text property's initial value explicitly overridden by  
      the Designer, so too would a new class that inherits from your custom  
      Form-derived class.  That is, the Designer treats that property specially,  
      and adds explicit code in the .Designer.cs file to initialize it according 
      to the Designer's default.
      Interesting. I guess I'll need to set the properties in code, then.
      2) With most Windows control types (textbox, label, etc.) they can be  
      added to the toolbox. I assume that the conventional method for  
      implementing a custom form class is just through the inherits statement. 
      Is that correct?
      >
      I'm sorry, I don't really understand this question.  I don't see the  
      connection between the two different sentences, nor does C# have an  
      "inherits" statement (do you simply mean the ":" used in the class  
      declaration?).
      Yes, apologies, that's exactly what I meant.
      A custom control that shows up in the Toolbox is simply the custom class  
      itself.  You don't get new class inheriting that custom control when you 
      use something from the Toolbox.  You just get a new instance of that class  
      itself (added to whatever container you drag it to).
      >
      You can create a custom form class with the designer by using the "Add New 
      Form..." menu item, or similar commands.  Regardless of the specific route  
      you take, you'll get a whole new class added to your project, already  
      declared to inherit the Form class (e.g. "class Form1 : Form").
      Yeah, that's the way I'm used to doing it. I was just wondering if
      there was something I was missing.
      The syntax for inheriting the Form class isn't just "the conventional  
      method".  It's the only way to do it.  If you want to create a custom form  
      class -- that is, a new class that inherits the existing Form class -- you 
      have to use that syntax and inherit the Form class (either directly or via 
      a class that itself inherits the Form class).
      Thanks a bunch for the reply.

      --
      Richard Carpenter

      Comment

      • Lav

        #4
        Re: Sub-classing windows forms controls

        Can you paste the code where you find the behavior strange ?

        Comment

        Working...