0

I have a little problem with my code in Visual Basic. It is quite simple but I'm very new to this language.

In Visual Basic, I want to add new items in my combo box like this:

ComboBox1.Items.Add(array(1))

When the program executes the code, I suppose that I make a new ComboBox item in a specific position with the attribute .Text, but I want to access and add some data in the attribute .Value at the same time. The code is inside a loop.

My Code:

While src.Peek() <> -1
    cadena = src.ReadLine()
    intermitja = Split(cadena, "/")
    ComboBox1.Items.Add(intermitja(1))
    ' I tried this...
    ' ComboBox1.Items(ComboBox1.Items.Count).value = (intermitja(0))
End While
djv
  • 15,168
  • 7
  • 48
  • 72
fr0g88
  • 1
  • 1
  • 2
  • 3
    Your question is not very clear, but it sounds like you need to look at DataBinding and use `ValueMember` and `DisplayMember` to show one thing but return another to your code. There are gobs of examples here. Please read [ask] and take the [tour] – Ňɏssa Pøngjǣrdenlarp Jan 09 '18 at 18:20
  • It seems to me from `Split()` you want a `ComboBox` with multiple columns. Normally only one item per item per line is possible. – help-info.de Jan 09 '18 at 19:13
  • Possible duplicate of [WinForms combobox with multiple columns (C#)?](https://stackoverflow.com/questions/1091414/winforms-combobox-with-multiple-columns-c) – help-info.de Jan 09 '18 at 19:13
  • Agreed with the question not being clear: you don't make a combobox item by handling the `Text` Property. – Jason Bayldon Jan 09 '18 at 19:18

1 Answers1

0

Thanks to everyone and my teacher I have already been able to do what I wanted @plutonix told me the answer despite my unclear question, thank you very much.

I needed something like this:

    ComboBox1.DisplayMember = "Text"
    ComboBox1.ValueMember = "Value"
    Dim tb As New DataTable
    tb.Columns.Add("Text", GetType(String))
    tb.Columns.Add("Value", GetType(Integer))
    tb.Rows.Add("USA", 101)
    tb.Rows.Add("UK", 109)
    tb.Rows.Add("France", 106)
    tb.Rows.Add("Germany", 121)
    ComboBox1.DataSource = tb
fr0g88
  • 1
  • 1
  • 2