2012-10-02 82 views
1

这是我的代码:奇怪的错误(输入的字符串格式不正确。)

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中说,以下情况除外:

enter image description here

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

回答

1

这是失败:

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

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

你必须对现有的Form1中发送到窗体2:

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); 
+0

这个工作...所以我必须明白,我可以通过将Form1作为参数传递给Form2()... ... – Victor

+0

@维克多:我真的建议只发送你需要的Form2而不是Form1本身。如果可以的话,耦合是一种避免的习惯,并且基于你的例子,Form2真的不需要了解Form1。 –

+0

好的。非常感谢你! :) – Victor

0

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中和值永远不会在Form1的新实例,距离窗体2。您应该将Form2中的属性设置为该值。

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

...

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

OP说价值是'200' ... – Servy

0

由于您firstHeight1String.Empty并没有类似空字符串可以Int类型中找到。因此,错误......

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

0

Form2当你说:

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

您没有访问Form1您已经使用较早,你要创建一个全新的具有空文本框的值。

什么,你应该做的是经过高度值到第二种形式创建时:

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

,并点击按钮:

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

你可以做这样的事情。

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

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

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

你的代码看起来像

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 
    } 

} }