C++ c++/cli检查值是否为number

C++ c++/cli检查值是否为number,c++,if-statement,c++-cli,C++,If Statement,C++ Cli,我制作了一个包含3个文本框的windows窗体应用程序。为了使程序正常运行,我需要在所有三个框中填入一个数字。它们可以是正的,也可以是负的 我用了这个: if(this->textBox1->Text=="" || this->textBox2->Text=="" || this->textBox3->Text=="") { MessageBox::Show("Error"); } else { // continue with the pro

我制作了一个包含3个文本框的windows窗体应用程序。为了使程序正常运行,我需要在所有三个框中填入一个数字。它们可以是正的,也可以是负的

我用了这个:

if(this->textBox1->Text=="" || this->textBox2->Text=="" || this->textBox3->Text=="") {
    MessageBox::Show("Error");
}
else {
    // continue with the program...
}
检查框是否已填充,但如果有字母或其他符号,与数字不同,我不知道如何显示错误消息。

使用
Double.TryParse()

doublex;
数组^inputs=gcnew数组(3);
输入[0]=此->文本框1;
输入[1]=此->文本框2;
输入[2]=此->文本框3;
对于(int i=0;iLength;i++)
{
如果(!Double::TryParse(输入[i]>Text,x))
{
MessageBox::Show(“错误”,字符串::格式(“无法将文本框{0}解析为数字”,i+1));
}
}

我想你真的想对这些数字做点什么

因此,转换失败测试:

int number1;
if (!int::TryParse(textBox1->Text, number1)) {
    MessageBox::Show("First box wasn't an integer");
    return;
}

double number2;
if (!double::TryParse(textBox2->Text, number2)) {
    MessageBox::Show("Second box wasn't numeric");
    return;
}
最后,您将在计算中使用数字
number1
number2


不再需要对空字符串进行单独测试,因为 TyPARSE < /C>如果输入为空,将返回false。这适用于int,但当我输入类似于2的值时,double会出错。5@user2765257:我以前使用过

TryParse
作为double,没有问题,所以我建议你再看看你的代码,你可能没有转换你认为你是什么。所以这就是我得到错误的地方:“number=System::Convert::ToInt32(this->textBox1->Text);”TryParse传递了这个数字,但当我试图转换它时,程序中断。@user2765257:不要使用
convert::ToInt32
。您已经从
TryParse
中获得了值。至于您的错误,是因为在
double::TryParse
之后,您应该使用
double::Parse
Convert::ToDouble
,而不是
Convert::ToInt32
。但是第二次转换又有什么意义呢?哦,是的,你一开始就这么说了,但我没有明白很好,谢谢你的帮助。
int number1;
if (!int::TryParse(textBox1->Text, number1)) {
    MessageBox::Show("First box wasn't an integer");
    return;
}

double number2;
if (!double::TryParse(textBox2->Text, number2)) {
    MessageBox::Show("Second box wasn't numeric");
    return;
}