【问题标题】:Strange error (Input string is not in correct format.)奇怪的错误(输入字符串的格式不正确。)
【发布时间】:2012-10-02 18:40:43
【问题描述】:

这是我的代码:

namespace Class_Properties {
    public partial class Form1 : Form {
        private string firstHeight1 = "";
        public int firstHeight {
            get {
                        return Convert.ToInt32( firstHeight1 );
            }
        }

        public Form1() {
            InitializeComponent();
        }

        private void button1_Click( object sender, EventArgs e ) {
            firstHeight1 = textBox2.Text;

            Form2 secondForm = new Form2();
            secondForm.Show();
        }
    }
}

然后是另一个类:

namespace Class_Properties {
    public partial class Form2 : Form {
        public Form2() {
            InitializeComponent();
            Form1 mainWindow = new Form1();
            this.Height = mainWindow.firstHeight;
        }
    }
}

当我运行时,我输入200 作为textbox2 的值并单击button1,然后Visual Studio 会出现以下异常:

我可以做些什么来解决这个错误?

【问题讨论】:

    标签: c# winforms exception types compiler-errors


    【解决方案1】:

    这是失败:

            InitializeComponent();
            Form1 mainWindow = new Form1();
            this.Height = mainWindow.firstHeight;  //<--
    

    无论您在另一个 Form1 上做了什么,它都不会出现在这个 firstHeight == string.Empty 上,因为它是一个新实例,所以解析会失败。

    您必须将现有的 Form1 发送到 Form2:

    public Form2(Form1 parent)
    {
        this.Height = parent.firstHeight;
    }
    
    // called like so from Form1:
    var form2 = new Form2(this);
    

    诚然,最好只发送你需要的东西:

    public Form2(int desiredHeight)
    {
        this.Height = desiredHeight;
    }
    
    // called like so from Form1:
    var form2 = new Form2(this.firstHeight);
    

    【讨论】:

    • 这行得通...所以我必须明白我可以通过将Form1 作为参数传递给Form2() 来完成这项工作...
    • @Victor:我真的建议只将您需要的内容发送到 Form2 而不是 Form1 本身。如果可以的话,耦合是一种避免的习惯,并且根据您的示例,Form2 确实不需要了解 Form1。
    【解决方案2】:

    抛出异常时firstHeight1的值是多少?

    您可能想查看 int.TryParse() 代替:

    int output = 0;
    int.TryParse(firstHeight1, out output);
    return output;
    

    如果无法解析该值,则不会设置output,而是引发异常。

    有关 int.TryParse 的更多信息:http://msdn.microsoft.com/en-us/library/f02979c7.aspx

    编辑:问题是您正在重新实例化 Form1,并且从未在 Form2 的新实例 Form1 中设置该值。您应该将 Form2 中的属性设置为该值。

    class Form2
    {
        public int FirstHeight { get; set; }
    }
    

    ...

    Form2 form2 = new Form2();
    form2.FirstHeight = this.FirstHeight;
    form2.Show();
    

    【讨论】:

      【解决方案3】:

      由于您的 firstHeight1 是 String.Empty 并且在 Int 类型中找不到空字符串的等效项。因此错误..

      在您的 Form2 实例中,该值仍然是 String.Empty。首先将其设置为某个值。

      【讨论】:

        【解决方案4】:

        在Form2 当你说:

        Form1 mainWindow = new Form1();
        this.Height = mainWindow.firstHeight;
        

        您访问的不是您之前使用的Form1,而是创建一个带有空文本框值的全新地址。

        你应该做的是在创建时将高度值传递给第二种形式:

        public Form2(int height) 
        {
           // use height here
        }
        

        在按钮中点击:

        Form2 secondForm = new Form2(firstHeight);
        secondForm.Show();
        

        【讨论】:

          【解决方案5】:

          你可以这样做。

          public int? FirstHeight
          {
              get
              {
                  int? returnValue = null;
          
                  int temp;
                  if (int.TryParse(textBox2.Text, out temp))
                  {
                      returnValue = temp;
                  }
          
                  return returnValue;
              }
          }
          

          然后,您只需在需要该值时调用FirstHeight 属性:

          if (FirstHeight.HasValue)
          {
              // Access the int value like so:
              int height = FirstHeight.Value;
          }
          

          如果你不想使用 Nullable 类型,你可以这样做:

          public int FirstHeight
          {
              get
              {
                  int returnValue; // Or some other default value you can check against.
          
                  if (!int.TryParse(textBox2.Text, out returnValue))
                  {
                      // If the call goes in here, the text from the input is not convertable to an int.
                      // returnValue should be set to 0 when int.TryParse fails.
                  }
          
                  return returnValue;
              }
          }
          

          【讨论】:

            【解决方案6】:

            你的代码看起来像

            public int FirstHeight
             {
               get
                 {
                  double Num;
                  bool isNum = double.TryParse(firstheight1, out Num);
                  if (isNum)
                 {
                   return firstheight1;
                  }
                else
                {
                 return 0; or 
                 your message goes here 
                }
            

            } }

            【讨论】:

              猜你喜欢
              • 2015-10-28
              • 1970-01-01
              • 2013-04-30
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2021-05-19
              相关资源
              最近更新 更多