How to change listview selected row backcolor even when focus on another control?

前端 未结 6 794
囚心锁ツ
囚心锁ツ 2020-11-28 13:18

I have a program which uses a barcode scanner as input device so that means I need to keep the focus on a text box.

The program has a listview control and I select o

6条回答
  •  庸人自扰
    2020-11-28 13:46

    You cant set focus on listview control in this situation. txtBarcode_Leave method will prevent this. But if you are desire to be able select listview items by clicking on them, just add code below to MouseClick event handler of listview:

        private void listView1_MouseClick(object sender, MouseEventArgs e)
        {
            ListView list = sender as ListView;
    
            for (int i = 0; i < list.Items.Count; i++)
            {
                if (list.Items[i].Bounds.Contains(e.Location) == true)
                {
                    list.Items[i].BackColor = Color.Blue; // highlighted item
                }
                else
                {
                    list.Items[i].BackColor = SystemColors.Window; // normal item
                }
            }
        }
    

提交回复
热议问题