代码:
private void comboBox_SelectedIndexChanged(object sender, EventArgs e)//在一个comboBox控件中的三个选项
{
if (comboBox.SelectedIndex == 0)//当选择了“常规”
{
textBox1.Font = new Font(textBox1.Font, textBox1.Font.Style ^ FontStyle.Bold);//去掉粗体
textBox1.Font = new Font(textBox1.Font, textBox1.Font.Style ^ FontStyle.Italic);//去掉斜体
textBox1.Font = new Font(textBox1.Font, textBox1.Font.Style | FontStyle.Regular);//设置成原始字形
}
if (comboBox.SelectedIndex == 1))//当选择了“粗体”
{
textBox1.Font = new Font(textBox1.Font, textBox1.Font.Style ^ FontStyle.Italic);//去掉斜体
textBox1.Font = new Font(textBox1.Font, textBox1.Font.Style | FontStyle.Bold);//设置成粗体
}
if (comboBox.SelectedIndex == 2))//当选择了“斜体”
{
textBox1.Font = new Font(textBox1.Font, textBox1.Font.Style ^ FontStyle.Bold);//去掉粗体
textBox1.Font = new Font(textBox1.Font, textBox1.Font.Style | FontStyle.Italic);//设置成斜体
}
}
但是为什么不能实现我们经常看到的设置字体时的哪种效果呢?不知我说明白了没有
private void comboBox_SelectedIndexChanged(object sender, EventArgs e)//在一个comboBox控件中的三个选项
{
if (comboBox.SelectedIndex == 0)//当选择了“常规”
{
textBox1.Font = new Font(textBox1.Font, textBox1.Font.Style ^ FontStyle.Bold);//去掉粗体
textBox1.Font = new Font(textBox1.Font, textBox1.Font.Style ^ FontStyle.Italic);//去掉斜体
textBox1.Font = new Font(textBox1.Font, textBox1.Font.Style | FontStyle.Regular);//设置成原始字形
}
if (comboBox.SelectedIndex == 1))//当选择了“粗体”
{
textBox1.Font = new Font(textBox1.Font, textBox1.Font.Style ^ FontStyle.Italic);//去掉斜体
textBox1.Font = new Font(textBox1.Font, textBox1.Font.Style | FontStyle.Bold);//设置成粗体
}
if (comboBox.SelectedIndex == 2))//当选择了“斜体”
{
textBox1.Font = new Font(textBox1.Font, textBox1.Font.Style ^ FontStyle.Bold);//去掉粗体
textBox1.Font = new Font(textBox1.Font, textBox1.Font.Style | FontStyle.Italic);//设置成斜体
}
}
但是为什么不能实现我们经常看到的设置字体时的哪种效果呢?不知我说明白了没有
//多了一个")"吧?
if (comboBox.SelectedIndex == 2))//当选择了“斜体”多半事件没触发吧,加上试试:
comboBox.SelectedIndexChanged += new System.EventHandler(comboBox_SelectedIndexChanged)
{
if (comboBox1.SelectedIndex == 0)//当选择了“常规”
{
textBox1.Font = new Font(textBox1.Font, textBox1.Font.Style & (~FontStyle.Bold));//去掉粗体
textBox1.Font = new Font(textBox1.Font, textBox1.Font.Style &(~ FontStyle.Italic));//去掉斜体
textBox1.Font = new Font(textBox1.Font, textBox1.Font.Style &(~ FontStyle.Regular));//设置成原始字形
}
if (comboBox1.SelectedIndex == 1)//当选择了“粗体”
{
textBox1.Font = new Font(textBox1.Font, textBox1.Font.Style ^ FontStyle.Bold);//(如果不想开关式,将^换成|)
}
if (comboBox1.SelectedIndex == 2)//当选择了“斜体”
{
textBox1.Font = new Font(textBox1.Font, textBox1.Font.Style ^ FontStyle.Italic);//(如果不想开关式,将^换成|)
} }
{
if (this.comboBox1.SelectedIndex == 0)//当选择了“常规”
{
textBox1.Font = new Font(textBox1.Font, FontStyle.Regular); //textBox1.Font = new Font(textBox1.Font, textBox1.Font.Style ^ FontStyle.Bold);//去掉粗体
//textBox1.Font = new Font(textBox1.Font, textBox1.Font.Style ^ FontStyle.Italic);//去掉斜体
//textBox1.Font = new Font(textBox1.Font, textBox1.Font.Style | FontStyle.Regular);//设置成原始字形
}
if (comboBox1.SelectedIndex == 1)
{
textBox1.Font = new Font(textBox1.Font, FontStyle.Bold); //textBox1.Font = new Font(textBox1.Font, textBox1.Font.Style ^ FontStyle.Italic);//去掉斜体
//textBox1.Font = new Font(textBox1.Font, textBox1.Font.Style | FontStyle.Bold);//设置成粗体
}
if (comboBox1.SelectedIndex == 2)//当选择了“斜体”
{
textBox1.Font = new Font(textBox1.Font, FontStyle.Italic); //textBox1.Font = new Font(textBox1.Font, textBox1.Font.Style ^ FontStyle.Bold);//去掉粗体
//textBox1.Font = new Font(textBox1.Font, textBox1.Font.Style | FontStyle.Italic);//设置成斜体
}
}
if (comboBox.SelectedItem.ToString()=="常规")
{
textBox1.Font = new Font(textBox1.Font, textBox1.Font.Style | FontStyle.Regular);
}
if (comboBox.SelectedItem.ToString()=="粗体")
{
textBox1.Font = new Font(textBox1.Font, textBox1.Font.Style | FontStyle.Bold);
}
if (comboBox.SelectedItem.ToString()=="斜体")
{
textBox1.Font = new Font(textBox1.Font, textBox1.Font.Style | FontStyle.Italic);
}
ps:
if (comboBox.SelectedIndex == 1))//当选择了“粗体”
和
if (comboBox.SelectedIndex == 2))//当选择了“斜体”
都多了一个反括号
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
FontStyle fs = FontStyle.Regular;
if (comboBox1.SelectedIndex == 0)
{
fs = FontStyle.Regular;
}
if (comboBox1.SelectedIndex == 1)
{
fs = FontStyle.Regular;
fs |= FontStyle.Bold;
textBox1.Font = new Font(textBox1.Font, fs);
}
if (comboBox1.SelectedIndex == 2)
{
fs = FontStyle.Regular;
fs = FontStyle.Italic;
textBox1.Font = new Font(textBox1.Font, fs);
}
}
{
if (comboBox1.SelectedIndex == 0)//当选择了“常规”
{
textBox1.Font = new Font(textBox1.Font, FontStyle.Regular);//去掉粗体
}
if (comboBox1.SelectedIndex == 1)//当选择了“粗体”
{
textBox1.Font = new Font(textBox1.Font, FontStyle.Bold);//设置成粗体
}
if (comboBox1.SelectedIndex == 2)//当选择了“斜体”
{
textBox1.Font = new Font(textBox1.Font, FontStyle.Italic);//设置成斜体
} }
,难到又是粗体又是常规? 本身有常规和粗体的枚举的情况下系统会显示什么?
{
switch (index)
{
case 0:
return FontStyle.Bold;
case 1:
return FontStyle.Italic;
default:
return FontStyle.Regular;
}
}
这样才能达到目的。