我在这里遇到组合框问题。我做了一个组合框,使用comboBox1.Items.Add("Something");
添加了项目。现在我在那里制作了一个文本框,我需要的是当我从组合框中选择一些文本框根据组合框中的选定项目更改时。我认为它会做什么
if(comboBox1.SelectedItem.ToString() == "Something")
{
textBox1.Text = "Something";
}
但由于某些原因它没有用,我在没有ToString()
的情况下尝试了两种情况,但仍然无法正常工作。
答案 0 :(得分:3)
尝试使用:
comboBox1.SelectedText
if(comboBox1.SelectedText == "Something")
{
textBox1.Text = "Something";
}
答案 1 :(得分:1)
将您的代码放入comboBox1_SelectedIndexChanged
事件
if(comboBox1.SelectedItem.Value.ToString() == "Something")
{
textBox1.Text = "Something";
}
else
{
textBox1.Text = "";
}
或简洁版
textBox1.Text =
comboBox1.SelectedItem.Value.ToString() == "Something" ? "Something" :"";
答案 2 :(得分:1)
双击你的组合框,它会为你生成一个事件(默认为SelectedIndexChanged
)。 将您的代码放入生成的事件中。更改组合框选定值后,您可以看到文本框值相应更改。
如果你需要在文本框中显示组合框选择的值,你可以将下面的代码放在生成的事件中
textBox1.Text = comboBox1.SelectedItem.ToString();
答案 3 :(得分:1)
答案 4 :(得分:0)
试试这个
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (comboBox1.SelectedItem.ToString() == "Something")
{
textBox1.Text = "Something";
}
}
答案 5 :(得分:0)
根据您的问题,我认为无论selectedItem
是什么,您都希望在TextBox
上显示它。所以,你为什么不用这个:
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
textBox1.Text = comboBox1.SelectedText.ToString();
}
答案 6 :(得分:0)
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");
}
}
}
答案 7 :(得分:0)
选项1:
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (comboBox2.SelectedItem.ToString() == "Dr")
{
MessageBox.Show("its dr");
}
else
{
MessageBox.Show("its cr");
}
}
选项2:
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (comboBox2.Text == "Dr")
{
MessageBox.Show("its dr");
}
else
{
MessageBox.Show("its cr");
}
}