zoukankan      html  css  js  c++  java
  • 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);
                }
            }

  • 相关阅读:
    小结 编程练习 制作新按钮,“新窗口打开网站” ,新窗口打开时弹出确认框,是否打开,通过输入对话框,打开的窗口要求,宽400像素,高500像素,无菜单栏、无工具栏。
    关闭窗口(window.close)
    打开新窗口(window.open) open() 方法可以查找一个已经存在或者新建的浏览器窗口。 语法: window.open([URL], [窗口名称], [参数字符串])
    提问(prompt 消息对话框)用于询问一些需要与用户交互的信息。弹出消息对话框(包含一个确定按钮、取消按钮与一个文本输入框)
    确认(confirm 消息对话框)语法:confirm(str); 消息对话框通常用于允许用户做选择的动作,如:“你对吗?”等。弹出对话框(包括一个确定按钮和一个取消按钮)
    警告(alert 消息对话框) 如果你不点击“确定”,就不能对网页做任何操作,这个小窗口就是使用alert实现的
    Socket的3次握手链接与4次断开握手
    大型电子商务网站架构之--分布式可扩展数据库架构
    程序员成长历程的四个阶段
    盘点 Github 所用到的开源项目
  • 原文地址:https://www.cnblogs.com/fanyf/p/2278957.html
Copyright © 2011-2022 走看看