Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DataBind listBox selected item to textboxes

I have ListBox data bind to list of users (collection):

    usersListBox.DataSource = null;
    usersListBox.DataSource = _users;
    usersListBox.DisplayMember = "Name";
    usersListBox.ValueMember = "Id";

Now I want properties of selected item to be shown in different text boxes, so I do the binding:

nameTextBox.DataBindings.Add("Text", usersListBox.SelectedItem, "Name");
loginTextBox.DataBindings.Add("Text", usersListBox.SelectedItem, "Login");

When form load I can see that values of selected item appear in textboxes, but when selected item in listBox is changed, values in text boxes are still the same. Do I have to catch selectedItemChanged of listbox and repeat binding of textboxes? Or I'm missing something and values in textboxes should change with changing selected item?

like image 710
Adrian Serafin Avatar asked Dec 01 '09 08:12

Adrian Serafin


1 Answers

If someone needs answer: you have to create binding source object and assign it list box and textboxes:

 usersBindingSource = new BindingSource();
 usersBindingSource.DataSource = _presenter.Users;

 usersListBox.DataSource = usersBindingSource;
 usersListBox.DisplayMember = "Name";
 usersListBox.ValueMember = "Id";

 nameTextBox.DataBindings.Add("Text", usersBindingSource, "Name", true, DataSourceUpdateMode.OnPropertyChanged);
 loginTextBox.DataBindings.Add("Text", usersBindingSource, "Login", true, DataSourceUpdateMode.OnPropertyChanged);
like image 85
Adrian Serafin Avatar answered Sep 23 '22 06:09

Adrian Serafin