How can I add a values to a listbox from a textbox each time a space is pressed (split textbox value on space)
+1
A:
In a nutshell, you'll need a TextChanged
event handler on the textbox. You can then take the text, use String.Split()
to separate it into individual items, and add them to your listbox.
Anna Lear
2010-09-16 14:56:27
what hes says but don't forget that you'll either need to clear the listbox down each time or just add the last element of the array that split produces. (Not that I don't make that mistake every time or anything...)
FixerMark
2010-09-16 15:00:41
A:
Regarding the edit and it is really what you mean, let's give it a try. Add a new Listener to the TextBox for KeyPress
, then try this code:
private void YourTextBox_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar.Equals(' '))
{
String[] items = Regex.Split(YourListBox.Text, @"/\w/");
YourListBox.Items.Clear();
foreach (String item in items)
YourListBox.Items.Add(item);
}
}
This is very quick and dirty, as it will iterate over the whole text each time you press space, but it should do the trick.
ApoY2k
2010-09-16 15:00:40