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

标签 c# winforms if-statement combobox operators

我正在使用 C# winforms 我有一个带有名为 cmbExport 的组合框和两个文本框 txtDateSendtxtSendNum 的表单 组合框从存储过程 GET_ALL_EXPORT_WITHNULL 获取数据

cmbExport.DataSource = cmp.GET_ALL_EXPORT_WITHNULL(); 
cmbExport.DisplayMember = "side";   
cmbExport.ValueMember = "ID_EXPORT";  
cmbExport.SelectedValue = "6";

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

我尝试了这段代码,但它不起作用:

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;
    } 

谢谢

最佳答案

由于没有数字可以同时是 642,因此每个数字都不同于 642,因此您的 if 语句始终计算结果为 true。我认为您打算在那里使用 && 而不是 || :

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

关于c# - 使用 if 语句将组合框值与两个文本框值进行比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30019578/

相关文章:

c# - 在 ContinueWith(anotherTask) + C# 任务并行库中共享变量

c# - 如何在 Windows 10 和 > .net 4.5 中用 c# 枚举音频输入设备?

c# - 如何根据其他单元格中的值禁用(只读)DataGridView CheckBox 列中的单元格?

c# - 使用 AutoSize=false 防止标签自动换行

c - 我的 if 语句与我想要的相反

java - Java 中使用循环的 if 语句和搜索字符串数组

javascript - JSLint 错误 : "unnecessary else after disruption"

c# - 如何使用反射访问内部类

c# - 如何在 JavaScript 中调用 Web.api 序列化数组

c# - 如何从 Windows 应用程序中抓取数据网格