1. 程式人生 > >WinForm中ListView設定選中行背景顏色,字型格式

WinForm中ListView設定選中行背景顏色,字型格式

 private void listView1_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e)
        {

            listView1.FullRowSelect = true;


            if (this.listView1.SelectedItems.Count > 0)
            {
               

                listView1.SelectedItems[0].SubItems[0].ForeColor = Color.Blue;

                //先清除原有格式

                foreach (ListViewItem item in listView1.Items)

                {

                     item.ForeColor = Color.Black;

                }
                foreach (ListViewItem item in listView1.Items)

                {

                    item.BackColor = Color.White;
                    Font f = new Font(Control.DefaultFont, FontStyle.Regular);
                    item.Font = f;
                }

                //加粗字型
                Font f2 = new Font(Control.DefaultFont, FontStyle.Bold);
                listView1.SelectedItems[0].SubItems[0].Font = f2;
                //設定選中行背景顏色
                listView1.SelectedItems[0].BackColor = Color.LightBlue;           
                listView1.SelectedItems[0].Selected = false;           
            }           

        }

下面是設定滑鼠滑過背景變色的程式碼,這個是參考的別人的

 private void listView1_MouseMove(object sender, MouseEventArgs e)
        {
            ListView _ListView = (ListView)sender;
            ListViewItem _OldItem = null;
            if (_ListView.Tag != null) _OldItem = (ListViewItem)_ListView.Tag;

            ListViewItem _Item = _ListView.GetItemAt(e.X, e.Y);
            if (_Item != null)
            {
                if (_OldItem != null && !_OldItem.Equals(_Item)) _OldItem.BackColor = _ListView.BackColor;
                _Item.BackColor = Color.LightBlue;
                _ListView.Tag = _Item;
            }
            else
            {
                if (_OldItem != null && !_OldItem.BackColor.Equals(_ListView.BackColor)) _OldItem.BackColor = _ListView.BackColor;
            }
        }