Sub-classing windows forms controls

R

Richard Carpenter

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?
 
R

Richard Carpenter

[...]
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.
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top