简体   繁体   中英

C# Updating Selected ComboBox Item Name from Textbox

I want to update comboBox selectedItem name by changing textbox name. Without losing combobox value, How can I achieve it?

private void addItem_Click(object sender, EventArgs e)
{
    nameItem.Enabled = true;
    nameItem.Text = "Item " + counter.ToString();
    nameItem.Focus();
    comboBox1.Items.Add(nameItem.Text);
    comboBox1.SelectedItem = nameItem.Text;
    counter++;

}

private void nameItem_TextChanged(object sender, EventArgs e)
{
    ????????
}

This one is simple and it's working, but it maybe a little bit long.

Here I got a combo box, textBox1 and button for adding value to combo box, and textBox2 for editing selected item.

    string[] items = new string[99];
    int a = 0;
    int i = 0;
    private void button1_Click(object sender, EventArgs e)
    {
        items[i] = textBox1.Text;
        i++;
        comboBox1.Items.Clear();
        for (int n = 0; n < items.Length; n++) {
            if(items[n] != null) comboBox1.Items.Add(items[n]);
        }
    }

    private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        a = comboBox1.SelectedIndex;
        MessageBox.Show(a.ToString());
    }

    private void textBox2_TextChanged(object sender, EventArgs e)
    {
        items[a] = textBox2.Text;
        comboBox1.Items.Clear();
        for (int n = 0; n < items.Length; n++)
        {
            if (items[n] != null) comboBox1.Items.Add(items[n]);
        }
    }

How it works: we have an array for items and two integar variables. one for count of added items and other for selected Items index Button 1 just addes new item, clears all items and update em again when u edit text of textbox2, It will update items from 'items' array, and then update the combo box, ez

EDIT: Did not see winform tag - this will not work - will leave in case any ASP people come across.

 public void TextBox1_OnTextChanged(object sender, EventArgs e)
        {
            ddl.DataSource = null;
            ddl.DataBind();
            ddl.DataTextField = "Text";
            ddl.DataValueField = "Value";
            ddl.DataSource = (from ListItem b in ddl.Items
                             select b.Selected ? new ListItem(TextBox1.Text, b.Value) : b).ToList();
            ddl.DataBind();
        }

ddl is name of the dropdown box Textbox1 is the name of the textbox.

This will change the name of the selected item. If you need more code let me know in comments.

Thank you for your time and kind answers. I solved the problem as it looks below;

 private void addItem_Click(object sender, EventArgs e)
    {
        nameItem.Enabled = true;
        comboBox1.Items.Add("Item " + counter.ToString());
        comboBox1.SelectedItem = "Item " + counter.ToString();
        nameMacro.Text = "Item " + counter.ToString();
        //comboBox1.SelectedItem = nameItem.Text;
        //nameItem.Focus();
        // Ad degistirme -> comboBox1.Items[comboBox1.FindStringExact("string value")] = "New Value";
        counter++;
    }

    private void textBox1_TextChanged(object sender, EventArgs e)
    {
        comboBox1.Items[comboBox1.SelectedIndex] = nameItem.Text;
    }

it's better to update the source then bind it again. try this also

private void nameItem_TextChanged(object sender, EventArgs e)
{
  string value = nameItem.Text;
  var list = (List<KeyValuePair<String, String>>)comboBox1.DataSource;
  list.Add(new KeyValuePair<string, string>(value,value));
  comboBox1.DataSource = list;
  comboBox1.DataBind();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM