开发者

Change Fonts of a WinForm's controls

I have a main form. On clicking Tools->Options from my app开发者_JAVA技巧's menu I'm showing another form (OptionsForm) which contains options to change font used by the controls.I'm storing the chosen font in Properties:Settings:Default:some_object. I'm able to retrieve it and update my controls too, but only on clicking of a button on my MainForm.

I tried calling a function written on MainForm from OptionsForm by instantiating the MainForm to update the control.Font --> not working.

How to make sure that whenever i change the font in Properties:Settings:Default:some_object, all the controls reflect the change ?

Thanks, Dev


  1. Locate and expand the "(ApplicationSettings)" property for a control whose font should be affected by this setting in the property grid.
  2. Click the "..." button next to the "(PropertyBinding)" sub-property.
  3. In the pop-up window, select "New" from the font property's drop-down box.
  4. Enter a name like "UserFont" in the "Name" field.
  5. Repeat steps 1 through 4 for every control whose font should be affected, except select the same setting you created before instead of selecting "New" in step 3.
  6. In your options dialog, make sure you have some control bound to the same setting, and update the font of that control when you change the font for the application. For example:

private void button1_Click(object sender, EventArgs e)
{
    if (DialogResult.OK == fontDialog1.ShowDialog(this))
    {
        button1.Font = fontDialog1.Font;
    }
}

This one line can affect the font of every control in the application because they are all bound to the same application setting now. Very handy.

Edit: Or you can update the setting directly (instead of updating one of the controls bound to it) with code like this:

WindowsFormsApplication1.Properties.Settings.Default.UserFont = fontDialog1.Font;


As per my understanding you want to set a Font for all the controls in a Form in the Form Load.

Try this code.

Form1 -

Form2 frm2 = new Form2();
frm2.ShowDialog(this);

And in the Form2- Button click

Properties.Settings.Default.MyFont = this.Font;
            Properties.Settings.Default.Save();
            (from Control ctrl in this.Owner.Controls
             select ctrl).ToList().ForEach(ctrl => ctrl.Font = Properties.Settings.Default.MyFont);

This code will find all the control in the Form and assign the Font.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜