Reputation: 377
I'm having problems focusing the textbox. I want to focus the textbox when I select a specific item from the listview. The Focus()
will work when I use the up down arrows from the keyboards, but when I use the mouse its not working.
***EDIT***
private void listView1_SelectedIndexChanged(object sender, EventArgs e)
{
textBox1.Focus();
}
private void Form1_Load(object sender, EventArgs e)
{
ListViewItem lvi = new ListViewItem("A");
lvi.SubItems.Add("AA");
listView1.Items.Add(lvi);
ListViewItem lvi1 = new ListViewItem("B");
lvi1.SubItems.Add("BB");
listView1.Items.Add(lvi1);
}
Upvotes: 1
Views: 1971
Reputation: 15813
The Click and Mouseclick events happen after the listview SelectedIndexChanged event, so if you have textbox.focus in SelectedIndexChanged, focus comes back to the listview after the click or mousclick event. If you add a textbox.focus to the listview mouseclick event, focus will end up on the text box (even though it goes there twice).
Upvotes: 2
Reputation: 129
In your SelectedIndexChanged event handler, first test if this.listView1.SelectedIndex > -1, and if it is, then do this.textBox1.Focus()
Upvotes: 0