スキップしてメイン コンテンツへ

 none
How to select a row in a listview when the focus is in a textbox? RRS feed

  • 質問

  • i want to enter texts to my textbox and then if it matches an item in the listview it will programmatically select the row and keeping the focus at the textbox. i tried moving the focus to the listview, set selected = true, txtbox.focus(). but the selection disappears when the focus leaves the listview. please help.
    2006年6月1日 7:04

回答

  • You need to change the HideSelection property of the ListView to false.  Here's the sample code that I used to test that the selection works.

    private void textBox1_TextChanged(object sender, EventArgs e)
    {
          ListViewItem item = listView1.FindItemWithText(textBox1.Text);

          if (item != null)
          {
              listView1.SelectedItems.Clear();
              item.Selected = true;
         }
    }

    2006年6月1日 7:40

すべての返信

  • You need to change the HideSelection property of the ListView to false.  Here's the sample code that I used to test that the selection works.

    private void textBox1_TextChanged(object sender, EventArgs e)
    {
          ListViewItem item = listView1.FindItemWithText(textBox1.Text);

          if (item != null)
          {
              listView1.SelectedItems.Clear();
              item.Selected = true;
         }
    }

    2006年6月1日 7:40
  • Thank you very much!
    2006年6月1日 9:20
  • You're welcome.
    2006年6月2日 1:59
  • Hi,

    i was searching for the solution to this problem and i encountered this:
    Indeed, item IS selected in the listview but when this item is not in the visible area of items of the listview, then it doesn't shows up even if is selected.

    e.x., if a listview holds 100 items but display only the 50 (other 50 can be shown by scroll down the listview) then with the above solution, if the item is at, let's say, 70 position, then IS selected but cannot be shown. We have to srcoll down manually to actually "see" it.

    Is there a solution to this ??
    2008年1月24日 11:56