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

    粘自:http://www.cnblogs.com/fanyf/archive/2011/12/07/2278957.html

  • 相关阅读:
    算法与数据结构(1):基础部分——以插入排序为例
    软件工程结对作业
    软件工程第1次作业
    软件工程第0次作业
    python爬虫随笔(2)—启动爬虫与xpath
    python爬虫随笔-scrapy框架(1)——scrapy框架的安装和结构介绍
    【面试题】String类、包装类的不可变性
    【面试题】Java类初始化和实例初始化的顺序
    【面试题】Java单例设计模式-饿汉式枚举(enum)单例
    【面试题】从JVM的角度去理解i++和++i
  • 原文地址:https://www.cnblogs.com/wwz-wwz/p/8462152.html
Copyright © 2020-2023  润新知