C # How to add value to comboBox text?

how do you add an integer value for an existing combo box? Example: in the combo box "Access", "Create" there are already 5 text values. But how to add an integer value to this text? Example "Access" = 1, "Create" = 2?

Please inform the codes! Thanks!

Codes:

comboBoxFilter.Items.Add("Access"); comboBoxFilter.Items.Add("Create"); comboBoxFilter.Items.Add("Delete"); comboBoxFilter.Items.Add("Modify"); comboBoxFilter.Items.Add("All"); 
+4
source share
3 answers

If you know the numbers in front of you, you can do something like this:

 comboBoxFilter.Items.Add("Access = 1"); 

If you do not, you may have something like this:

 comboBoxFilter.Items.Add("Access = " + numbers.ToString()); 

The above should give you something like this in the combo box: Access = <someNumber>

If you strictly want "Access" = to do something like:

 comboBoxFilter.Items.Add("\"Access\" = 1"); 

or

 comboBoxFilter.Items.Add("\"Access\" = " + numbers.ToString()); 
0
source

I suggest you create a class:

 public class Permission { public Int32 Index { get; set; } public String Value { get; set; } } 

And fill in the ComboBox as follows:

 List<Permission> permissions = new List<Permission>() { new Permission(){ Index = 1, Value ="Access" }, new Permission(){ Index = 2, Value ="Create" }, new Permission(){ Index = 3, Value ="Delete" }, new Permission(){ Index = 4, Value ="Modify" }, new Permission(){ Index = 5, Value ="All" }, }; comboBoxFilter.DisplayMember = "Value"; comboBoxFilter.DataSource = permissions; 

Using the code above, you can access the integer value using the following code:

 (comboBoxFilter.SelectedItem as Permission).Index 
+4
source

You can use the ListItem approach, where you will need to use

 comboBoxFilter.Items.Add(new ListItem("Access", "1")); comboBoxFilter.Items.Add(new ListItem("Create", "2")); comboBoxFilter.Items.Add(new ListItem("Delete", "3")); comboBoxFilter.Items.Add(new ListItem("Modify", "4")); comboBoxFilter.Items.Add(new ListItem("All", "5")); 

Keep in mind that both the Value and Text sections in the ListItem are strings.

PS You did not indicate whether it was winforms or webforms, so I accepted the web. If it is Winforms, it does not apply.

+1
source

Source: https://habr.com/ru/post/1333269/


All Articles