使用if语句将组合框值与两个文本框值进行比较

时间:2022-11-29 18:02:15

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

当用户不从组合框中选择某些值并且其中一个文本框为空时,会出现一个meesage框

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

3 个解决方案

#1


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. I think you meant to use && instead of || there:

由于数字不能同时为6和42,因此每个数字都不同于6或42,因此if语句的计算结果始终为true。我认为你打算使用&&而不是||那里:

if (x != 6 && x != 42 && x != 1042 && string.IsNullOrEmpty(sn)

#2


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. 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.

这提高了可读性,您可以根据某些规则动态创建dependsOnSendNumber列表。因此,如果在任何时候创建了遵循相同规则的新选项,则您唯一需要做的就是相应地设置它以包含在列表中。

#3


You can't use int x = cmbExport.SelectedValue because mbExport.SelectedValue return String

你不能使用int x = cmbExport.SelectedValue,因为mbExport.SelectedValue返回String

#1


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. I think you meant to use && instead of || there:

由于数字不能同时为6和42,因此每个数字都不同于6或42,因此if语句的计算结果始终为true。我认为你打算使用&&而不是||那里:

if (x != 6 && x != 42 && x != 1042 && string.IsNullOrEmpty(sn)

#2


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. 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.

这提高了可读性,您可以根据某些规则动态创建dependsOnSendNumber列表。因此,如果在任何时候创建了遵循相同规则的新选项,则您唯一需要做的就是相应地设置它以包含在列表中。

#3


You can't use int x = cmbExport.SelectedValue because mbExport.SelectedValue return String

你不能使用int x = cmbExport.SelectedValue,因为mbExport.SelectedValue返回String