Consider the following form:

I want textbox1 or textbox2 to be focused when i click on “1” or “2” in the listview.
I wrote the following code:
private void listView1_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e)
{
if (e.IsSelected)
{
if (e.Item.Text == "1")
textBox1.Focus();
else
textBox2.Focus();
}
}
But it doesn’t work. (Textbox gets focused for a moment, but after that the focus goes back to listview.) I want the focus to be on textbox when I select the relevant item in the listview.
Any suggestions?
Thank you for the help.
The focus is being reset to the
ListViewbecause of the order the events are being fired.listView1_ItemSelectionChanged.listView1this sets the focus to the list view.Disabling the control will work, but it adds a ‘flicker’ when the item is clicked. Otherwise, you can track the selected item on
MouseDownandMouseUp/MoustLeaveand set focus appropriately.Side note – do you need to use a
ListView? Did a quick test on my end and aListBoxbehaves the way you want without any hacks.