簡體   English   中英

根據文本框值更改組合框值? C#

[英]Change comboBox value based on textBox value? c#

我正在制作 PC 配置程序,但我被卡住了。 在第一種形式中,我選擇了 CPU 和顯示在文本框中的信息,但在第二種形式中,我選擇了 MoBo。 CPU信息之一是socket,它是主板形式的第一個comboBox。 有沒有辦法將 textBox 中的值與該組合框鏈接? 我也使用了 Sql Studio。

我真的需要幫助,我很不擅長編程,我這樣做只是為了不讓這個學期失敗。 請幫助我。

這是我的那個組合框的代碼圖片

私有無效 MaticnaPloca_Load(對象發送者,EventArgs e){

        con.Open();
        SqlCommand sc = new SqlCommand("SELECT distinct(soket) from MaticnaPloca", con);
        SqlDataReader reader;
        reader = sc.ExecuteReader();
        DataTable dt = new DataTable();
        dt.Columns.Add("soket", typeof(string));
        dt.Load(reader);
        cbSoket.ValueMember = "soket";
        cbSoket.DataSource = dt;
        cbSoket.SelectedIndex = -1;
        con.Close();

        con.Open();
        SqlCommand sc1 = new SqlCommand("SELECT distinct(proizvodjac) from MaticnaPloca order by Proizvodjac", con);
        SqlDataReader reader1;
        reader1 = sc1.ExecuteReader();
        DataTable dt1 = new DataTable();
        dt1.Columns.Add("proizvodjac", typeof(string));
        dt1.Load(reader1);
        comboBox2.ValueMember = "proizvodjac";
        comboBox2.DataSource = dt1;
        comboBox2.SelectedIndex = -1;
        comboBox2.Enabled = false;
        con.Close();

        con.Open();
        SqlCommand sc2 = new SqlCommand("SELECT (model) from MaticnaPloca order by model", con);
        SqlDataReader reader2;
        reader2 = sc2.ExecuteReader();
        DataTable dt2 = new DataTable();
        dt2.Columns.Add("model", typeof(string));
        dt2.Load(reader2);
        comboBox3.ValueMember = "model";
        comboBox3.DataSource = dt2;
        comboBox3.SelectedIndex = -1;
        comboBox3.Enabled = false;
        con.Close();
        
    }       
            
    private void cbSoket_SelectedIndexChanged(object sender, EventArgs e)
    {            
        
        try
        {

            SqlCommand sc = new SqlCommand();
            con.Open();           
            if (cbSoket.SelectedValue.ToString() == "1151")
                sc = new SqlCommand("select distinct (proizvodjac) from MaticnaPloca where Soket = '1151'", con);

            else if (cbSoket.SelectedValue.ToString() == "AM4")
                sc = new SqlCommand("select distinct (proizvodjac) from MaticnaPloca where Soket = 'AM4'", con);

            else if (cbSoket.SelectedValue.ToString() == "TR4")
                sc = new SqlCommand("select distinct (proizvodjac) from MaticnaPloca where Soket = 'TR4'", con);

            SqlDataReader reader;
            reader = sc.ExecuteReader();
            DataTable dt = new DataTable();
            dt.Columns.Add("proizvodjac", typeof(string));
            dt.Load(reader);
            comboBox2.ValueMember = "proizvodjac";
            comboBox2.DataSource = dt;
            comboBox2.SelectedIndex = -1;
            comboBox2.Enabled = true;
            con.Close();
        }
        catch (Exception)
        {

        }

    }
                          

    private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
    {          
     

        try
        {

            SqlCommand sc = new SqlCommand();
            con.Open();

            if (comboBox2.SelectedValue.ToString() == "Asus" && cbSoket.SelectedValue.ToString() == "1151")
                sc = new SqlCommand("select distinct (model) from MaticnaPloca where Proizvodjac = 'Asus' AND Soket = '1151'", con);
            if (comboBox2.SelectedValue.ToString() == "Asus" && cbSoket.SelectedValue.ToString() == "AM4")
                sc = new SqlCommand("select distinct(model) from MaticnaPloca where Proizvodjac = 'Asus' AND Soket = 'AM4'", con);
            if (comboBox2.SelectedValue.ToString() == "Asus" && cbSoket.SelectedValue.ToString() == "TR4")
                sc = new SqlCommand("select distinct(model) from MaticnaPloca where Proizvodjac = 'Asus' AND Soket = 'TR4'", con);


            if (comboBox2.SelectedValue.ToString() == "Biostar" && cbSoket.SelectedValue.ToString() == "AM4")
                sc = new SqlCommand("select distinct(model) from MaticnaPloca where Proizvodjac = 'Biostar' AND Soket = 'AM4' ", con);
            if (comboBox2.SelectedValue.ToString() == "Biostar" && cbSoket.SelectedValue.ToString() == "1151")
                sc = new SqlCommand("select distinct(model) from MaticnaPloca where Proizvodjac = 'Biostar' AND Soket = '1151'", con);


            if (comboBox2.SelectedValue.ToString() == "Gigabyte" && cbSoket.SelectedValue.ToString() == "1151")
                sc = new SqlCommand("select distinct(model) from MaticnaPloca where Proizvodjac = 'Gigabyte' AND Soket = '1151'", con);
            if (comboBox2.SelectedValue.ToString() == "Gigabyte" && cbSoket.SelectedValue.ToString() == "AM4")
                sc = new SqlCommand("select distinct(model) from MaticnaPloca where Proizvodjac = 'Gigabyte' AND Soket = 'AM4'", con);
            if (comboBox2.SelectedValue.ToString() == "Gigabyte" && cbSoket.SelectedValue.ToString() == "TR4")
                sc = new SqlCommand("select distinct(model) from MaticnaPloca where Proizvodjac = 'Gigabyte' AND Soket = 'TR4'", con);


            if (comboBox2.SelectedValue.ToString() == "MSI" && cbSoket.SelectedValue.ToString() == "AM4")
                sc = new SqlCommand("select distinct(model) from MaticnaPloca where Proizvodjac = 'MSI' AND Soket = 'AM4'", con);
            if (comboBox2.SelectedValue.ToString() == "MSI" && cbSoket.SelectedValue.ToString() == "TR4")
                sc = new SqlCommand("select distinct(model) from MaticnaPloca where Proizvodjac = 'MSI' AND Soket = 'TR4'", con);


            SqlDataReader reader;
            reader = sc.ExecuteReader();
            DataTable dt2 = new DataTable();
            dt2.Columns.Add("model", typeof(string));
            dt2.Load(reader);
            comboBox3.ValueMember = "model";
            comboBox3.DataSource = dt2;
            comboBox3.Enabled = true;
            con.Close();
        }
        catch (Exception)
        {

        }

我也不太明白你想做什么,但我嘗試。

將文本框文本添加到組合框

comboBox1.Items.Add(textBox1.Text);

獲取組合框值,並在文本框中顯示

int pos = 0; //item position starting from 0 1 2 3 . . . 
textBox1.Text = comboBox1.Items[pos].ToString();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM