[英]using if statement to compare a combo box value with two text boxes value
I'm using C# winforms I have a form with combobox called cmbExport
and two textboxes txtDateSend
and txtSendNum
The combobox get its data from a stored procedure GET_ALL_EXPORT_WITHNULL
我正在使用C#winforms,我有一个带有组合框的窗体,名为cmbExport
,两个文本框为txtDateSend
和txtSendNum
。组合框从存储过程GET_ALL_EXPORT_WITHNULL
获取数据
cmbExport.DataSource = cmp.GET_ALL_EXPORT_WITHNULL();
cmbExport.DisplayMember = "side";
cmbExport.ValueMember = "ID_EXPORT";
cmbExport.SelectedValue = "6";
When the user NOT choose certain values from the combobox and one of the textboxes are empty a meesage box appear 当用户未从组合框中选择某些值并且文本框之一为空时,将出现消息框
I tried this code but it didn't work: 我尝试了这段代码,但是没有用:
int x = Convert.ToInt32(cmbExport.SelectedValue); //Its already integer but the code didn't accept int x = cmbExport.SelectedValue; ???
string ds = txtDateSend.Text;
string sn = txtSendNum.Text;
if ((x != 6 || x != 42 || x != 1042) && string.IsNullOrEmpty(sn))
{
MessageBox.Show("you should enter a send number");
return;
}
else if ((x != 6 || x != 42 || x != 1042) && string.IsNullOrEmpty(ds))
{
MessageBox.Show("you should enter a date send);
return;
}
thank you 谢谢
Since no number can be both 6
and 42
, every number is different from either 6
or 42
, so your if
statement always evaluates to true. 由于没有数字可以同时是6
和42
,因此每个数字都不同于6
或42
,因此您的if
语句始终求值为true。 I think you meant to use &&
instead of ||
我认为您打算使用&&
而不是||
there: 那里:
if (x != 6 && x != 42 && x != 1042 && string.IsNullOrEmpty(sn)
You can improve the intention revealing of your code by creating a list, for example: 您可以通过创建列表来改善代码的使用意图,例如:
var dependsOnSendNumber = new [] {6, 42, 1042};
And then simply use a Linq query: 然后只需使用Linq查询:
if (dependsOnSendNumber.Contains(x) && string.IsNullOrEmpty(sn))
This improves readability, and you can make the dependsOnSendNumber list to be created dynamically accordingly to some rule. 这样可以提高可读性,并且您可以根据某些规则动态创建dependsOnSendNumber列表。 So, if anytime a new option is created that follows the same rule, the only thing you need to do is to set it accordingly to be included in the list. 因此,如果任何时候创建遵循相同规则的新选项,您唯一需要做的就是相应地设置它以包含在列表中。
您不能使用int x = cmbExport.SelectedValue
因为mbExport.SelectedValue
返回String
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.