ListView失去焦点选中行不能高亮显示的问题解决

方法一:

1.ListView的HideSelection属性设置为True。

 

2.ListView的Validated事件处理

        /// <summary>
        /// 失去焦点事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void lvSeries_Validated(object sender, EventArgs e)
        {
            try
            {
                if (lvSeries.FocusedItem != null)
                {
                    lvSeries.FocusedItem.BackColor = SystemColors.Highlight;
                    lvSeries.FocusedItem.ForeColor = Color.White;
                    lvSeries.SelectedIndices.Add(lvSeries.FocusedItem.Index);
                }
            }
            catch (Exception eEx)
            {
                MessageBox.Show(eEx.Message);
            }
        }

 

3.ListView的ItemSelectionChanged事件处理

        /// <summary>
        /// 选择变化事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void lvSeries_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e)
        {
            try
            {
                e.Item.ForeColor = Color.Black;
                e.Item.BackColor = SystemColors.Window;

                if (lvSeries.FocusedItem != null)
                {
                    lvSeries.FocusedItem.Selected = true;
                }
            }
            catch (Exception eEx)
            {
                MessageBox.Show(eEx.Message);
            }
        }

方法二:只能单选行功能,并排除方法一中的不易发现的Bug

          this.listview1.Validated += new System.EventHandler(this.lvSeries_Validated);
          this.listview1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.lvSeries_MouseDown);

        /// <summary>
        /// 失去焦点事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void lvSeries_Validated(object sender, EventArgs e)
        {
            try
            {
                if (lvSeries.FocusedItem != null)
              {
                lvSeries.FocusedItem.BackColor = SystemColors.Highlight;
                lvSeries.FocusedItem.ForeColor = Color.White;
                 }         

             }
            catch (Exception eEx)
            {
                MessageBox.Show(eEx.Message);
            }
        }

        /// <summary>
        /// 重新选择行事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void lvSeries_MouseDown(object sender, MouseEventArgs e)
        {
            try
            {

               ListViewItem _curItem = this.lvSeries.GetItemAt(e.X, e.Y);
                 foreach (ListViewItem item in lvSeries.Items)
             {
                item.ForeColor = Color.Black;
                item.BackColor = Color.White;
            }
            if (_curItem != null && _curItem.Index > -1)
            {
                _curItem.BackColor = SystemColors.Highlight;
                _curItem.ForeColor = Color.White;
              }
            else
            {
                if (lvSeries.FocusedItem != null)
                {
                    lvSeries.FocusedItem.BackColor = SystemColors.Highlight;
                    lvSeries.FocusedItem.ForeColor = Color.White;
                 }
              }           

           }
            catch (Exception eEx)
            {
                MessageBox.Show(eEx.Message);
            }
        }

 

 

posted @ 2011-12-07 11:15  园封记忆  阅读(8987)  评论(0编辑  收藏  举报