How to manipulate Combobox

Asked

Viewed 364 times

-1

Hello personal I have 2 combobox and I have 1 (one) file with texts and subtexts

text 1

<001>ola

<002>Hi

text2

<001>house

<002>hospital

....

I want to load this information into the 2 combobox

Combobox1 = Text 1

Combobox2 = subtexts

Ex1:

Combobox1 = Text 1

Combobox2 = <001>Ola

ex2:

Combobox1 = Text 2

Combobox2 = <001>house

I hope you understand and help me please

1 answer

1

You can create a list and assign it to the combobox.

public sealed class ComboBoxItem
{
    public string Texto { get; set; }
    public string Valor { get; set; }

    public override string ToString()
    {
        return this.Texto;
    }
 }

Use:

   var itens = new List<ComboBoxItem>();
   itens.Add(new ComboBoxItem { Valor = "1", Texto = "Ola" });
   itens.Add(new ComboBoxItem { Valor = "2", Texto = "Oi" });

   cbo.DataSource = itens;
   cbo.DisplayMember = "Texto";
   cbo.ValueMember = "Valor";

Redeem selected value:

var item = (ComboBoxItem)cbo.SelectedItem;   

Browser other questions tagged

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