Click here to Skip to main content
15,894,460 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hello sir,
I want to set default background of all forms,
default font,font color,font family for all controls on whole Application.

Sir how to do this.

thanks in advance.
Posted
Comments
BillWoodruff 26-Feb-15 2:10am    
By 'windowBase do you mean a 'Win Forms Application ?

What have you tried so far ? What do you observe is the default behavior ?

There are several ways you can do this; I'll outline here what I think is the simplest way, which may, or may not, meet the OP's goals. In the future I intend to publish an article here on CP about a "fancier" way to do this.

But, first, note that there is already a kind-of inheritance of Color, and Font, built-in: when you create a WinForm, and you set its Font and Fore/Back- Color properties: when you drag-drop Controls onto that Form certain Types of Controls will inherit the Font and Fore/Back-Color properties (for example, Panel, GroupBox, Button, Label, PictureBox all inherit the BackColor). For the Controls Button, and Label, their Text will inherit the ForeColor of the Form.

How-to, Simple:

1. Create your main UI form as usual.

2. Add another Form to your Project, give it a name like "ThemeForm."

a. configure your ThemeForm's Size, Color, Font, etc., settings so they define the "common look and feel" that you want other Forms to inherit.

3. Add other Forms to your Project: change their Class declaration so they inherit from your ThemeForm:
C#
public partial class SecondaryForm1 : ThemeForm
{
    public SecondaryForm1 ()
    {
        InitializeComponent();
    }
}
Discussion:

1. If you put Controls on your "ThemeForm," then those Controls will also appear in "locked" form on any Form that inherits from "ThemeForm." So, if you define a Button Click EventHandler for a Button on the ThemeForm, and you click that Button in a derived Form, the EventHandler defined in the ThemeForm will fire.

That may be a behavior you don't want, or a behavior you will want to take advantage of for enabling certain types of communication between inherited Forms.

For example, if I define this Click EventHandler for 'button1 in the ThemeForm:
C#
private void button1_Click(object sender, EventArgs e)
{
    MessageBox.Show(string.Format("in ThemeForm: firing from: {0}",(sender as Control).Parent.Name ));
}
, and I click on 'button1 in a derived Form named 'Form2, I will get the message: "firing from: Form2"

How to "get rid of" inherited Controls you don't want to use in derived Forms (not as simple as you might think), and how to add EventHandlers to the derived Controls, while either removing or leaving active, its inherited EventHandlers ... is something I intend to address fully in the article I plan to write.

2. Caution: if things get "messed up" so you get an error message trying to show the Design View of an inheriting Form ... but you can still run your application ... just save everything in the current Project, close it, and then re-open it in Visual Studio.
 
Share this answer
 
There is no automatic way to do this: a modified font style is not "inherited" when you cretae a child control. You will have to create a Font class instance and loop through all controls applying it to then individually. You will have to do the same for the individual Color instances as well.

Once you have done that, changes to that Font instance will be applied when the controls are repainted (which you can force by calling Invalidate for the form). However, Color changes will not be propagated in this way, as Color is a struct and thus a value type. You will have to change the Color values of each control yourself each time it changes.

It might be worth looking at skinning your form, but that gets very complex and / or expensive.
 
Share this answer
 
Comments
BillWoodruff 26-Feb-15 16:50pm    
I'm going to dare to disagree with you on this one :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900