开发者

Loading certain data in combobox based on textbox input

开发者 https://www.devze.com 2023-04-04 09:52 出处:网络
Based on gender, I want to load certain criteria. For example, if I type 0 in a textbox, I want to load Mr., Dr. , etc..

Based on gender, I want to load certain criteria. For example, if I type 0 in a textbox, I want to load Mr., Dr. , etc.. If I type 1, I want to 开发者_运维问答load Ms, Mrs, Miss, Dr. etc... How can I do so? Gender is typed in a textbox, and I want the combo box to load what I specified above. Thank you.


This is just Sudo code there must be typo or Syntax error but you need to do something as below :

    List<string> strMale = new List<string>{"Mr.", "Dr. "};
    List<string> strFMale = new List<string>{"Mrs.", "Miss"};

//make use of Textbox Change Event
public void Text1_TextChanged(object sender, EventArgs e)

{

  Combo1.Items.Clear();

  //Bind the values using the text box input value 
  if(Text1.Text=="0")
   {
     Combo1.DataSource = strMale ;
   }
   else if(Text1.Text=="1")
   {
     Combo1.DataSource = strFMale ;
   }
  Combo1.SelectedIndex = 0;
}


You have to handle event ValueChanged (the name of the event depends on platform you're working with) and depending on value typed change source of your combobox


Try the following Code Use can use comboBox.Items.Insert or comboBox.Items.Add for inserting new items into a combobox.

private void textBox2_TextChanged(object sender, EventArgs e)
        {
            if (textBox2.Text == "0")
            {
                if (comboBox1.Items.Count > 0)
                      comboBox1.Items.Clear();
                comboBox1.Items.Insert(0,"Mr");
                comboBox1.Items.Insert(1, "Dr");
            }
            else if (textBox2.Text == "1")
            {
                if (comboBox1.Items.Count > 0)
                      comboBox1.Items.Clear();
                comboBox1.Items.Add("Ms");
                comboBox1.Items.Add("Mrs");
                comboBox1.Items.Add("Miss");
            }
        }
0

精彩评论

暂无评论...
验证码 换一张
取 消