代码:
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);//设置成斜体
   }
}
但是为什么不能实现我们经常看到的设置字体时的哪种效果呢?不知我说明白了没有

解决方案 »

  1.   


    //多了一个")"吧?
    if (comboBox.SelectedIndex == 2))//当选择了“斜体”多半事件没触发吧,加上试试:
    comboBox.SelectedIndexChanged += new System.EventHandler(comboBox_SelectedIndexChanged)
      

  2.   

     private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
            {
                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);//(如果不想开关式,将^换成|)
                }        }
      

  3.   

    private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
            {
                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);//设置成斜体
                }
            }
      

  4.   

    我又稍稍改动了一下:但还是不行
    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);
    }
      

  5.   

    不能实现吗?我用你的代码可以实现啊,你看是不是你其他地方错了。
    ps:
    if (comboBox.SelectedIndex == 1))//当选择了“粗体” 

    if (comboBox.SelectedIndex == 2))//当选择了“斜体”  
    都多了一个反括号
      

  6.   

    别获取这个textBox1.Font.Style,直接 FontStyle各项异或,与
      

  7.   


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

  8.   

    private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
            {
                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);//设置成斜体 
      }         }
      

  9.   

    textBox1.Font.Style | FontStyle.Italic 我觉得像这种代码不如直接指定
    ,难到又是粗体又是常规? 本身有常规和粗体的枚举的情况下系统会显示什么?  
      

  10.   

    换个思路,这种思路要是,你们的SelectedItem有多个呢,10,100,岂不是每次都要去掉很多,代码也吓人不妨用基本的样式去+上想要的样式.
      

  11.   

    private FontStyle GetFontStyle(int index)
      {
        switch (index)
        {
          case 0:
            return FontStyle.Bold;
          case 1:
            return FontStyle.Italic;
          default:
            return FontStyle.Regular;
        }
      }
      

  12.   

    textBox1.Font = new Font(textBox1.Font, textBox1.Font.Style&~FontStyle.Bold);//去掉粗体 
    这样才能达到目的。