我写了一个关于Combox 的代码
int[] i = { 5, 6, 7, 80 };
            foreach (int ii in i)
            {
                toolastripcombox.Items.Add(i);
 private void ChangeFontSize( int  fontSize)   //改变文字大小
        {
            if (fontSize <= 0)
                throw new InvalidProgramException("字号参数错误,不能小于等于0.0");            RichTextBox tempRichTextBox = new RichTextBox();            int curRtbStart = textEditor.SelectionStart;            int len = textEditor.SelectionLength;
            int tempRtbStart = 0;            Font font = textEditor.SelectionFont;
            if (len <= 1 && null != font)
            {
                textEditor.SelectionFont = new Font(font.Name, fontSize, font.Style);
                return;
            }
            tempRichTextBox.Rtf = textEditor.SelectedRtf;
            for (int i = 0; i < len; i++)
            {
                tempRichTextBox.Select(tempRtbStart + i, 1);
                tempRichTextBox.SelectionFont =
                    new Font(tempRichTextBox.SelectionFont.Name,
                             fontSize, tempRichTextBox.SelectionFont.Style);
            }
            tempRichTextBox.Select(tempRtbStart, len);
            textEditor.SelectedRtf = tempRichTextBox.SelectedRtf;
            textEditor.Select(curRtbStart, len);
            textEditor.Focus();
        }
       private void toolastripcombox_SelectedIndexChanged(object sender, EventArgs e)
        {
            ChangeFontSize(Int32.Parse(toolastripcombox.SelectedItem.ToString()));        }

出现错误,提示是输入的格式不正确。不知道哪里错了?         

解决方案 »

  1.   

    本帖最后由 net_lover 于 2011-09-09 21:43:07 编辑
      

  2.   

    刚加载时初始化是“”,加载时会执行selectIndex事件,空值转换数值型肯定会报错。判断一下就可以了。
      

  3.   

    也不能说的这么绝对,combox 的item 本身就是数字呢出现这样的情况只能说明,楼主的combox中的item是字符串类型的
    这句Int32.Parse(comboBox1.SelectedItem.ToString());就通不过编译
      

  4.   

    ChangeFontSize(Int32.Parse(toolastripcombox.SelectedItem.Text.ToString()));
      

  5.   

    toolastripcombox.SelectedItem是个对象应该是一个行对象,而不是其值。.ToString()是调用这个对象的ToString()方法,你可以用SelectedText或SelectedValue。
      

  6.   


    MessageBox.Show(toolastripcombox.SelectedItem.ToString());
    这样显示的是数字?不可能吧
      

  7.   

    toolastripcombox.SelectedItem.ToString()
    这个LZ你调试一哈看 值是多少
    这样调试
    string str=toolastripcombox.SelectedItem.ToString();
    看看str值是多少
      

  8.   

    调试以后显示的system.In32.怎么不显示数字那。
      

  9.   

    显示的是system.Int32????这个明显是没取到值撒LZ 
      

  10.   

    何不这样呢LZ if(判断是否=null)
    if(XX==null)
    {
         XX=0;//为空的时候来个默认值就不会报错了
    }
      

  11.   

    调试出来了,错误的地方在int[] i = { 5, 6, 7, 80 };
      foreach (int ii in i)
      {
      toolastripcombox.Items.Add(i);

    应该为
    for (int j = 0; j < i.Length; j++)
                {
                    toolastripcombox.Items.Add(i[j]);
                }
    原因我不清楚。请高手指点一下。