C# 如何比较两个文本框,如果两者都为空,如何在c中打印消息框#
我在这里比较两个文本框,并试图打印一条错误消息,如果两者都是空的C# 如何比较两个文本框,如果两者都为空,如何在c中打印消息框#,c#,C#,我在这里比较两个文本框,并试图打印一条错误消息,如果两者都是空的 int ina=int.Parse(txttea.Text); int inb = int.Parse(txtcoffee.Text); int inc=0, ind=0; if(this.txttea.Text=="" && this.txtcoffee.Text=="") { MessageBox
int ina=int.Parse(txttea.Text);
int inb = int.Parse(txtcoffee.Text);
int inc=0, ind=0;
if(this.txttea.Text=="" && this.txtcoffee.Text=="")
{
MessageBox.Show("select a item");
txttea.Focus();
}
在以下行中需要
|
而不是&&
:
if(this.txttea.Text=="" && this.txtcoffee.Text=="")
注意:问题与标题不符。而不是
&
您需要在以下行中填写|
:
if(this.txttea.Text=="" && this.txtcoffee.Text=="")
注意:问题与标题不符。应如下所示。请编辑您的问题以匹配下面提供的答案
int ina=int.Parse(txttea.Text);
int inb = int.Parse(txtcoffee.Text);
int inc=0, ind=0;
if(this.txttea.Text=="" || this.txtcoffee.Text=="")
{
MessageBox.Show("select an item");
txttea.Focus();
}
应为以下内容,请编辑您的问题以匹配下面提供的答案
int ina=int.Parse(txttea.Text);
int inb = int.Parse(txtcoffee.Text);
int inc=0, ind=0;
if(this.txttea.Text=="" || this.txtcoffee.Text=="")
{
MessageBox.Show("select an item");
txttea.Focus();
}
使用
int.Parse
解析空字符串将出现异常。我的意思是:int.Parse(“”)
导致:输入字符串的格式不正确。
要解决该问题,请改用:
当然,您也可以单独测试它们[首先是ina,然后是inb,反之亦然]:
int ina;
if (int.TryParse(txttea.Text, out ina))
{
int inb;
if (int.TryParse(txtcoffee.Text, out inb))
{
//Ok, more code here
}
else
{
//got a wrong format, MessageBox.Show or whatever goes here
}
}
else
{
//got a wrong format, MessageBox.Show or whatever goes here
}
现在,关于比较空字符串,如果要在两个字符串均为空时显示消息:
if(this.txttea.Text == "" && this.txtcoffee.Text == "")
{
MessageBox.Show("select a item");
txttea.Focus();
}
另一方面,如果您希望在至少一条消息为空时收到该消息:
if(this.txttea.Text == "" || this.txtcoffee.Text == "")
{
MessageBox.Show("select a item");
txttea.Focus();
}
使用
int.Parse
解析空字符串将出现异常。我的意思是:int.Parse(“”)
导致:输入字符串的格式不正确。
要解决该问题,请改用:
当然,您也可以单独测试它们[首先是ina,然后是inb,反之亦然]:
int ina;
if (int.TryParse(txttea.Text, out ina))
{
int inb;
if (int.TryParse(txtcoffee.Text, out inb))
{
//Ok, more code here
}
else
{
//got a wrong format, MessageBox.Show or whatever goes here
}
}
else
{
//got a wrong format, MessageBox.Show or whatever goes here
}
现在,关于比较空字符串,如果要在两个字符串均为空时显示消息:
if(this.txttea.Text == "" && this.txtcoffee.Text == "")
{
MessageBox.Show("select a item");
txttea.Focus();
}
另一方面,如果您希望在至少一条消息为空时收到该消息:
if(this.txttea.Text == "" || this.txtcoffee.Text == "")
{
MessageBox.Show("select a item");
txttea.Focus();
}
您的问题是如何验证
文本框是否为空或空白
如果您使用的是.Net 3.5或更高版本,则最好使用
if(string.IsNullOrWhiteSpace(txttea.Text) ||
string.IsNullOrWhiteSpace(txtcoffee.Text))
{
MessageBox.Show("select a item");
txttea.Focus();
return;
}
您的问题是如何验证文本框是否为空或空白
如果您使用的是.Net 3.5或更高版本,则最好使用
if(string.IsNullOrWhiteSpace(txttea.Text) ||
string.IsNullOrWhiteSpace(txtcoffee.Text))
{
MessageBox.Show("select a item");
txttea.Focus();
return;
}
不,你想要什么?标题和书面问题不同!不,你想要什么?标题和书面问题不同!