views:

979

answers:

4

Easy one for you guys.

I have a textbox on top of a listbox.

The textbox is use to filter ther data in the listbox.

So... When the user type in the textbox, I would like to "trap" the down/up/pagedown/pageup keystrokes and fowarding them to the listbox.

I know I could use the Win32 API and send the WM_KeyDown message. But there's must be some .NET way to do this.

+2  A: 

SendKeys.Send() Method.

 private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            listBox1.Focus();
            SendKeys.Send(e.KeyChar.ToString());
        }

Here is code through which you can select a list item.

private void Form1_Load(object sender, EventArgs e)
        {
            textBox1.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
            textBox1.AutoCompleteSource=AutoCompleteSource.CustomSource;
            string[] ar = (string[])(listBox1.Items.Cast<string>()).ToArray<string>();
            textBox1.AutoCompleteCustomSource.AddRange(ar);
        }
        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            listBox1.Text  = textBox1.Text;
        }
adatapost
How do I tell SendKeys.Send to send the keystroke to the listbox control?
vIceBerg
And if I want the focus to stay in the textbox, I do textbox.focus() right after?Weird that sendkey don't have an overload to pass a control to it...
vIceBerg
+1  A: 

You can use Data Binding

            listBox1.DataBindings.Add("DataSource", textBox1, "Text", true, DataSourceUpdateMode.OnPropertyChanged).
            Format += (sender, e) =>
            {
                e.Value = _strings.FindAll(s => s.StartsWith((string) e.Value));
            };
Jacob Seleznev
Data binding to send a keystroke to another control??
vIceBerg
No, to filter ther data in the list box. If you bind Text property of the textbox to DataSource property of the listbox and use DataSourceUpdateMode.OnPropertyChanged you can achieve this.
Jacob Seleznev
Thanks for the tip
vIceBerg
Oh... I think I can't do this. Because the listbox items are already filled with a List<> binded to the datasource property...
vIceBerg
You need to somehow filter the listbox content in the Format event for the binding.
Jacob Seleznev
A: 

Using SendKeys methods

rahul
How do I tell SendKeys.Send to send the keystroke to the listbox control?
vIceBerg
A: 

In our wpf app we have a textbox that filters a listbox, we use the previewkeyup event. Inside the code, we can check what key was pressed (don't have my code in front of me, it's something like e.Key == Key.UpArrow, either way there's a built in class in C# for this). If it's one of the hot keys, we manipulate the user control accordingly.

For the listbox we tossed it into a user control and implemented an interface, called it NavigateableListbox or something like that, forced it to implement MoveUp(), MoveDown(), PageUp(), PageDown() etc so the textbox event just says if e.Key = Key.UpArrow { mylistbox.MoveUp() }

Eric