开发者

C# Ways to handle dynamic font sizing

开发者 https://www.devze.com 2023-04-01 08:32 出处:网络
What would be a good way to dynamically change the font size on my application?I have many screens with many labels.Those labels are at least inheriting from a common label.The other issue is should 开

What would be a good way to dynamically change the font size on my application? I have many screens with many labels. Those labels are at least inheriting from a common label. The other issue is should 开发者_StackOverflowI leave the labels to autosize and just use the line breaks in the label so that it can break up? I've switched many of the labels to not be autosized because it was going wide and not wrapping itself.

Currently I have everything set to anchor, etc. and any of the buttons and such will be fine. It's just the font now that needs to be sized dyanmically.

Thanks!


Before InitializeComponent(); in each form's constructor, simply put this.Font = new Font( ... ); as you desire. However, it will only cascade through the controls if you left each control at the default. You can always put a loop after the initialization:

foreach(Control c in this.Controls)
{
    if(c is Label) //if you want to change Labels only
        c.Font = new Font( ... );
}

If it makes things look weird, change your AutoScaleMode and related properties.

To address the question of how to handle wrapping the label text, use Label1.AutoSize = true, and simply set Label1.MaximumSize = new Size(x, 0);, where x is your maximum width.

That all said, if you're going to be dynamically scaling things often, you really should look into using WPF instead of WinForms. It has more ability to handle these types of tasks automatically.


You can save the font size as an integer in the application settings. Then your application will remember its font state upon start when you get the font size.

    Properties.Settings.Default.FontSize = 3;
    Properties.Settings.Default.Save();

Then as said above use a foreach loop.

    foreach(Control c in this.Controls)
    {
     c.Font = new Font( .. );
    // if(c is Panel)
    //   {
    //     foreach(Control d in c.Controls)
    //     {
    //     d.Font = new Font( .. );
    //     }
       }
    }
0

精彩评论

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