How to transfer from one Combobox to another (C#)

Asked

Viewed 145 times

1

The idea is, there is a Main Combobox, each with a customer from 4 different cities, I wanted to select a customer on Combobox, press the button and remove from this Combobox and insert in the corresponding city of it.

Summarizing: I want to take an item, remove it and put in the selected Combobox.

I tried using IF, since each Combobox starts with 0, but when adding to Combobox, it returns a value that contains something inside, but shows no text. (Example of what I tried)

if (cboClientes.SelectedIndex == 0 || cboClientes.SelectedIndex == 2 || cboClientes.SelectedIndex == 3 || 
{
    cboVarginha.Items.Add(cboClientes.SelectedIndex);
    cboClientes.Items.RemoveAt(cboClientes.SelectedIndex);
}

1 answer

4


From what I understand, you want to select an item on ComboBox, push one button and move it to another ComboBox.

I set up a scheme here... I hope it fits.

Imagery: inserir a descrição da imagem aqui

Code Used:

        private void Form2_Load(object sender, EventArgs e)
        {
            for (int i = 1; i < 5; i++)
                comboBox1.Items.Add($"Item {i}");
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (!comboBox1.SelectedIndex.Equals(-1))
            {
                comboBox2.Items.Add(comboBox1.SelectedItem);
                comboBox2.Sorted = true;

                Resultado.Text = $"O Elemento {(string)comboBox1.SelectedItem} foi transferido para o Destino";
                Resultado.Left = (Resultado.Parent.Width / 2) - (Resultado.Width / 2);

                comboBox1.Items.RemoveAt(comboBox1.SelectedIndex);                
                Resultado.Visible = true;
            }
        }

There is also the possibility to use the event SelectedIndexChanged of ComboBox and send it straight to Monday ComboBox at the time of selecting the Item in First.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.