Windows Develop Bookmark and Share   
 index > Windows Forms Designer > Update listbox selected item
 

Update listbox selected item

Hi

How can I update the text of the selected item?

Code Snippet

((MyObject)lst.SelectedItem).Name = txt.Text;

lst.Refresh();

or

lst.Update();

The object is updated but I can't see it until the item is selected again

Thanks

Itzik Kramer  Wednesday, February 27, 2008 2:33 PM

Hi, Kramer

You can use a BindingSource as the DataSource of the ListBox, and data bind the TextBox to the same BindingSource as well, the you'll find the value of the SelectedItem in the ListBox and the text in the TextBoxstay in sync, you don't have to handle the TextChanged event at all, see my sample below for the details.

Code Snippet

void Form18_Load(object sender, EventArgs e)

{

List<Class2> l = new List<Class2>();

l.Add(new Class2(1, "11"));

l.Add(new Class2(2, "22"));

l.Add(new Class2(3, "33"));

l.Add(new Class2(4, "44"));

BindingSource bs = new BindingSource();

bs.DataSource = l;

listBox1.DisplayMember = "Name";

listBox1.ValueMember = "ID";

listBox1.DataSource = bs;

this.textBox1.DataBindings.Add("Text", bs, "Name", true,
DataSourceUpdateMode.OnPropertyChanged);

}

Zhi-Xin Ye  Tuesday, March 04, 2008 2:36 AM

Thank's again

I was able to cancel the change by setting

e.value = old value

(e is argument in the parse event)

Thanks

Itzik Kramer  Tuesday, March 04, 2008 9:12 AM

Hi,

I think you use the following code to work around.

Code Snippet
lst.Items[lst.SelectedIndex]="Updated value";

jsj_Samson  Thursday, February 28, 2008 7:58 AM

It dosn't work I am getting an error

"Items collection cannot be modified when the DataSource property is set."

Thanks

Itzik Kramer  Thursday, February 28, 2008 8:01 AM

Could you paste your main code please ?

I will debug for you.

Samson

jsj_Samson  Thursday, February 28, 2008 8:04 AM

Thanks

Code Snippet

class Class2

{

private int _ID;

private string _Name;

public string Name

{

get { return _Name; }

}

public int ID

{

get { return _ID; }

}

public Class2(int I, string N)

{

_ID = I;

_Name = N;

}

}

private void Form1_Load(object sender, EventArgs e)

{

List<Class2> l = new List<Class2>();

l.Add(new Class2(1, "11"));

l.Add(new Class2(2, "22"));

l.Add(new Class2(3, "33"));

l.Add(new Class2(4, "44"));

lst.DisplayMember = "Name";

lst.ValueMember = "ID";

lst.DataSource = l;

}

private void txt_TextChanged(object sender, EventArgs e)

{

((Class2)lst.SelectedItem).Name = txt.Text;

lst.Items[lst.SelectedIndex] = txt.Text;

}

Itzik Kramer  Thursday, February 28, 2008 8:18 AM

I think you can change it like this.

Code Snippet

private DataSet dataSource;

public void LoadData()

{

dataSource = new DataSet("dataSource");

DataTable dt = new DataTable("TestTable");

dt.Columns.Add("ID", typeof(int));

dt.Columns.Add("Name", typeof(string));

dt.Rows.Add(1, "Roy");

dt.Rows.Add(2, "Samson");

dataSource.Tables.Add(dt);

}

private void Form1_Load(object sender, EventArgs e)

{

this.listBox1.DisplayMember = "Name";

this.listBox1.ValueMember = "ID";

this.listBox1.DataSource = dataSource.Tables["TestTable"];

}

private void button2_Click(object sender, EventArgs e)

{

CurrencyManager cm=this.BindingContext[dataSource.Tables["TestTable"]] as CurrencyManager;

DataRowView view = cm.Current as DataRowView; // retrieve the selected item.

DataRow r = view.Row;

r["Name"] = "Updated";

}

Hope it help.
jsj_Samson  Thursday, February 28, 2008 8:57 AM
I don't realy know how to work with "BindingContext", Is thier any way that I use it with a List objectbecause my datasource is a List and not a DataTable.

Itzik Kramer  Thursday, February 28, 2008 9:17 AM

When the DataSource of the listBox control is not null, you cannot change the Items Property of the control.
jsj_Samson  Thursday, February 28, 2008 9:22 AM

To avoid that I tried now something else.

I ran over the collection and added each item but when I try to get the selected item something unexpected is happening as I wrote it the following thread

http://forums.microsoft.com/Forums/ShowPost.aspx?PostID=2919664&SiteID=1

anyway thanks for your help

Itzik Kramer  Thursday, February 28, 2008 9:30 AM

Hi, Kramer

You can use a BindingSource as the DataSource of the ListBox, and data bind the TextBox to the same BindingSource as well, the you'll find the value of the SelectedItem in the ListBox and the text in the TextBoxstay in sync, you don't have to handle the TextChanged event at all, see my sample below for the details.

Code Snippet

void Form18_Load(object sender, EventArgs e)

{

List<Class2> l = new List<Class2>();

l.Add(new Class2(1, "11"));

l.Add(new Class2(2, "22"));

l.Add(new Class2(3, "33"));

l.Add(new Class2(4, "44"));

BindingSource bs = new BindingSource();

bs.DataSource = l;

listBox1.DisplayMember = "Name";

listBox1.ValueMember = "ID";

listBox1.DataSource = bs;

this.textBox1.DataBindings.Add("Text", bs, "Name", true,
DataSourceUpdateMode.OnPropertyChanged);

}

Zhi-Xin Ye  Tuesday, March 04, 2008 2:36 AM
Thank very very very very much you realy solved a few problems that I had.

Itzik Kramer  Tuesday, March 04, 2008 7:11 AM

Hi

Is thier any way I can validate the data before it changes the data source?

I tried the following and it didn't work

Code Snippet

void MyBindingSource_CurrentItemChanged(object sender, EventArgs e)

{

if(some condition)

{

BindingSource MyBindingSource = (BindingSource)sender;

MyBindingSource.CancelEdit();

}

}

void MyBindingSource_CurrentChanged(object sender, EventArgs e)

{

if(some condition)

{

BindingSource MyBindingSource = (BindingSource)sender;

MyBindingSource.CancelEdit();

}

}

Itzik Kramer  Tuesday, March 04, 2008 8:30 AM

Hi, Kramer

You can handle the Validaing event of the TextBox, or you can handle the Parse event of the Binding object, something like this:

Code Snippet

void Form17_Load(object sender, EventArgs e)

{

Binding binding = new Binding("Text", bs, "Name", true,

DataSourceUpdateMode.OnPropertyChanged);

binding.Parse += new ConvertEventHandler(binding_Parse);

this.textBox1.DataBindings.Add(binding);

}

void binding_Parse(object sender, ConvertEventArgs e)

{

// do your validating here..

}


Zhi-Xin Ye  Tuesday, March 04, 2008 8:35 AM

Hi

Thanks on your quick response.

But I still don't understant how do I cancel the change

Itzik

Itzik Kramer  Tuesday, March 04, 2008 9:02 AM

Thank's again

I was able to cancel the change by setting

e.value = old value

(e is argument in the parse event)

Thanks

Itzik Kramer  Tuesday, March 04, 2008 9:12 AM

Hehe, I'm glad to hear that. Smile

Zhi-Xin Ye  Tuesday, March 04, 2008 9:13 AM

You can use google to search for other answers

Custom Search

More Threads

• Switching localizable forms language messes up design
• The line endings in the following file are not consistent. Do you want to normalize the line endings?
• UserControl TestContainer -- Startup Control determined by ?
• design verbs iin control
• Button border issue in flat style
• Eliminating flicker in transparent controls
• How to detect any mouse click or key press anywhere, either inside or outside the application
• Toolbar buttons and menu items dissappeared
• Check gridView "saved" status
• Editable Panel within UserControl