Winforms, Data Binding, Listbox and Text Box

I have ListBox ( MyListBox) on my screen and text box ( MyTextBox).

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

Now I am trying to do this:

The ListBox data source is List (Of T).

Now that the item is changing, I want the text box to be updated to the specific 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 fine.

_listOfItems declared as follows:

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

Where MyItem:

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

I tried to override ToString()in MyItemso that it uses this. But that doesn't work either.

Anyone want a try?

Thank!

-Snakiej

+5
1

, , BindingSource, ListBox.DataSource BindingSource.

  • BindingSource ;
  • ListBox.DataSource BindingSource;
  • ValueMember DisplayMember , ;
  • DataBinding TextBox BindingSource , MyItem.Comment;
  • List(Of MyItem)``to your Binding.DataSource`,
  • TextBox CurrencyManager.CurrentItem , ListBox.SelectedItem.

, , , INotifyPropertyChanged, .

SelectValue, ?

+9

All Articles