Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Update listbox

Status
Not open for further replies.

Vandy02

Programmer
Jan 7, 2003
151
US
I have listbox that I populate with data from a text file..
I select an item and populate a textbox(txtFileText) with the item I selected...I want to be able to modify the data and then update the list with the btnUpdLst_Click

I was able to do this in VB .net...but unable to do so in C #...

private void listBox1_SelectedIndexChanged(object sender, System.EventArgs e)
{
txtFileText.Text = listBox1.SelectedItem.ToString();
j = listBox1.SelectedItem.ToString();
j2 = listBox1.SelectedIndex; }

private void btnUpdLst_Click(object sender, System.EventArgs e)
{
listBox1.Items[j2] = txtFileText.Text;

}
 
and what's not working?

here's what I use and it works:
Code:
int selectedIndex = -1;

private void listBox1_SelectedIndexChanged(object sender, System.EventArgs e)
{
     if (listBox1.SelectedItem != null)
     {
           textBox1.Text = listBox1.SelectedItem.ToString();
           selectedIndex = listBox1.SelectedIndex;
     }
}

private void Button1_Click(object sender, System.EventArgs e)
{
     listBox1.Items[selectedIndex] = textBox1.Text
}

--------------------------
"two wrongs don't make a right, but three lefts do" - the unknown sage
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top