7

我在这里的组合框有问题。我所做的是制作了一个组合框,使用comboBox1.Items.Add("Something");. 现在我在那里做了一个文本框,我需要的是当我从组合框中选择一些东西时,文本框会根据组合框中的选定项目而变化。我认为它会做的是

if(comboBox1.SelectedItem.ToString() == "Something")
{
    textBox1.Text = "Something";
}

但由于某种原因它不起作用,我尝试了没有ToString()和仍然没有工作。

4

9 回答 9

3

尝试使用: comboBox1.SelectedText

if(comboBox1.SelectedText == "Something")
{
      textBox1.Text = "Something";
}
于 2012-06-23T08:01:57.030 回答
1

双击您的组合框,它将为您生成事件(SelectedIndexChanged默认情况下)。将您的代码放入生成的事件中。当您更改组合框选定值时,您可以看到文本框值相应更改。

如果您需要在文本框中显示组合框选定的值,您可以将下面的代码放入生成的事件中

textBox1.Text = comboBox1.SelectedItem.ToString();
于 2012-06-23T08:12:46.443 回答
1

哦发现问题了。我将代码放在错误的部分(在 textBOX_click 上)部分:P

于 2012-06-23T09:56:17.353 回答
0

将您的代码放入comboBox1_SelectedIndexChanged事件中

if(comboBox1.SelectedItem.Value.ToString() == "Something")
{
    textBox1.Text = "Something";
}
else
{
    textBox1.Text = "";
}

或者一个简洁的版本

textBox1.Text = 
comboBox1.SelectedItem.Value.ToString() == "Something" ? "Something" :"";
于 2012-06-23T08:03:00.830 回答
0

试试这个

 private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (comboBox1.SelectedItem.ToString() == "Something")
            {
                textBox1.Text = "Something";
            }
        }
于 2012-06-23T08:07:23.557 回答
0

根据您的问题,我想无论selectedItem您想在TextBox. 那么,你为什么不使用这个:

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    textBox1.Text = comboBox1.SelectedText.ToString();
}
于 2012-06-23T10:07:56.727 回答
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");



    }
    }
}
于 2013-12-26T12:44:32.030 回答
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");
    }
}
于 2016-01-06T09:54:29.223 回答
0

试试这个,在你的 InitializeComponent() 添加这个

private void InitializeComponent()
{
//
// combobox
//
this.combobox.SelectedIndexChanged += new System.EventHandler(this.changed);
}

然后在你的主要方法中,创建这个方法

private void changed(object sender, EventArgs e)
{
if(this.combobox.Text == "Something")
Textbox1.Text = "Something";
}
于 2016-08-15T17:28:52.597 回答