How do I make a ListBox refresh its item text?

前端 未结 10 1980
后悔当初
后悔当初 2020-12-03 04:42

I\'m making an example for someone who hasn\'t yet realized that controls like ListBox don\'t have to contain strings; he had been storing formatted strings and

相关标签:
10条回答
  • 2020-12-03 05:03

    If objLstTypes is your ListBox name Use objLstTypes.Items.Refresh(); Hope this works...

    0 讨论(0)
  • 2020-12-03 05:05

    If you derive from ListBox there is the RefreshItem protected method you can call. Just re-expose this method in your own type.

    public class ListBox2 : ListBox {
        public void RefreshItem2(int index) {
            RefreshItem(index);
        }
    }
    

    Then change your designer file to use your own type (in this case, ListBox2).

    0 讨论(0)
  • 2020-12-03 05:08

    It's little bit unprofessional, but it works. I just removed and added the item (also selected it again). The list was sorted according to "displayed and changed" property so, again, was fine for me. The side effect is that additional event (index changed) is raised.

    if (objLstTypes.SelectedItem != null)
    {
     PublisherTypeDescriptor objType = (PublisherTypeDescriptor)objLstTypes.SelectedItem;
     objLstTypes.Items.Remove(objType);
     objLstTypes.Items.Add(objType);
     objLstTypes.SelectedItem = objType;
    }
    
    0 讨论(0)
  • 2020-12-03 05:11
    typeof(ListBox).InvokeMember("RefreshItems", 
      BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.InvokeMethod,
      null, myListBox, new object[] { });
    
    0 讨论(0)
提交回复
热议问题