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

  • Question

  • 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.
    jeudi 1 juin 2006 07:04

Réponses

  • 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;
         }
    }

    jeudi 1 juin 2006 07:40

Toutes les réponses

  • 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;
         }
    }

    jeudi 1 juin 2006 07:40
  • Thank you very much!
    jeudi 1 juin 2006 09:20
  • You're welcome.
    vendredi 2 juin 2006 01: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 ??
    jeudi 24 janvier 2008 11:56