Reputation: 105
I am having a problem with a combo box here. What I did is made a combo box, added items using comboBox1.Items.Add("Something");
. Now I made a text box down there and what I need is when I select something from the combo box the text box changes according to selected item on combo box. What I thought it would do is
if(comboBox1.SelectedItem.ToString() == "Something")
{
textBox1.Text = "Something";
}
But it's not working for some reason, I tried both without ToString()
and with still it is not working.
Upvotes: 7
Views: 77434
Reputation: 1
Option 1:
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (comboBox2.SelectedItem.ToString() == "Dr")
{
MessageBox.Show("its dr");
}
else
{
MessageBox.Show("its cr");
}
}
Option 2:
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (comboBox2.Text == "Dr")
{
MessageBox.Show("its dr");
}
else
{
MessageBox.Show("its cr");
}
}
Upvotes: 0
Reputation: 353
Try this, in your InitializeComponent() add this
private void InitializeComponent()
{
//
// combobox
//
this.combobox.SelectedIndexChanged += new System.EventHandler(this.changed);
}
then in your main method, create this method
private void changed(object sender, EventArgs e)
{
if(this.combobox.Text == "Something")
Textbox1.Text = "Something";
}
Upvotes: 0
Reputation: 1
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (comboBox1.SelectedItem.ToString() == "M.Naveed")
{
textBox1.Text = textBox1.Text + ("Networking");
textBox2.Text = textBox2.Text + ("Networking");
textBox1.Text = textBox1.Text + ("mobile");
textBox2.Text = textBox2.Text + ("mobile");
ListViewItem li = new ListViewItem("Networking");
li.SubItems.Add("mobile");
listView1.Items.Add(li);
}
else if (comboBox1.SelectedItem.ToString() == "Zeeshan")
{
textBox1.Text = textBox1.Text + ("Networking");
textBox2.Text = textBox2.Text + ("Networking");
textBox1.Text = textBox1.Text + ("Jave");
textBox2.Text = textBox2.Text + ("Jave");
ListViewItem li = new ListViewItem("Networking");
li.SubItems.Add("Jave");
listView1.Items.Add(li);
}
else if (comboBox1.SelectedItem.ToString() == "Shamsher")
{
textBox1.Text = textBox1.Text + ("Networking"); textBox1.Text = " ";
textBox2.Text = textBox2.Text + ("Networking");
textBox1.Text = textBox1.Text + ("Web");
textBox2.Text = textBox2.Text + ("Web");
ListViewItem li = new ListViewItem("Networking");
li.SubItems.Add("Web");
listView1.Items.Add(li);
}
else if (comboBox1.SelectedItem.ToString() == "Mudasir")
{
textBox1.Text = textBox1.Text + ("Networking");
textBox2.Text = textBox2.Text + ("Networking");
textBox1.Text = textBox1.Text + ("Team Fundation");
textBox2.Text = textBox2.Text + ("Team Fundation");
ListViewItem li = new ListViewItem("Networking");
li.SubItems.Add("Team Funadation");
listView1.Items.Add(li);
}
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
comboBox1.Items.Add("Someting");
comboBox1.Items.Add("Mcs");
}
private void button2_Click(object sender, EventArgs e)
{
}
private void Form1_Load(object sender, EventArgs e)
{
comboBox1.Items.Add("M.Naveed");
comboBox1.Items.Add ("Mudasir");
comboBox1.Items.Add ("Zeeshan");
comboBox1.Items.Add("Shamsher");
}
}
}
Upvotes: 0
Reputation: 9783
According to your question I suppose that whatever the selectedItem
is you want to display it on the TextBox
. So, why don't you use this:
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
textBox1.Text = comboBox1.SelectedText.ToString();
}
Upvotes: 0
Reputation: 48558
Put your code in comboBox1_SelectedIndexChanged
event
if(comboBox1.SelectedItem.Value.ToString() == "Something")
{
textBox1.Text = "Something";
}
else
{
textBox1.Text = "";
}
OR a consice version
textBox1.Text =
comboBox1.SelectedItem.Value.ToString() == "Something" ? "Something" :"";
Upvotes: 0
Reputation: 105
Oh found the problem . I was putting the code in wrong section (on textBOx_click) section :P
Upvotes: 1
Reputation: 63065
Double click on your combobox and it will generate event for you(SelectedIndexChanged
by default). put your code inside that generated event. When you change combobox selected value then you can see the text box value change accordingly.
if you need to show combobox selected value in the textbox, you can put below code inside generated event
textBox1.Text = comboBox1.SelectedItem.ToString();
Upvotes: 1
Reputation: 10143
try this
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (comboBox1.SelectedItem.ToString() == "Something")
{
textBox1.Text = "Something";
}
}
Upvotes: 0
Reputation: 70718
Try using:
comboBox1.SelectedText
if(comboBox1.SelectedText == "Something")
{
textBox1.Text = "Something";
}
Upvotes: 3