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

1

The idea is, there is a Main ComboBox, each with a client of 4 different cities, I wanted to select a client in the ComboBox, push the button and remove from that ComboBox and insert in the corresponding city of it.

In short: I want to take an item, remove it and put it in the pre-selected ComboBox.

I tried using IF, since each ComboBox starts with 0, but when adding in the ComboBox, it returns a value that contains something inside it, but does not show any 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);
}
    
asked by anonymous 25.03.2018 / 06:23

1 answer

4

As far as I understand, you want to select an item in ComboBox , push a button and move it to another ComboBox .

I've set up a scheme here ... I hope it does.

Picture:

CodeUsed:

privatevoidForm2_Load(objectsender,EventArgse){for(inti=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;
            }
        }

You can also use the SelectedIndexChanged event of ComboBox and send it to Second ComboBox at the time of item selection in the first

    
25.03.2018 / 08:47