0

I am new to C# and I am trying to create a drop-down box with a label and value property. For example in HTML I can have <option value"user_name"> Username </option> , how do I get this done in WinForms using C#.

I understand how to create a combo box and add string collections to it using the WinForm designer, but how do I assign value to this strings? The value is quite important, as this is what is needed to interact with a different server.

Thanks.

Focus Ifeanyi
  • 57
  • 1
  • 5
  • The following thread might be useful for you: https://stackoverflow.com/questions/3063320/combobox-adding-text-and-value-to-an-item-no-binding-source – ivayle Jul 11 '17 at 04:56
  • 2
    Possible duplicate of [ComboBox: Adding Text and Value to an Item (no Binding Source)](https://stackoverflow.com/questions/3063320/combobox-adding-text-and-value-to-an-item-no-binding-source) – Peter Jul 11 '17 at 04:57
  • Here is article about it http://net-informations.com/q/faq/combovalue.html, the main idea is to bind a dictionary to combobox – Samvel Petrosov Jul 11 '17 at 04:57
  • I can think of a few ways to assign values to items in the list...which I would choose depends entirely upon why I would be doing it. Directly giving the items a value, as your example suggests, is usually my least favorite choice. – DonBoitnott Jul 11 '17 at 15:36

1 Answers1

0

Break up your problem

1) You said - "I understand how to create a combo box and add string collections to it using the WinForm designer"

2) You said - "but how do I assign value to this strings?"

Option 1, is not at all related to Option 2.

Option 1 - You are adding data to the combobox in design mode.

Option 2 - Which string you are talking about? If you have already assigned the data(collection of strings) to combobox then combobox will display collection of strings only. M i right?

You don't need Option 1, if you are implementing Option 2.

You need data to be displayed in your combobox which your user will select.

So, now where the data coming up? It is from Sql Server/XML/Text File/...? Or you are creating a List/Dictionary in your code and assigning to the combobox?

So finally, you can use any one approach...either Option 1 - "add data to your combobox in design mode" Or Option 2 - "add data to your combobox by retrieving from a datasource(Sql Server/XML/Text File/List/Dictionary)".

How to do that, refer the links mentioned in your comments.

Both the case/s, data will be available inside combobox and user can interact with the data inside combobox.

Chandan Kumar
  • 4,570
  • 4
  • 42
  • 62