如何解决这个ComboBox项目设置问题? [英] How to solve this ComboBox item setting problem?

查看:115
本文介绍了如何解决这个ComboBox项目设置问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此代码无法为组合框设置正确的值。这是因为,这些是来自不同的上下文。

This code is not setting right value to the combobox. This is because, those are from different contexts.

此外,

In addition, in my actual problem, neither overloading == in nor overriding Equals() in MyClass is working as well.

任何替代方法?

如何解决这个问题?

public class MyClass
    {
        public int ID { get; set; }
        public string Name { get; set; }
        public MyClass(int id, string name)
        {
            ID = id;
            Name = name;
        }

        public static List<MyClass> Get()
        {
            return new List<MyClass>
            {
                new MyClass(2, "AAA"),
                new MyClass(4, "BBB"),
                new MyClass(5, "CCC"),
                new MyClass(7, "DDD"),
                new MyClass(10, "EEE")
            };
        }
    }



ComboBoxForm.cs



ComboBoxForm.cs

public partial class ComboBoxForm : Form
    {
        public ComboBoxForm()
        {
            InitializeComponent();
        }

        private void Form1_Shown(object sender, EventArgs e)
        {
            LoadDataToComboBoxFromDatabase();
        }

        private void LoadDataToComboBoxFromDatabase()
        {
            comboBox1.Items.Clear();

            List<MyClass> list = MyClass.Get();

            foreach(MyClass mc in list)
            {
                comboBox1.Items.Add(mc);
            }

            comboBox1.DisplayMember = "Name";
            comboBox1.ValueMember = "ID";

            comboBox1.SelectedIndex = 0;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            int ID = 5;//This is what I have.
            comboBox1.SelectedItem = .............how?
        }
    }


推荐答案

I相信你的Equals比较可能不正确。对于NHibernate,我使用以下 Equals()方法:

I believe your Equals comparison may be incorrect. For NHibernate, I use the following Equals() method:

public static class EntityUtil
{
    public static bool Equals(IEntity a, IEntity b)
    {
        // Transient entitys are never equal (unless they are ReferenceEquals)!

        return
            ReferenceEquals(a, b) || (
                !((object)a == null || (object)b == null) &&
                !(IsTransient(a) || IsTransient(b)) &&
                NHibernateProxyHelper.GetClassWithoutInitializingProxy(a) == NHibernateProxyHelper.GetClassWithoutInitializingProxy(b) &&
                a.Id.Equals(b.Id)
            );
    }

    public static bool IsTransient(IEntity entity)
    {
        return entity.Id == 0;
    }
}



我已经在一个单独的帮助类中定义了这个方法,在我的实体中使用:

I have defined this method in a separate helper class and use this in my entity:

public class Entity : IEquatable<Entity>, IEquatable<IEntity>
{
    private int? _hashCode;

    public virtual int Id { get; protected set; }

    public override bool Equals(object obj)
    {
        return EntityUtil.Equals((IEntity)this, obj as IEntity);
    }

    public virtual bool Equals(Entity obj)
    {
        return EntityUtil.Equals((IEntity)this, (IEntity)obj);
    }

    public virtual bool Equals(IEntity obj)
    {
        return EntityUtil.Equals((IEntity)this, obj);
    }

    public static bool operator ==(Entity a, Entity b)
    {
        return EntityUtil.Equals((IEntity)a, (IEntity)b);
    }

    public static bool operator !=(Entity a, Entity b)
    {
        return !(a == b);
    }

    public override int GetHashCode()
    {
        // GetHashCode needs to return a stable value.

        if (!_hashCode.HasValue)
        {
            _hashCode =
                NHibernateProxyHelper.GetClassWithoutInitializingProxy(this).GetHashCode() ^
                ((IEntity)this).Id.GetHashCode();
        }

        return _hashCode.Value;
    }
}

这是与NHibernate 。也许这也有助于你。

This is the suggested implementation for working with equality with NHibernate. Maybe this helps for you too.

这篇关于如何解决这个ComboBox项目设置问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆