C#如何更改标签的字体 [英] C# How to change font of a label

查看:33
本文介绍了C#如何更改标签的字体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

带有标签和按钮选项"的表单.通过单击该按钮,将打开一个带有 2 个单选按钮Font1"和Font2"以及两个按钮应用"和取消"的新表单.选择一个单选按钮并单击应用"后,第一个表单上的标签将更改字体.问题是如何将字体从 Tahoma 更改为 Arial 或标签的任何其他字体.

A form with a label and a button 'Options'. By clicking the button a new form opens with 2 radio buttons 'Font1' and 'Font2', and two buttons 'Apply' and 'Cancel'. Upon selecting one of the radio buttons and clicking 'Apply' will make the label on the first form change the font face. The problem is how to change the font as in from say Tahoma to Arial or to any other font face of the label.

应用按钮的选项表单代码,如果被点击将返回 dialogresult.ok == true 并更改第一个表单上标签的字体:

Options form code for apply button, which if was clicked will return dialogresult.ok == true and change the font of the label on the first form:

private void btnApply_Click(object sender, EventArgs e)
{
    if (radioFont1.Checked)
    {
        mainForm.lblName.Font.Name = "Arial"; 'wrong attempt 
    }
    this.DialogResult = DialogResult.OK;
}

在第一个表单上声明标签,以便它对第二个表单可见:

Declaration of the label on first form so that it is visible to second form:

public static Label lblName = new Label();

...

private void mainForm_Load(object sender, EventArgs e)
{
    lblName = lblBarName;
}

推荐答案

Font.Name, Font.XYZProperty, etc are readonly as Font 是一个不可变对象,所以你需要指定一个新的Font 对象来替换它:

Font.Name, Font.XYZProperty, etc are readonly as Font is an immutable object, so you need to specify a new Font object to replace it:

mainForm.lblName.Font = new Font("Arial", mainForm.lblName.Font.Size);

检查 Font 类的构造函数以获得更多选项.

Check the constructor of the Font class for further options.

这篇关于C#如何更改标签的字体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆