Creating Forms with fixed set of controls

D

Doc John

I need to create several Windows Forms and I'm thinking of creating a base
Form from which I can derive all Forms. Is it possible to include something
in that Base Form so that all Forms I create will use some specific controls
with some specific properties? For example, if I add to the Base Form a
button with a black background ans Arial Font, will all the Forms be
required to use this same Button?

Thanks.
 
P

Peter Duniho

I need to create several Windows Forms and I'm thinking of creating a
base
Form from which I can derive all Forms. Is it possible to include
something
in that Base Form so that all Forms I create will use some specific
controls
with some specific properties? For example, if I add to the Base Form a
button with a black background ans Arial Font, will all the Forms be
required to use this same Button?

Define "required".

If you create a form in the designer, you can use that Form-derived class
as a base class for new forms. Anything you put into the form will be
inherited by subclasses. There will always be a way for the subclasses to
disable or hide various controls you include in the base class, and you
can make this easier to do (i.e. in the designer or by directly accessing
the control members) by changing the access modifier for the base form's
controls in the designer.

Actually, if you really want to impose a requirement, there are ways that
you can prevent the sub-classed form from changing the properties of the
various controls, even if they are accessed indirectly. But it's not
clear whether you want to be able to do this, or if you're just asking if
the sub-class would be required by default.

Pete
 
D

Doc John

Thanks for the post.

The problem is that I'm working on an existing Windows application which
includes many Forms, which in turn include many controls. Since we're
changing its appearance so it looks better and more up-to-date (a nicer
GUI), I need to change, for example, all the buttons in the project so that
their background color is green. Since that property isn't being used, I
can't Find/Replace.
So I was thinking that, somehow, I could create a Base Form so that all
these Form would inherit from it. The same way they'll inherit the Form
backcolor when I change the background, maybe there's a way that all buttons
can inherit from a "base button".


Thanks.
 
P

Peter Duniho

[...]
So I was thinking that, somehow, I could create a Base Form so that all
these Form would inherit from it. The same way they'll inherit the Form
backcolor when I change the background, maybe there's a way that all
buttons
can inherit from a "base button".

If the button itself exists as an instance on the base form, then that
would work. All the forms inheriting the base form would also inherit the
base button _instance_ and that _instance_ would have the green background
you specify in the base form.

The only way to make a base button _class_ that all the other buttons in
the project inherit would be to go ahead and make that inherited button
class, and then change all of the button instances in all of the forms to
use that class instead of the Button class. A simple search-and-replace
might actually accomplish that, but it's an even more broad change than
the already-broad suggestion of changing all your forms to derive from a
single base form.

An alternative to the inheriting of a form with a button instance, or of
changing your buttons to use a new derived button class, would be to make
the base form as suggested before, but instead of putting the button
instance in the form itself for the inheriting classes to inherit,
override the OnLoad() method in the base form class, enumerate all your
controls in the form, and for every button change the background color to
whatever you want.

I am not aware of a general-purpose way that you can do something simple
and have all of your controls draw in a different color, for example. As
of Windows XP, there's an idea called "visual styles", but AFAIK this is
controlled by the OS; you can take advantage of it to use the "current
style" for your UI, but your application cannot override the current
visual style by automatically using a different visual style for rendering.

Pete
 
L

Linda Liu [MSFT]

Hi Doc,

I agree to Peter. He has given an excellent reply : )

If you want to change all the buttons in your application to have the green
back color, I suggest you create a custom button that has a green back
color and make all the buttons on the forms in your application of the type
of the custom button.

To do this, open each form's .Designer.cs file and select the definition
part of the buttons on the form and change the type of those buttons to the
custom button using the Find and Replace edit function. You also need to
modify some lines of code in the InitializeComponent method to construct
those buttons with the custom button type. For example:

this.button1 = new CustomButton();

Hope this helps.
If you have any question, please feel free to let me know.

Sincerely,
Linda Liu
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
L

Linda Liu [MSFT]

Hi Doc,

How about the problem now?

If you have any question, please feel free to let me know.

Thank you for using our MSDN Managed Newsgroup Support Service!

Sincerely,
Linda Liu
Microsoft Online Community Support
 

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