주 콘텐츠로 건너뛰기

 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