繁体   English   中英

奇怪的错误(输入字符串的格式不正确。)

[英]Strange error (Input string is not in correct format.)

这是我的代码:

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出现以下异常:

在此处输入图片说明

我该怎么办才能解决此错误?

这是失败的:

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

不管您在其他Form1上执行了什么操作,它都不会在此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);

当firstHeight1引发异常时,它的值是什么?

您可能要看int.TryParse():

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

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

有关int.TryParse的详细信息: http : //msdn.microsoft.com/zh-cn/library/f02979c7.aspx

编辑:问题是您要重新实例化Form1,并且永远不会在Form2的新实例中设置值。 您应该在Form2中将一个属性设置为该值。

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

...

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

由于您的firstHeight1String.Empty ,因此在Int类型中找不到等效的空字符串。 因此错误。

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

Form2当您说:

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

您没有访问以前使用过的Form1 ,而是在创建一个具有空文本框值的全新Form1

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

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

然后在按钮中单击:

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

你可以做这样的事情。

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

您的代码看起来像

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 
    }

}}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM