using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;namespace Proj2_2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }        private void Form1_Load(object sender, EventArgs e)
        {
            int a, b, c;
            a = Convert.ToInt16(textBox1.Text);
            b = Convert.ToInt16(textBox2.Text);
            c = a + b;
            textBox3.Text = Convert.ToString(c);
        }
    }
}
这个会报错,这是我按照书上来写的。报错但可以继续执行,可是并不能进行加法计算。求帮助!以下是报错的详细情况:
System.FormatException: 输入字符串的格式不正确。
   在 System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal)
   在 System.Number.ParseInt32(String s, NumberStyles style, NumberFormatInfo info)
   在 System.Int16.Parse(String s, NumberStyles style, NumberFormatInfo info)
   在 System.Convert.ToInt16(String value)
   在 Proj2_2.Form1.Form1_Load(Object sender, EventArgs e) 位置 E:\visual studio 2010\Projects\Proj2-2\Proj2-2\Form1.cs:行号 22
   在 System.Windows.Forms.Form.OnLoad(EventArgs e)
   在 System.Windows.Forms.Form.OnCreateControl()
   在 System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
   在 System.Windows.Forms.Control.CreateControl()
   在 System.Windows.Forms.Control.WmShowWindow(Message& m)
   在 System.Windows.Forms.Control.WndProc(Message& m)
   在 System.Windows.Forms.ScrollableControl.WndProc(Message& m)
   在 System.Windows.Forms.ContainerControl.WndProc(Message& m)
   在 System.Windows.Forms.Form.WmShowWindow(Message& m)
   在 System.Windows.Forms.Form.WndProc(Message& m)
   在 System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   在 System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   在 System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
C#textbox报错

解决方案 »

  1.   

    System.FormatException: 输入字符串的格式不正确 
    提示的很清楚了,
    Convert.ToInt16(textBox1.Text);
    Convert.ToInt16(textBox2.Text);
    主要是这两块出错,原因可能是文本框内容为空,或者你输入的还是整数。
      

  2.   

     输入字符串的格式不正确Form1_Load函数在窗体Load的时候发生,那时候你的textBox1.Text的值不是数字,所以转换的时候Convert.ToInt16(textBox1.Text);就出错了!
      

  3.   

    1.确认你在文本框中输入的是数字
    2.确认输入的数字小于32768(因为代码中转为Int16类型)
      

  4.   

    a = Convert.ToInt16(textBox1.Text);
    b = Convert.ToInt16(textBox2.Text);
    textBox1.Text textBox2.Text默认为空值
    Convert.ToInt16()函数需要一个数字类型的字符串参数
    哪本书会这样写程序的啊
    先给textBox1.Text = "1";textBox2.Text="2";
    唉 路漫漫其修远兮啊
      

  5.   

    你这个加法是写在Form1_Load里面,也就是说窗体一加载完就相加,此时你两个textBox的值应该为空,所以Convert.ToInt16的时候出错了
    除非你两个textBox已经有默认值
      

  6.   


    书本没错啊!
    你在设计的界面上,给textBox1 和textBox2里分别写上12和34,就不会出错了!