• 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

  • 相关阅读:
    购买成熟软件产品后的二次开发的问题
    outlook2010如何导入csv的通讯录?
    导入Excel数据时对数据校验提示方法
    系统开发中存储过程使用的优势和劣势
    FCKeditor.Net_2.5的使用
    [正则表达式]如何高亮显示搜索关键字
    国外网站模板网址集锦
    _NET 下 FCKeditor_2_5_1上传图片的配置
    用属性模拟多继承机制
    FCKeditor 2.6在ASP.NET中的配置方法
  • 原文地址:https://www.cnblogs.com/wwz-wwz/p/8462152.html
Copyright © 2020-2023  润新知