C# 检查是否已选择组合框值-C.net

C# 检查是否已选择组合框值-C.net,c#,.net,if-statement,combobox,messagebox,C#,.net,If Statement,Combobox,Messagebox,我正试图通过处理异常来改进我的应用程序。我有一个表单,如果所有字段都未填写,则向用户显示一个消息框。以下是我尝试过的,但即使所有字段都已完成,也不会让我通过 if (textBox1.Text == null || comboBox3.SelectedValue == null || comboBox4.SelectedValue == null || comboBox5.SelectedValue == null || comboBox8.SelectedValue ==

我正试图通过处理异常来改进我的应用程序。我有一个表单,如果所有字段都未填写,则向用户显示一个消息框。以下是我尝试过的,但即使所有字段都已完成,也不会让我通过

if (textBox1.Text == null || comboBox3.SelectedValue == null || 
    comboBox4.SelectedValue == null || 
    comboBox5.SelectedValue == null || comboBox8.SelectedValue == null)
{
    MessageBox.Show("Please make sure you don't have any missing fields");
}
else
{
    connection.Open();

    //update the settings to the database table 
    MySqlCommand command = connection.CreateCommand();
    // Insert into table_name values ("","name","1")
    command.CommandText = @"insert into CustomTests values ('','" + textBox1.Text + "'," + Convert.ToBoolean(checkBox1.CheckState) + ",'" + comboBox3.Text + "'," + comboBox4.Text + "," + comboBox5.Text + ",'" + comboBox8.Text + "'," + comboBox2.Text + "," + Timer_Enabled + ",'" + comboBox1.Text + "')";

    command.ExecuteNonQuery();
}

不要检查它是否为空。
检查文本长度是否大于0

不要检查文本长度是否为空。 检查文本长度是否大于0

文本框的文本值设置为空字符串,而不是空字符串。接下来,我不使用ComboBox.SelectedValue,而是使用ComboBox.SelectedIndex,如果未选择任何内容,则检查它是否不是默认值-1

 if (textBox1.Text == "" || comboBox3.SelectedIndex == -1 
     || comboBox4.SelectedIndex == -1 || comboBox5.SelectedIndex == -1 
     || comboBox8.SelectedIndex == -1)
TextBox的文本值设置为空字符串,而不是null。接下来,我不使用ComboBox.SelectedValue,而是使用ComboBox.SelectedIndex,如果未选择任何内容,则检查它是否不是默认值-1

 if (textBox1.Text == "" || comboBox3.SelectedIndex == -1 
     || comboBox4.SelectedIndex == -1 || comboBox5.SelectedIndex == -1 
     || comboBox8.SelectedIndex == -1)
您可以尝试以下方法:

if (string.IsNullOrEmpty(textBox1.Text) || comboBox3.SelectedIndex == -1 || comboBox4.SelectedIndex == -1 ||
     comboBox5.SelectedIndex == -1 || comboBox8.SelectedIndex == -1)
 {
     MessageBox.Show("Please make sure you don't have any missing fields");
 }
 else
 {
     connection.Open();

     //update the settings to the database table 
     MySqlCommand command = connection.CreateCommand();
     // Insert into table_name values ("","name","1")
     command.CommandText = @"insert into CustomTests values ('','" + textBox1.Text + "'," + Convert.ToBoolean(checkBox1.CheckState) + ",'" + comboBox3.Text + "'," + comboBox4.Text + "," + comboBox5.Text + ",'" + comboBox8.Text + "'," + comboBox2.Text + "," + Timer_Enabled + ",'" + comboBox1.Text + "')";

     command.ExecuteNonQuery();
 }
您可以尝试以下方法:

if (string.IsNullOrEmpty(textBox1.Text) || comboBox3.SelectedIndex == -1 || comboBox4.SelectedIndex == -1 ||
     comboBox5.SelectedIndex == -1 || comboBox8.SelectedIndex == -1)
 {
     MessageBox.Show("Please make sure you don't have any missing fields");
 }
 else
 {
     connection.Open();

     //update the settings to the database table 
     MySqlCommand command = connection.CreateCommand();
     // Insert into table_name values ("","name","1")
     command.CommandText = @"insert into CustomTests values ('','" + textBox1.Text + "'," + Convert.ToBoolean(checkBox1.CheckState) + ",'" + comboBox3.Text + "'," + comboBox4.Text + "," + comboBox5.Text + ",'" + comboBox8.Text + "'," + comboBox2.Text + "," + Timer_Enabled + ",'" + comboBox1.Text + "')";

     command.ExecuteNonQuery();
 }

你也可以试试这个

if (string.IsNullOrEmpty(textBox1.Text) || string.IsNullOrEmpty(this.comboBox3.Text) || string.IsNullOrEmpty(this.comboBox4.Text) ||
     string.IsNullOrEmpty(this.comboBoxSelect5.Text)|| string.IsNullOrEmpty(this.comboBox8.Text))

你也可以试试这个

if (string.IsNullOrEmpty(textBox1.Text) || string.IsNullOrEmpty(this.comboBox3.Text) || string.IsNullOrEmpty(this.comboBox4.Text) ||
     string.IsNullOrEmpty(this.comboBoxSelect5.Text)|| string.IsNullOrEmpty(this.comboBox8.Text))

如果你能举个这样的例子会很有帮助的。。comboBox4.SelectedIndex>0?您可以选择“索引”或“长度”组合框。SelectedIndex>0或“组合框”。SelectedValue.length>0如果您能给出一个类似的示例,将会很有帮助。。comboBox4.SelectedIndex>0?您可以检查索引或长度组合框。SelectedIndex>0或Combox4.SelectedValue.length>0它仍然不起作用,即使我完成了所有字段,它仍然会抛出错误,不让它通过它仍然不起作用,即使我已经完成了所有字段,它仍然会抛出错误,不会让它通过