开发者

How to focus the textbox by selecting an item in a listview?

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);
    }
开发者_开发技巧


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).


In your SelectedIndexChanged event handler, first test if this.listView1.SelectedIndex > -1, and if it is, then do this.textBox1.Focus()

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜