简体   繁体   English

在组合框中选择枚举作为SelectedItem

[英]selecting enum as SelectedItem in combobox

I struggle with selecting proper item in winforms combobox. 我在选择winforms组合框中的适当项目时遇到了困难。 Previously I described in detail here but I think that problem remain unsolved cause I use one form for add/edit record. 以前我在这里详细描述过,但是我认为问题仍然没有解决,因为我使用一种形式进行添加/编辑记录。 so on form load I have 所以我有表格加载

  private void AddEditForm_Load(object sender, EventArgs e)
  {
      PopulateComboBoxLanguage();
  }

private void PopulateComboBoxLanguage()
{
    comboBoxLang.DataSource = Enum.GetValues(typeof(Book.EnumLang));
}

and on edit action I want to populate form with existing data and everything is populated as it should except combobox where first item from EnumLang is always displayed. 在执行编辑操作时,我想用现有数据填充表单,并按应有的方式填充所有内容,但组合框始终显示EnumLang的第一项。 from my second constructor I call PopulateWithExisingData(book) where I use 从第二个构造函数中,我在其中使用PopulateWithExisingData(book)

comboBoxLang.SelectedItem = book.Language;

but even when passed book.Language is set to German SelectedItem is always null on debug mode. 但是即使在通过book.Language设置为German SelectedItem情况下,在调试模式下也始终为null。

ps I tried with comboBoxLang.SelectedItem = (book.EnumLang)book.Language; ps我尝试了comboBoxLang.SelectedItem = (book.EnumLang)book.Language; also with SelectedValue but remains the same. 也与SelectedValue相同,但保持不变。

Once more I guess that problem is on populating combobox on page load but I don't know is it and how to fix that. 我再一次猜想问题出在页面加载时填充组合框,但我不知道是什么以及如何解决。

Please ask for more info. 请询问更多信息。

  1. Declare an instance of the object type you are adding/editing in your form. 声明要在表单中添加/编辑的对象类型的实例。
  2. Add a bool isEdit to the form and set it to false 将bool isEdit添加到表单并将其设置为false
  3. Add a method public void Initialize(ObjectType name) 添加方法public void Initialize(ObjectType name)
  4. Your Initialize method should set the form instance equal to the parameter and it should set a boolean flag isEdit = true. 您的Initialize方法应将表单实例设置为与参数相等,并且应设置一个布尔标志isEdit = true。
  5. Put all your code that loads data/populates controls (like your combobox) in your forms load event. 将所有加载数据/填充控件的代码(如组合框)放入表单加载事件中。
  6. At the bottom of your load event, after your controls are populated do 在加载事件的底部,填充控件后,执行

      if (isEdit) { //Set your controls selected values from the object you are editing } 

Now, for new objects, just make your form and call a Show or a ShowDialog on it. 现在,对于新对象,只需创建表单并在其上调用Show或ShowDialog。 This will cause the Load event to fire and your controls will populate. 这将引发Load事件,并填充您的控件。

For edits, make your form, call Initialize, THEN do the Show/ShowDialog. 要进行编辑,请创建表单,调用Initialize,然后执行Show / ShowDialog。 Since your Initialize method sets isEdit = true, the if(isEdit) block of code at the bottom of your load event will be hit and the controls values will be set equal to the properties of the object you are editing. 由于您的Initialize方法设置为isEdit = true,因此将加载事件底部的if(isEdit)代码块,并且控件值将设置为与正在编辑的对象的属性相等。

Here is some very simple example code: 这是一些非常简单的示例代码:

    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        //This is simulating an add...First Language will be displayed on form2, 
        //which is English
        Form2 form = new Form2();
        form.ShowDialog();
    }

    private void button2_Click(object sender, EventArgs e)
    {
        //This is simulating an edit...this will display french 
        //(or whatever is passed in)
        Form2 form = new Form2();
        form.Initialize(Languages.French);
        form.ShowDialog();
    }

    Languages editValue;
    bool isEdit = false;

    public Form2()
    {
        InitializeComponent();
    }

    public void Initialize(Languages var)
    {
        editValue = var;
        isEdit = true;
    }

    private void Form2_Load(object sender, EventArgs e)
    {
        comboBox1.DataSource = Enum.GetValues(typeof(Languages));

        if (isEdit)
        {
            comboBox1.SelectedItem = editValue;
        }
    }

public enum Languages
{
    English = 0,
    French = 1,
    Spanish = 2,
    German = 3
}

When you set DataSource, you pass an array of objects. 设置数据源时,将传递对象数组。 When you set SelectedItem, you pass an enum value, so it gets boxed to an object again. 设置SelectedItem时,将传递一个枚举值,因此它将再次装箱到对象。 ComboBox searches for your item among the DataSource values, using method IndexOf, which uses method Object.Equals to compare those values against your new value. ComboBox使用IndexOf方法在DataSource值中搜索您的项目,该方法使用Object.Equals方法将这些值与新值进行比较。 And since they are different objects (the references differ), your item is never found in the DataSource collection, so the selection is not changed. 并且由于它们是不同的对象(引用不同),因此在DataSource集合中永远找不到您的项目,因此选择不会更改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM