C#如何使用comboBox中的值替换EventLogEntryType?

时间:2009-02-27 11:16:32

标签: c#

在下面的代码中,我想用从组合框中选择的值替换EventLogEntryType.Warning,组合框值是EventLogEntryType.Warning,EventLogEntryType.Information,EventLogEntryType.Error。组合框只显示“警告”,“信息”和“错误”。我怎么能这样做?

由于

EventLog myLog = new EventLog(); myLog.Source = "Test";             
int intEventID = int.Parse(txtEventID.Text);            
myLog.WriteEntry(txtDescription.Text, EventLogEntryType.Warning, intEventID);

新手

2 个答案:

答案 0 :(得分:1)

假设EventLogEntryType是一个枚举而且你不需要本地化,你可以很容易地做到这一点。

在Form_Load

  combobox1.Items.Add(EventLogEntryType.Warning);
   combobox1.Items.Add(EventLogEntryType.Information);
   ...

以后

   myLog.WriteEntry(txtDescription.Text, (EventLogEntryType)combobox1.Selecteditem, intEventID);

答案 1 :(得分:1)

您可以使用Enum.Parse将组合框中的值转换为枚举值:

var sv = yourComboBox.SelectedValue;
var entryType = (EventLogEntryType) Enum.Parse(typeof(EventLogEntryType), sv);