C++/CLI 检查值是否为数字
c++/cli check if value is number
我做了一个带有3个文本框的Windows表单应用程序。为了使程序正常工作,我需要用数字填充所有三个框。它们可以是积极的,也可以是消极的。
我用了这个:
if(this->textBox1->Text=="" || this->textBox2->Text=="" || this->textBox3->Text=="") {
MessageBox::Show("Error");
}
else {
// continue with the program...
}
检查框是否已填充,但如果有一个符号,如字母或其他东西,与数字不同,我无法弄清楚如何显示错误消息。
我假设你真的想用数字做点什么?
因此,测试转换失败:
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
的数字,以便在计算中使用。
您将不再需要对空字符串进行单独的测试,因为如果输入为空TryParse
将返回 false。
使用 Double.TryParse()
:
Double x;
array<TextBox^>^ inputs = gcnew array<TextBox^>(3);
inputs[0] = this->textBox1;
inputs[1] = this->textBox2;
inputs[2] = this->textBox3;
for (int i = 0; i < inputs->Length; i++)
{
if(!Double::TryParse(inputs[i]->Text, x))
{
MessageBox::Show("Error", String::Format("Cannot parse textBox{0} as number", i+1));
}
}
相关文章:
- 检查输入是否不是整数或数字
- 用于检查数字是否有数字或可被该数字的值整除的程序
- 如何知道n!是否可以表示为三个连续数字的乘法?
- 如何将 UTF-8 文本从文件转换为某个可以迭代的容器,并检查每个符号是否为C++字母数字?
- 如何使用递归检查数字是否有重复数字?
- 查找数字是否为 2 的幂的时间复杂度
- 将 C 函数转换为 C++ 以检查数字是否有效
- C ++:检查它是否是类中的数字
- 检查数字是否为素数的算法
- 如何检查用户的输入是否有效以及我正在寻找的数字?
- 找到 x^n 的所有组合,并检查它们的总和是否等于一个不包括相同数字的数字
- 是否有一个 std::set 函数来确定不超过数字 x 的最大元素?
- 如何确定integer_sequence在编译时是否包含给定的数字?
- 有没有办法检查用户输入是否是数字?
- 如何知道文本文件中的输入是否是 C++ 中的有效数字
- C++:这两种将数字写入矩阵的方式之间是否存在显着的速度差异?
- 如何检查cin是否与数组中的一组数字匹配
- 如何检查字符串是否包含所有这些:数字、字母和特殊字符
- 在C/C++中检查数字是否为整数(不带scanf/gets/etc)
- 检查字符串是否是数字,然后将该数字转换为 int?