动态更改Winforms ComboBox中的项目文本

Ian*_*ose 5 .net c# combobox tostring winforms

我有一个Winforms ComboBox包含自定义类的实例.当项目首次添加到其中的Items集合时ComboBox,该ToString方法将调用每个项目.

但是,当用户更改运行应用程序的语言时,ToString方法的结果会更改.

因此,我怎样才能再次ComboBox调用ToString所有项目的方法,而无需从中删除所有项目ComboBox并将其添加回来?

Ian*_*ose 6

谢谢svick,RefreshItems()工作,但因为它受到保护(所以只能由子类调用)我不得不做

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

    public new void RefreshItems()
    {
        base.RefreshItems();
    }
}
Run Code Online (Sandbox Code Playgroud)

我只需要为ToolStripComboBox做同样的事情,但是因为你不能将它包含的Combro盒子类化,所以它有点难,我做了

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);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

通过重写OnSelectedValueChanged,OnSelectedItemChanged和OnSelectedIndexChanged,我不得不做同样的行程以阻止普通CombroBox的不需要的事件,因为代码与ToolStripComboBox相同,我没有在这里包含它.