如何更改字体样式(常规、粗体等)?
到目前为止,我所拥有的是:
if (listBox1.SelectedIndex == 3)
{
if (textBox1.Text == "regular")
{
//FontStyle regular = new FontStyle !!!wrong one!!!
}
}
所以我需要的是,当我在文本框中输入“常规”时,字体样式将变为常规。我怎么做?
您需要设置Control.FontWeight属性
if (listBox1.SelectedIndex == 3)
{
if (textBox1.Text == "regular")
{
TextBox1.FontWeight = FontWeights.Regular;
}
}
您是否正在寻找这样的东西:
textBox1.Font = new Font(textBox1.Font, FontStyle.Bold);
您需要创建一个新字体并将其应用于您的文本框:
textBox1.Font = new Font(FontFamily.GenericSansSerif, 12.0F, FontStyle.Normal);
这将创建一个具有新系列的系列等。您可以从现有的 Font 对象中读取大小和系列以保留它们。
textBox1.Font = new Font(textBox1.Font, FontStyle.Normal);
有关 Font 类属性的更多信息,请参阅MSDN 页面