格式异常未处理
此代码给出了未处理的格式异常!
我想从文本框中获取数值。我还有其他方法可以采用吗?
该表单(form2)用作自定义消息框,从我的主表单(form1)调用两次。因此数组的大小为 3。
Int32[] g = new Int32[3];
Int32[] h = new Int32[3];
int TwoClicks = 0;
private void Form2_Load(object sender, EventArgs e)
{
g[TwoClicks] = (Int32.Parse(textBox1.Text) * 60 + Int32.Parse(textBox2.Text));
h[TwoClicks] = (Int32.Parse(textBox3.Text) * 60 + Int32.Parse(textBox4.Text));
}
This code is giving format exception unhandled!
I want to get the numerical values from the textboxes. Is there any other method i can adopt?
This form(form2) is used as a custom message box, which is called twice from my main form(form1). Therefore the arrays are of size 3.
Int32[] g = new Int32[3];
Int32[] h = new Int32[3];
int TwoClicks = 0;
private void Form2_Load(object sender, EventArgs e)
{
g[TwoClicks] = (Int32.Parse(textBox1.Text) * 60 + Int32.Parse(textBox2.Text));
h[TwoClicks] = (Int32.Parse(textBox3.Text) * 60 + Int32.Parse(textBox4.Text));
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
答案取决于您想要实现什么目标。如果解析成功,您可以使用 TryParse 方法来获取解析值和信息,如果有错误,您可以根据情况执行一些适当的操作,例如返回 0 作为 Textbox 的值。您可以做的其他事情是用
try cach
包围您的代码,并在引发异常时执行某些操作。Answer depends on what do u want to achive. u can use
TryParse
method to get parsed value and information if parsing was sucesful, it there was error u can do something appropriate to the situation like return 0 as value of Textbox. Other thing u can do is suround the code u have withtry cach
and do something when exception is thrown.使用列表尝试一下:
Try it using a List: