如何从C#中的另一个组合框中删除从组合框中选择的项目

时间:2019-04-05 15:18:50

标签: c# winforms combobox

两个组合框中的项目列表相同。我的问题是,如果我从第一个组合框中选择一个项目,我想从第二个组合框中删除或不显示该项目,但是该项目将保留在第一个组合框中。

//////////////this is how i fill combobox
public void fillcombobox()
    {
        String selectQuery = "SELECT * FROM candidate_list";
        connection.Open();
        cmd = new MySqlCommand(selectQuery, connection);
        mdr = cmd.ExecuteReader();
        try
        {
            while (mdr.Read())
            {
               if (mdr.GetString("candidate_position").Equals("PRO"))
                {
                    //PRO1
                    cbo_Pro1.Text = "-- SELECT CANDIDATE --";
                    cbo_Pro2.Text = "-- SELECT CANDIDATE --";
                    string fname = mdr.GetString("candidate_name");
                    string spacee = " ";
                    string lname = mdr.GetString("candidate_surname");
                    cbo_Pro1.Items.Add(fname + spacee + lname);
                    cbo_Pro2.Items.Add(fname + spacee + lname);
                }
            }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }

        connection.Close();
    }


////////////this is the event for 1st combobox

private void cbo_Pro1_SelectedIndexChanged(object sender, EventArgs e)
    {
        pro1Name = cbo_Pro1.Text.Split(null);

        String selectQuery = "Select * FROM candidate_list WHERE candidate_name LIKE '%" + pro1Name[0] + "%'";

        cmd = new MySqlCommand(selectQuery, connection);

        da = new MySqlDataAdapter(cmd);

        DataTable table = new DataTable();


        da.Fill(table);

        byte[] image = (byte[])table.Rows[0][5];

        pictureBox8.Image = byteArrayToImage(image);

        da.Dispose();

    }

//////////////////this is the event for second combobox

private void cbo_Pro2_SelectedIndexChanged(object sender, EventArgs e)
    {
        pro2Name = cbo_Pro2.Text.Split(null);

        String selectQuery = "Select * FROM candidate_list WHERE candidate_name LIKE '%" + pro2Name[0] + "%'";

        cmd = new MySqlCommand(selectQuery, connection);

        da = new MySqlDataAdapter(cmd);

        DataTable table = new DataTable();


        da.Fill(table);

        byte[] image = (byte[])table.Rows[0][5];

        pictureBox9.Image = byteArrayToImage(image);

        da.Dispose();

    }

1 个答案:

答案 0 :(得分:0)

您说两个ComboBox都有完全相同的项目,

这很简单:

private void cbo_Pro1_SelectedIndexChanged(object sender, EventArgs e)
{
     cbo_Pro2.Items.Remove(cbo_Pro1.Text);
}

基本上,当您在cbo_Pro1中选择一项时,请在cbo_Pro2中删除该特定项。