如果Else语句无法识别我的comboBox

本文关键字:识别 我的 comboBox Else 语句 如果 | 更新日期: 2023-09-27 17:59:02

我的if-else语句有问题。当我提交信息时,它应该阅读组合框中的文本,并给它一个定义数字。然而,它直接下降到其他地方并输入了它。你能告诉我我做错了什么吗。

try
        {
            string combo;
            if (comboBox1.SelectedText == "Random Pool")
            {
                combo = "10";
            }
            if (comboBox1.SelectedText == "Other")
            {
                combo = "20";
            }
            if (comboBox1.SelectedText == "DOT Pool")
            {
                combo = "30";
            }
            if (comboBox1.SelectedText == "Follow up")
            {
                combo = "40";
            }
            if (comboBox1.SelectedText == "Pre-employement Screening")
            {
                combo = "50";
            }
            if (comboBox1.SelectedText == "Aberrant Behavior")
            {
                combo = "60";
            }
            if (comboBox1.SelectedText == "Incident/Near Miss Investigation")
            {
                combo = "70";
            }
            if (comboBox1.SelectedText == "Investigation")
            {
                combo = "80";
            }
            else
            {
                combo = "20";
            }
            string cmdstring = "INSERT INTO Test (SELECTION_DATE, TEST_REASON_CODE, PEOPLESOFT_EMPL_ID, TEST_TYPE_CODE) VALUES(@date, '" + combo + "', @emp, 10);";
            using (OleDbCommand cmd = new OleDbCommand(cmdstring, con))
            {
                cmd.Parameters.Add("@date", OleDbType.Date).Value = dateTimePicker1.Value;
                cmd.Parameters.Add("@emp", OleDbType.Char).Value = textBox1.Text;
                con.Open();
                cmd.ExecuteNonQuery();
                con.Close();
                MessageBox.Show("Submitted Successfully");
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show("Failed due to " + ex.Message);
        }

如果Else语句无法识别我的comboBox

问题是,在您的代码中,它正在检查if (comboBox1.SelectedText == "Investigation"),如果没有,它会将combo设置为"20"。要么写if,else-if,else if等。即使前面的if条件中的任何一个已经执行,底部的那个总是获胜,你也可以做如下所示的事情:

        string combo;
        if (comboBox1.SelectedText == "Random Pool")
        {
            combo = "10";
        }
        else if (comboBox1.SelectedText == "Other")
        {
            combo = "20";
        }
        else if (comboBox1.SelectedText == "DOT Pool")
        {
            combo = "30";
        }
        else if (comboBox1.SelectedText == "Follow up")
        {
            combo = "40";
        }
        else if (comboBox1.SelectedText == "Pre-employement Screening")
        {
            combo = "50";
        }
        else if (comboBox1.SelectedText == "Aberrant Behavior")
        {
            combo = "60";
        }
        else if (comboBox1.SelectedText == "Incident/Near Miss Investigation")
        {
            combo = "70";
        }
        else if (comboBox1.SelectedText == "Investigation")
        {
            combo = "80";
        }
        else
        {
            combo = "20";
        }

或者,您可以简单地创建一个函数:

string GetCombo()
{
    if (comboBox1.SelectedText == "Random Pool")
        return "10";
    if (comboBox1.SelectedText == "Other")
        return "20";
    if (comboBox1.SelectedText == "DOT Pool")
        return "30";
    if (comboBox1.SelectedText == "Follow up")
        return "40";
    if (comboBox1.SelectedText == "Pre-employement Screening")
        return "50";
    if (comboBox1.SelectedText == "Aberrant Behavior")
        return "60";
    if (comboBox1.SelectedText == "Incident/Near Miss Investigation")
        return "70";
    if (comboBox1.SelectedText == "Investigation")
        return "80";
    return "20";
}

在你的代码中,

string combo = GetCombo();
string cmdstring = "INSERT INTO Test (SELECTION_DATE, TEST_REASON_CODE, PEOPLESOFT_EMPL_ID, TEST_TYPE_CODE) VALUES(@date, '" + combo + "', @emp, 10);";
// ...

希望它能有所帮助!:)

编辑:正如Asad所提到的,你可以使用switch语句,比如:

string GetCombo()
{
    switch(comboBox1.SelectedText)
    {
        case "Random Pool":
            return "10";
        case "Other":
            return "20";
        case "DOT Pool":
            return "30";
        case "Follow up":
            return "40";
        case "Pre-employement Screening":
            return "50";
        case "Aberrant Behavior":
            return "60";
        case "Incident/Near Miss Investigation":
            return "70";
        case "Investigation":
            return "80";
        default:
            return "20";
    }    

}

像这样尝试

更改Text而不是Selected Text

try
        {
            string combo;
            if (comboBox1.Text== "Random Pool")
            {
                combo = "10";
            }
            if (comboBox1.Text== "Other")
            {
                combo = "20";
            }
            if (comboBox1.Text== "DOT Pool")
            {
                combo = "30";
            }
            if (comboBox1.Text== "Follow up")
            {
                combo = "40";
            }
            if (comboBox1.Text== "Pre-employement Screening")
            {
                combo = "50";
            }
            if (comboBox1.Text== "Aberrant Behavior")
            {
                combo = "60";
            }
            if (comboBox1.Text== "Incident/Near Miss Investigation")
            {
                combo = "70";
            }
            if (comboBox1.Text== "Investigation")
            {
                combo = "80";
            }
            else
            {
                combo = "20";
            }
            string cmdstring = "INSERT INTO Test (SELECTION_DATE, TEST_REASON_CODE, PEOPLESOFT_EMPL_ID, TEST_TYPE_CODE) VALUES(@date, '" + combo + "', @emp, 10);";
            using (OleDbCommand cmd = new OleDbCommand(cmdstring, con))
            {
                cmd.Parameters.Add("@date", OleDbType.Date).Value = dateTimePicker1.Value;
                cmd.Parameters.Add("@emp", OleDbType.Char).Value = textBox1.Text;
                con.Open();
                cmd.ExecuteNonQuery();
                con.Close();
                MessageBox.Show("Submitted Successfully");
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show("Failed due to " + ex.Message);
        }