vb.net 在继承的标签上设置 Font.Bold

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15536549/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 12:54:14  来源:igfitidea点击:

Set Font.Bold on inherited label

vb.netwinformsfontscontrols

提问by Wine Too

I have to set .Font.Bold = True on label which havent defined .Font property but inherite Font (name, size, style) from form. For that I erases her .Font property from form's designer file.

我必须在没有定义 .Font 属性但从表单继承字体(名称、大小、样式)的标签上设置 .Font.Bold = True 。为此,我从表单的设计器文件中删除了她的 .Font 属性。

Now I need to set text of this label to be bold without defining font name, size etc. for this label.

现在我需要将此标签的文本设置为粗体,而无需为此标签定义字体名称、大小等。

I try:

我尝试:

label6.Font.Bold = True

But this don't work (Property .Font.Bold is readonly).
If I set font for this label like:

但这不起作用(Property .Font.Bold 是只读的)。
如果我为此标签设置字体,例如:

label6.Font = New Font(myfontname, 10, FontStyle.Bold, GraphicsUnit.Point)

then I get bold text but label then don't inherite form's font size anymore.

然后我得到粗体文本但标签然后不再继承表单的字体大小。

Is here possible to keep form's font inheritance to label but get bold text on such label?

是否可以将表单的字体继承保留为标签,但在此类标签上使用粗体文本?

回答by Cody Gray

No, as you've already discovered the Font.Boldproperty is read-only. Font objects are immutable, meaning that their properties cannot be changed once they have been created. The only way to modify this property is to create a new Font object.

不,因为您已经发现该Font.Bold属性是只读的。Font 对象是不可变的,这意味着它们的属性一旦创建就无法更改。修改此属性的唯一方法是创建一个新的 Font 对象。

When creating this new Font, you can certainly copy the properties of an existing Font object (like the one used by your form), but there is no way to dynamically couple the two Font objects together. If the font size used by your form changes, a new Font object will be created with that new size for the form, but your custom bold Font object will not be updated.

创建这个新 Font 时,您当然可以复制现有 Font 对象的属性(如表单使用的那个),但无法将两个 Font 对象动态耦合在一起。如果表单使用的字体大小发生更改,将使用该表单的新大小创建一个新的 Font 对象,但不会更新您的自定义粗体 Font 对象。

The only thing that makes this confusing is that there is a bit of magic that goes on if you do not set a custom font for child controls. They automatically inherit the font of their parent (a container control, such as a form). Such properties that retrieve their value from their parent when they have not been explicitly set are referred to as ambient properties. Ambient properties are explained in the documentationwhere applicable. But the upshot is that the ambience goes away at the point where you explicitly set the property. So forget about that.

唯一让人困惑的是,如果您没有为子控件设置自定义字体,就会有一些魔力。它们会自动继承其父级(容器控件,例如表单)的字体。此类在未明确设置时从其父级检索其值的属性称为环境属性。环境属性在适用的文档中进行了解释。但结果是,当您明确设置属性时,氛围就会消失。所以忘掉那个吧。

To achieve what you want, we need to get a notification when the form's font size changes and in response, you can create a new bold Font object with the new size for your Label control. Luckily, there is just such a mechanism in the form of the FontChangedevent. Handle the FontChangedevent for your form, and in response, create a new Font object for your Label control. For example:

为了实现您想要的效果,我们需要在表单的字体大小更改时收到通知,作为响应,您可以为 Label 控件创建一个具有新大小的新粗体 Font 对象。幸运的是,FontChanged事件的形式正好有这样一种机制。FontChanged为您的表单处理事件,作为响应,为您的 Label 控件创建一个新的 Font 对象。例如:

Private Sub myForm_FontChanged(ByVal sender As Object, ByVal e As EventArgs) Handles myForm.FontChanged
    Dim oldFont As Font = myLabel.Font
    myLabel.Font = New Font(myForm.Font, myForm.Font.Style Or FontStyle.Bold)
    oldFont.Dispose()
End Sub

Although, I'm not sure if/why this is really necessary. It's rare that the font size of a form gets changed while the application is running. Generally that happens only at creation, in which case when you retrieve the value to create the custom Font object for your Label control, it would already be set correctly.

虽然,我不确定这是否/为什么真的有必要。在应用程序运行时,表单的字体大小很少发生变化。通常,这仅在创建时发生,在这种情况下,当您检索值以为 Label 控件创建自定义 Font 对象时,它已经被正确设置。