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

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

问题描述

一个标签和一个按钮选项的形式。通过点击一个新的形式与2个单选按钮'Font1'和'Font2,和两个按钮打开按钮,应用和取消。当选择其中一个单选按钮,然后单击应用将第一个表单上的标签更改字体的脸。现在的问题是如何改变字体作为从说宋体为Arial或标签的任何其他字体。



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

 私人无效btnApply_Click(对象发件人,EventArgs五)
{
如果(radioFont1.Checked)
{
mainForm.lblName.Font.Name =宋体; 错误的尝试
}
this.DialogResult = DialogResult.OK;
}



第一种形式的标签,以便它是第二种形式可见宣言:

 公共静态标签lblName =新的Label(); 



...

 私人无效mainForm_Load(对象发件人,EventArgs五)
{
lblName = lblBarName;
}


解决方案

Font.Name Font.XYZProperty 等是只读的字体是不可变对象,所以你需要指定一个新的 字体 对象来替代它:

  mainForm.lblName.Font =新的字体(宋体,MainForm中。 lblName.Font.Size); 



检查字体类的构造函数进一步的选项。


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.

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 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);

Check the constructor of the Font class for further options.

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

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