Update bound ListBox

M

mick

I`ll try to make this clear:)

I have a BindingList (progList) containing progItems.

class ProgItem
{
string name;
string path;
getters...setters here
overridden ToString() here
}

progList is bound to a ListBox and the "name" for each item is appearing
as it should.

I also have a textbox which will also diaplay the name of the particular item
that is selected in the Listbox. Next to the textbox is a button (Change). The idea
is, if I change the contents of the name textbox then press the Change button
the item.name will be changed to reflect this. Problem is, although the item name updates
the listbox does not.

private void btnChange_Click(object sender, EventArgs e)
{
// Find the selected item ***Thanks Pete***
ProgItem item = progList.Cast<ProgItem>().FirstOrDefault(delegate(ProgItem x)
{
return x.Name == lbxTabProgList.SelectedItem.ToString();
});

item.Name = tbTabProgListName.Text;
item.Path = tbTabProgListLocation.Text;
lbxTabProgList.Refresh(); //*** Tried this - doesnt work ***
lbxTabProgList.DataSource = null; // nulling then resetting the datasource does
lbxTabProgList.DataSource = progList; // work however
}

Dont think resetting the datasource is the right way so does anyone know why the listbox
update to what it`s bound to?

TIA,
mick
 
M

mick

I`ll try to make this clear:)

I have a BindingList (progList) containing progItems.

class ProgItem
{
string name;
string path;
getters...setters here
overridden ToString() here
}

progList is bound to a ListBox and the "name" for each item is appearing
as it should.

I also have a textbox which will also diaplay the name of the particular item
that is selected in the Listbox. Next to the textbox is a button (Change). The idea
is, if I change the contents of the name textbox then press the Change button
the item.name will be changed to reflect this. Problem is, although the item name updates
the listbox does not.

private void btnChange_Click(object sender, EventArgs e)
{
// Find the selected item ***Thanks Pete***
ProgItem item = progList.Cast<ProgItem>().FirstOrDefault(delegate(ProgItem x)
{
return x.Name == lbxTabProgList.SelectedItem.ToString();
});

item.Name = tbTabProgListName.Text;
item.Path = tbTabProgListLocation.Text;
lbxTabProgList.Refresh(); //*** Tried this - doesnt work ***
lbxTabProgList.DataSource = null; // nulling then resetting the datasource does
lbxTabProgList.DataSource = progList; // work however
}

Dont think resetting the datasource is the right way so does anyone know why the listbox
update to what it`s bound to?

Sorted it. Update() was the one I wanted.

mick
 
M

mick

private void btnChange_Click(object sender, EventArgs e)
{
// Find the selected item ***Thanks Pete***
ProgItem item =
progList.Cast<ProgItem>().FirstOrDefault(delegate(ProgItem x)
{
return x.Name == lbxTabProgList.SelectedItem.ToString();
});
item.Name = tbTabProgListName.Text;
item.Path = tbTabProgListLocation.Text;
lbxTabProgList.Refresh(); //*** Tried this - doesnt work ***
lbxTabProgList.DataSource = null; // nulling then resetting the datasource does
lbxTabProgList.DataSource = progList; // work however
}
Dont think resetting the datasource is the right way so does anyone know
why the listbox
update to what it`s bound to?
Sorted it. Update() was the one I wanted.

It worked the first time but doesnt work now, so back to the problem.
Anyone?

mick
 
G

**Group User**

<snip>

 >  private void btnChange_Click(object sender, EventArgs e)
 >       {
 >           // Find the selected item ***Thanks Pete***>             ProgItem item =

 >           {
 >              return x.Name == lbxTabProgList.SelectedItem.ToString();
 >           });

 >           item.Name = tbTabProgListName.Text;
 >          item.Path = tbTabProgListLocation.Text;
 >          lbxTabProgList.Refresh();                   //*** Tried this -
doesnt work ***
 >           lbxTabProgList.DataSource = null;        // nulling then
resetting the datasource does
 >          lbxTabProgList.DataSource = progList; // work however
 >       }


It worked the first time but doesnt work now, so back to the problem.
Anyone?

mick

You already tried it out 3 times. How about the fourth time and on ?
 
P

Peter Duniho

mick said:
[...]
lbxTabProgList.Refresh(); //*** Tried this - doesnt work ***
lbxTabProgList.DataSource = null; // nulling then resetting the datasource does
lbxTabProgList.DataSource = progList; // work however
}
Dont think resetting the datasource is the right way so does anyone
know why the listbox
update to what it`s bound to?
Sorted it. Update() was the one I wanted.

It worked the first time but doesnt work now, so back to the problem.
Anyone?

The Refresh() and Update() methods in Control aren't related to data
binding. They simply deal with redrawing the visual on-screen after
some underlying data in the control changes. Normally you don't need
those methods at all, since changes typically cause a redraw to happen
automatically. And even when you might use them, they won't cause
underlying data to be updated; they simply cause the control to be redrawn.

I think what you want is the BindingList<T>.ResetItem() method. That
will alert other objects that are bound to the BindingList<T> object
that an individual item within the list has changed.

Note that the method takes as its argument an index, not a specific
object, while in your code you have a specific object but not an index.
Fortunately, this isn't really a problem because the code you've
posted actually isn't really the best way to deal with finding the right
object in the list and modifying it.

Try this instead:

private void btnChange_Click(object sender, EventArgs e)
{
int iitem = lbxTabProgList.SelectedIndex;
ProgItem item = progList[iitem];

item.Name = tbTabProgListName.Text;
item.Path = tbTabProgListLocation.Text;

progList.ResetItem(iitem);
}

Pete
 
M

mick

Peter Duniho said:
mick said:
[...]
lbxTabProgList.Refresh(); //*** Tried this - doesnt work ***
lbxTabProgList.DataSource = null; // nulling then resetting the datasource does
lbxTabProgList.DataSource = progList; // work however
}
Dont think resetting the datasource is the right way so does anyone know
why the listbox
update to what it`s bound to?
Sorted it. Update() was the one I wanted.

It worked the first time but doesnt work now, so back to the problem.
Anyone?

The Refresh() and Update() methods in Control aren't related to data
binding. They simply deal with redrawing the visual on-screen after some
underlying data in the control changes. Normally you don't need those
methods at all, since changes typically cause a redraw to happen
automatically. And even when you might use them, they won't cause
underlying data to be updated; they simply cause the control to be
redrawn.

I think what you want is the BindingList<T>.ResetItem() method. That will
alert other objects that are bound to the BindingList<T> object that an
individual item within the list has changed.

Note that the method takes as its argument an index, not a specific
object, while in your code you have a specific object but not an index.
Fortunately, this isn't really a problem because the code you've posted
actually isn't really the best way to deal with finding the right object
in the list and modifying it.

Try this instead:

private void btnChange_Click(object sender, EventArgs e)
{
int iitem = lbxTabProgList.SelectedIndex;
ProgItem item = progList[iitem];

item.Name = tbTabProgListName.Text;
item.Path = tbTabProgListLocation.Text;

progList.ResetItem(iitem);
}

Yep, thats the one. Thanks again.

mick
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top