VB.NET - Change listbox selected item text in VB net

Asked By mark van on 06-Mar-06 08:08 AM
I've got a textbox that automatically displays the listbox.selecteditem.  I'm trying to change the selecteditem text with what is in the textbox on enter or with a command button.

Am I naive to think that listbox.selecteditem=textbox.text should work?  Do I need to refresh the listbox after the command?  I thought this would be easier.  Thanks in advance.

Try this - Asked By Pankaj Sharma on 06-Mar-06 08:21 AM

Hi,

Use this code.

[CODE]
listBox1.SelectedIndex = listBox1.FindString(txtBox.Text);
[/CODE]

bye

ListBox SelectedItem Text - Asked By F Cali on 06-Mar-06 08:35 AM

Use the Text property of the SelectedItem and not just the SelectedItem itself:

listBox1.SelectedItem.Text = TextBox1.Text;
Asked By mark van on 06-Mar-06 09:22 AM
Thanks for the help, but neither solution worked.  Pankaj's did not throw an error, but did not change the selecteditem text.

Ronald's threw a missing member exception error.  "Public member 'text' on type 'String' not found."

To clarify, I'm changing the text in the textbox in run time and wanting to change the selecteditem to that text in run time.
Asked By Pankaj Sharma on 06-Mar-06 09:36 AM
Hi,

This will definitely work if you are developing Windows application.Just use the code as shown below in the TextBox_TextChanged event.

This will select the Listitem as it matches the string you typed in.

[CODE]
private void txtName_TextChanged(object sender, System.EventArgs e)
{
	listBox1.SelectedIndex = listBox1.FindString(txtName.Text);
}
[/CODE]

I am using it several places in my projects. Work fine.
Asked By mark van on 06-Mar-06 10:26 AM
More specifically, what I'm trying to do, is select a listbox item and change the text of the selected item during run time.  The user will select an item from a listbox, it will show up in a textbox, they will translate the text to a foreign language, and then I want to replace the selected listbox item text  with the translation.  So they can go down the listbox and translate each item to a forieign language.  I know that just having the text in a textbox instead of using a listbox might sound easier, but I need to do it this way.  Thanks for all the help so far.
Hi mark, - Asked By Pankaj Sharma on 07-Mar-06 02:28 AM
If you are listing simple string values in the listbox then you can easily modify the text of the selected item as shown in the ButtonClick event.

First you getting selected Item text in a TextBox. Then change the text in textbox(in case using your foreign langauge) and click Button to change the text in listbox and to display sorted list.

[CODE]
private void listBox1_SelectedIndexChanged(object sender, System.EventArgs e)
{
	txtName.Text = listBox1.Text; 		 
}

private void button1_Click(object sender, System.EventArgs e)
{
	//Declare new string array with size of number of items in listbox	
	string[] arrNames = new string[listBox1.Items.Count];
                //Copy all Items to the array
	listBox1.Items.CopyTo(arrNames,0);
                //Changing the text of selected item
	arrNames[listBox1.SelectedIndex]=txtName.Text;
                
                //Rebinding the listbox.   
	listBox1.DataSource = arrNames;
                //Selecting the changed item
	listBox1.SelectedIndex = listBox1.FindString(txtName.Text);
}
[/CODE]


And in case if you are binding your listbox to some DataTable Then you can get that datatable and change the text in datatable for the selected item and rebind the listbox to that same DataTable.

Hope this could solve your problem.
Thanks - Asked By mark van on 07-Mar-06 07:28 AM
Thanks for all of your help.  I also FINALLY found an "On the fly "way to do it without arrays since right now I'm writing from the listbox directly to a binary file.  The selected item stays selected while waiting for the changed text since I prompt a home-made input box for the translation:

x=listbox.selectedindex

listbox.items(x)=textbox.text

Thanks again. You're the best.