简体   繁体   English

如何防止控件从其父GroupBox继承Font

[英]How to prevent controls inherit Font from their parent GroupBox

I'm developing Windows Forms application and have a GroupBox with many Labels and TextBoxes. 我正在开发Windows窗体应用程序,并且有一个带有许多Labels和TextBoxes的GroupBox If I change the GroupBox's font then the Label's and TextBox's font are also changes. 如果我更改了GroupBox的字体,那么Label和TextBox的字体也会更改。 Is there a way to change the GroupBox's font without change Label's and TextBox's font in it? 有没有一种方法可以更改GroupBox的字体而不更改其中的Label和TextBox的字体?

You need to set them, otherwise they will be inherited. 您需要设置它们,否则它们将被继承。 So just change the font of the labels inside. 因此,只需更改内部标签的字体。

You can change the Font of the controls at run-time to the Font of the group box parent: 您可以在运行时将控件的字体更改为组框父代的字体:

InitializeComponent();

foreach(Control c in this.groupBox1.Controls)
    c.Font = c.Parent.Parent.Font;

For few group boxes: 对于几个分组框:

foreach(Control p in new[] {groupBox1, groupBox2})
    foreach (Control c in p.Controls)
        c.Font = c.Parent.Parent.Font;     // or just p.Parent.Font;

I often dock a Panel to Fill inside the GroupBox. 我经常将面板停靠在GroupBox中。 Then I change the Panel Font to what I want. 然后,将面板字体更改为所需的字体。 Then I add all the labels and other controls. 然后添加所有标签和其他控件。 They will inherit from the Panel rather than the GroupBox. 它们将从面板继承,而不是从GroupBox继承。

In Design mode you can go to View > Other Windows > Document Outline 在设计方式下,您可以转到视图>其他Windows>文档大纲
Now you can notice the controls are under the group box and inherit the group box font. 现在您可以注意到控件位于组框下并继承了组框字体。
In the designer, you can move the controls a bit out of the group box and then you can move the control with the arrow keys so that it is above the group box but not in it. 在设计器中,您可以将控件稍微移出组框,然后使用箭头键将控件移至组框上方但不在组框内。

[ [ 在此处输入图像描述] [1]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM