Winforms, databinding, Listbox and textbox

Posted by Snake on Stack Overflow See other posts from Stack Overflow or by Snake
Published on 2010-05-25T12:46:59Z Indexed on 2010/05/25 12:51 UTC
Read the original article Hit count: 359

Filed under:
|
|
|

Hi dear friends,

I have a ListBox (MyListBox) on my screen, and a Textbox (MyTextBox).

The ListBox is filled with a List(Of T), which are all custom items.

Now I try to do this:

The ListBox' datasource is the List(Of T).

Now when an Item changes I want the textbox to be updated to a particular property of the selected item in my ListBox.

In code this means:

Me.MyListBox.DisplayMember = "SelectionName"
Me.MyListBox.ValueMember = "Id"

MyTextbox.DataBindings.Add(New Binding("Text", Me._listOfItems, "SelectedItem.Comment", True, DataSourceUpdateMode.OnPropertyChanged))

Me.MyListBox.DataSource = Me._listOfItems

this does not work. But when I bind to SelectedValue instead of SelectedItem it works perfectly.

The _listOfItems is declared as this:

Dim _listOfItems As List(Of MyItem) = New List(Of MyItem)()

Where MyItem is this:

public class MyItem
{
    public string SelectionName { get; set; }
    public int Id { get; set; }
    public string Comment { get; set; }
}

I tried overriding the ToString() in MyItem so that it would use that. But that doesn't work either.

Anybody care to give it a try?

Thanks!

-Snakiej

© Stack Overflow or respective owner

Related posts about c#

Related posts about vb.net