C # Windows - How to get Enum value from ComboBox

I am associating a dictionary with a ComboBox that stores enum values. To get the selected value that I used

comboBox1.SelectedItem that returns size values โ€‹โ€‹says [0, Permanent] . Just i, which extracts "Permanent" and converts it back to an enumeration.


something like ..
Employee.JobType = Enum.Parse (JobType, comboBox1.SelectedItem)

how can i achieve this

+3
source share
6 answers

Or:

Employee.JobType = (JobType)Enum.Parse(typeof(JobType), comboBox1.SelectedValue);

Or:

Employee.JobType = (JobType)Enum.Parse(typeof(JobType), comboBox1.SelectedText);
+15
source

, SelectedItem : KeyValuePair < [ ], JobType >

, SelectedItem Value.

var selectedItem = (KeyValuePair<[type of key], JobType>) comboBox1.SelectedItem;
var jobType = selectedItem.Value;
+1

- http://www.fmsinc.com/free/NewTips/NET/NETtip4.asp

PeopleNames people = (PeopleNames)Enum.Parse(ComboBox1.SelectedValue, PeopleNames)

:

ComboBox1.DataSource = System.Enum.GetValues(typeof(PeopleNames))
+1

, :

string[] parts = comboBox1.SelectedItem.Split(
                      new char[] { ',', '[', ']' }, 
                      StringSplitOptions.RemoveEmptyEntries);
Employee.JobType = (JobType)Enum.Parse(typeof(JobType), parts[1].Trim()));

. , . enum.

, Type Parse, , Parse object.

0
Employee.JobType = (JobTypeEnum)Enum.Parse(typeof(JobTypeEnum), comboBox1.SelectedValue);
0

- (WPF), .

, ,

 KeyValuePair<string,string> selectedPair =  (KeyValuePair<string,string>)(cmbApplications.SelectedItem);
 ProTraceLicence.Products chosenProduct = (ProTraceLicence.Products)Enum.Parse(typeof(ProTraceLicence.Products), selectedPair.Key);

Hope this helps someone. I can't believe it is so hard

0
source

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


All Articles