简体   繁体   中英

How to prevent C# WinForm listview selection Change when key is pressed and list view auto find next row on the basis of first column

My listview is populated from DB table When I press a number key the list view auto find that key in first column and change selection to that item. I haven't written any code for that purpose. I want to stop doing this.

For finding item by first column when key pressed in ListView:

        private void listView1_KeyUp(object sender, KeyEventArgs e)        
        {
            var i = 0;
            var data = listView1.Items.Cast<ListViewItem>()
                                 .Select((a,b)=>new { itemText = a.Text, index = i++ })
                                 .ToList();
            var findItem = data.Where(w => w.itemText.StartsWith(e.KeyValue.ToString())).FirstOrDefault();
            if (findItem == null)
                return;

            listView1.Items[findItem.index].Focused = true;
            listView1.Items[findItem.index].Selected = true;
            listView1.Items[findItem.index].EnsureVisible();
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM