46

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 jumping through complicated parsing hoops to get the data back out of the ListBox and I'd like to show him there's a better way.

I noticed that if I have an object stored in the ListBox then update a value that affects ToString, the ListBox does not update itself. I've tried calling Refresh and Update on the control, but neither works. Here's the code of the example I'm using, it requires you to drag a listbox and a button onto the form:

Public Class Form1

    Protected Overrides Sub OnLoad(ByVal e As System.EventArgs)
        MyBase.OnLoad(e)

        For i As Integer = 1 To 3
            Dim tempInfo As New NumberInfo()
            tempInfo.Count = i
            tempInfo.Number = i * 100
            ListBox1.Items.Add(tempInfo)
        Next
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        For Each objItem As Object In ListBox1.Items
            Dim info As NumberInfo = DirectCast(objItem, NumberInfo)
            info.Count += 1
        Next
    End Sub
End Class

Public Class NumberInfo

    Public Count As Integer
    Public Number As Integer

    Public Overrides Function ToString() As String
        Return String.Format("{0}, {1}", Count, Number)
    End Function
End Class

I thought that perhaps the problem was using fields and tried implementing INotifyPropertyChanged, but this had no effect. (The reason I'm using fields is because it's an example and I don't feel like adding a few dozen lines that have nothing to do with the topic I'm demonstrating.)

Honestly I've never tried updating items in place like this before; in the past I've always been adding/removing items, not editing them. So I've never noticed that I don't know how to make this work.

So what am I missing?

13 Answers 13

35

I use this class when I need to have a list box that updates.

Update the object in the list and then call either of the included methods, depending on if you have the index available or not. If you are updating an object that is contained in the list, but you don't have the index, you will have to call RefreshItems and update all of the items.

public class RefreshingListBox : ListBox
{
    public new void RefreshItem(int index)
    {
        base.RefreshItem(index);
    }

    public new void RefreshItems()
    {
        base.RefreshItems();
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

Note, RefreshItem works only if the DisplayMember property is set.
If you want to brute force it you can do it via reflection with typeof(ListBox).InvokeMember("RefreshItems", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.InvokeMethod, null, listBox, new object[] { });
The same thing works for a ComboBox
30
lstBox.Items[lstBox.SelectedIndex] = lstBox.SelectedItem;

4 Comments

this is a valid answer in fact!
This one gets my vote! Simple, can be done without adding any new classes or paragraphs of code. This was all I needed.
Note, that this does not work, if you set the items via the DataSource property.
Stupid simple. Great answer.
25

BindingList handles updating the bindings by itself.

using System;
using System.ComponentModel;
using System.Windows.Forms;

namespace TestBindingList
{
    public class Employee
    {
        public string Name { get; set; }
        public int Id { get; set; }
    }

    public partial class Form1 : Form
    {
        private BindingList<Employee> _employees;

        private ListBox lstEmployees;
        private TextBox txtId;
        private TextBox txtName;
        private Button btnRemove;

        public Form1()
        {
            InitializeComponent();

            FlowLayoutPanel layout = new FlowLayoutPanel();
            layout.Dock = DockStyle.Fill;
            Controls.Add(layout);

            lstEmployees = new ListBox();
            layout.Controls.Add(lstEmployees);

            txtId = new TextBox();
            layout.Controls.Add(txtId);

            txtName = new TextBox();
            layout.Controls.Add(txtName);

            btnRemove = new Button();
            btnRemove.Click += btnRemove_Click;
            btnRemove.Text = "Remove";
            layout.Controls.Add(btnRemove);

            Load+=new EventHandler(Form1_Load);
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            _employees = new BindingList<Employee>();
            for (int i = 0; i < 10; i++)
            {
                _employees.Add(new Employee() { Id = i, Name = "Employee " + i.ToString() }); 
            }

            lstEmployees.DisplayMember = "Name";
            lstEmployees.DataSource = _employees;

            txtId.DataBindings.Add("Text", _employees, "Id");
            txtName.DataBindings.Add("Text", _employees, "Name");
        }

        private void btnRemove_Click(object sender, EventArgs e)
        {
            Employee selectedEmployee = (Employee)lstEmployees.SelectedItem;
            if (selectedEmployee != null)
            {
                _employees.Remove(selectedEmployee);
            }
        }
    }
}

8 Comments

This is actually less work than the currently accepted answer. Magnificent! I edited your post to include an example.
You could actually improve that further I think. You can apply parent and child bindings to controls meaning that you could do without the _SelectedIndexChanged event handler. I forget the precise code though..... :(
I've updated the example, removing the SelectedIndexChanged event handler and replacing with 2 new lines in the Load handler. :)
I was using a regular List, but I didn't think that was the problem and was looking everywhere else for the trouble. Thanks!
From what I can tell, this doesn't address the issue of actually updating the text of the listed items.
|
17
typeof(ListBox).InvokeMember("RefreshItems", 
  BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.InvokeMethod,
  null, myListBox, new object[] { });

1 Comment

I found this useful: it can be used to cause the listbox to refresh dynamically
10

Use the datasource property and a BindingSource object in between the datasource and the datasource property of the listbox. Then refresh that.

update added example.

Like so:

Public Class Form1

    Private datasource As New List(Of NumberInfo)
    Private bindingSource As New BindingSource

    Protected Overrides Sub OnLoad(ByVal e As System.EventArgs)
        MyBase.OnLoad(e)

        For i As Integer = 1 To 3
            Dim tempInfo As New NumberInfo()
            tempInfo.Count = i
            tempInfo.Number = i * 100
            datasource.Add(tempInfo)
        Next
        bindingSource.DataSource = datasource
        ListBox1.DataSource = bindingSource
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        For Each objItem As Object In datasource
            Dim info As NumberInfo = DirectCast(objItem, NumberInfo)
            info.Count += 1
        Next
        bindingSource.ResetBindings(False)
    End Sub
End Class

Public Class NumberInfo

    Public Count As Integer
    Public Number As Integer

    Public Overrides Function ToString() As String
        Return String.Format("{0}, {1}", Count, Number)
    End Function
End Class

3 Comments

Excellent. For some reason, data binding in WinForms never jumps out at me as a solution no matter how much I use it in WPF.
Heh it used to be even more fun than this. Something like: ((CurrencyManager)this.BindingContext[ListBox1]).Refresh(); Getting a "hidden" object out of the BindingContext and then casting it to a currency manager. Although this is the C# as I never did this in VB.NET.
This is a good answer, but ultimately geno's suggestion to use BindingList<T> leads to less work.
2

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).

Comments

0

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;
}

1 Comment

This will always put the selected item at the end of the ListBox!
0

If you use a draw method like:

private void listBox1_DrawItem(object sender, DrawItemEventArgs e)
{
    e.DrawBackground();
    e.DrawFocusRectangle();

    Sensor toBeDrawn = (listBox1.Items[e.Index] as Sensor);
    e.Graphics.FillRectangle(new SolidBrush(toBeDrawn.ItemColor), e.Bounds);
    e.Graphics.DrawString(toBeDrawn.sensorName, new Font(FontFamily.GenericSansSerif, 14, FontStyle.Bold), new SolidBrush(Color.White),e.Bounds);
}

Sensor is my class.

So if I change the class Color somewhere, you can simply update it as:

int temp = listBoxName.SelectedIndex;
listBoxName.SelectedIndex = -1;
listBoxName.SelectedIndex = temp;

And the Color will update, just another solution :)

Comments

0

If you are doing databinding, try this:

private void CheckBox_Click(object sender, EventArgs e)
{
    // some kind of hack to make the ListBox refresh
    int currentPosition = bindingSource.Position;
    bindingSource.Position += 1;
    bindingSource.Position -= 1;
    bindingSource.Position = currentPosition;
}

In this case there is a checkbox that updates an item in a data bound ListBox. Toggling the position of the binding source back and forth seems to work for me.

Comments

0

Some code that I built some code in VBnet to help do this. The class for anObject has the ToString override to show the object's "title/name".

Dim i = LstBox.SelectedIndex
LstBox.Items(i) = anObject
LstBox.Sorted = True

Comments

0

you also can try with this fragment of code, it works fine:

Public Class Form1

Dim tempInfo As New NumberInfo()

Private Sub Form1_Load() Handles Me.Load
    For i As Integer = 1 To 3
        tempInfo.Count = i
        tempInfo.Number = i * 100
        ListBox1.Items.Add(tempInfo)
    Next
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim info As NumberInfo = tempInfo
    Dim obj As New Object
    info.Count += 1
    info.Number = info.Count * 100
    obj = info
    ListBox1.Items.Add(obj)
    ListBox1.Items.RemoveAt(0)
End Sub
End Class

Public Class NumberInfo
Public Count As Integer
Public Number As Integer
    Public Overrides Function ToString() As String
        Return String.Format("{0}, {1}", Count, Number)
    End Function
End Class

Comments

-1

I don't know much about vb.net but in C# you should use datasource and then bind it by calling listbox.bind() would do the trick.

1 Comment

Thats for teh internets. Methinks this person interested in WinForm.
-3

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.