动态更改 Winforms ComboBox 中项目的文本 [英] Dynamically changing the text of items in a Winforms ComboBox

查看:33
本文介绍了动态更改 Winforms ComboBox 中项目的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含自定义类实例的 Winforms ComboBox.当项目第一次被添加到 ComboBox 的 Items 集合时,ToString 方法会被调用.

I have a Winforms ComboBox that contains instances of a custom class. When the items are first added to the Items collection of the ComboBox, the ToString method is call on each of them.

然而,当用户更改应用程序运行的语言时,ToString 方法的结果会发生变化.

However when the user changes the language the application is running in, the result of the ToString method changes.

因此,如何让 ComboBox 再次对所有项目调用 ToString 方法,而不必从 ComboBox 中删除所有项目和重新添加它们?

Therefore how can I get the ComboBox to call the ToString method on all items again without having to remove all items from the ComboBox and adding them back in?

推荐答案

感谢 svick,RefreshItems() 可以工作,但是由于它是受保护的(因此只能由子类调用)我不得不这样做

Thanks svick, RefreshItems() work, however as it is protected (so can only be called by a subclass) I had to do

public class RefreshingComboBox : ComboBox
{
    public new void RefreshItem(int index)
    {
        base.RefreshItem(index);
    }

    public new void RefreshItems()
    {
        base.RefreshItems();
    }
}

我只需要为 ToolStripComboBox 做同样的事情,但是它有点难,因为你不能对它包含的 Combro 框进行子类化,我做到了

I have just had to do the same for a ToolStripComboBox, however it was a bit harder as you can not subclass the Combro box it contains, I did

public class RefreshingToolStripComboBox : ToolStripComboBox
{
    // We do not want "fake" selectedIndex change events etc, subclass that overide the OnIndexChanged etc
    // will have to check InOnCultureChanged them selfs
    private bool inRefresh = false;
    public bool InRefresh { get { return inRefresh; } }

    public void Refresh()
    {
        try
        {
            inRefresh = true;

            // This is harder then it shold be, as I can't get to the Refesh method that
            // is on the embebed combro box.
            //
            // I am trying to get ToString recalled on all the items
            int selectedIndex = SelectedIndex;
            object[] items = new object[Items.Count];
            Items.CopyTo(items, 0);

            Items.Clear();

            Items.AddRange(items);
            SelectedIndex = selectedIndex;
        }
        finally
        {
            inRefresh = false;
        }
    }

    protected override void OnSelectedIndexChanged(EventArgs e)
    {
        if (!inRefresh)
        {
            base.OnSelectedIndexChanged(e);
        }
    }
}

我必须通过覆盖 OnSelectedValueChanged、OnSelectedItemChanged 和 OnSelectedIndexChanged 来停止普通 CombroBox 不需要的事件,因为代码与 ToolStripComboBox 的代码相同,我没有将其包含在此处.

I had to do the same trip to stop unwanted events for the normal CombroBox, by overriding OnSelectedValueChanged, OnSelectedItemChanged and OnSelectedIndexChanged, as the code is the same as for the ToolStripComboBox I have not included it here.

这篇关于动态更改 Winforms ComboBox 中项目的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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