以文本方式查看主题

-  Foxtable(狐表)  (http://foxtable.net/bbs/index.asp)
--  专家坐堂  (http://foxtable.net/bbs/list.asp?boardid=2)
----  listview鼠标移动当前行变色问题  (http://foxtable.net/bbs/dispbbs.asp?boardid=2&id=144043)

--  作者:douglas738888
--  发布时间:2019/12/9 15:01:00
--  listview鼠标移动当前行变色问题

 

请教老师,想实现鼠标进入LISTVIEW控件时,鼠标移动到相关行(不是点击当前行),该行的背景颜色改变

 

我们模仿网上的一段代码改写后报错,请老师帮忙看看

Public Sub Listview_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
Dim Lv As System.Windows.Forms.Listview = CType(sender, System.Windows.Forms.Listview)
    Dim OldItem As System.Windows.Forms.ListViewItem = Nothing
    If lv.Tag IsNot Nothing Then OldItem = CType(lv.Tag, System.Windows.Forms.ListViewItem)
    Dim Item As System.Windows.Forms.ListViewItem = Lv.GetItemAt(e.X, e.Y)

    If Item IsNot Nothing Then
        If OldItem IsNot Nothing AndAlso Not OldItem.Equals(Item) Then OldItem.BackColor = lv.BackColor
        Item.BackColor = Color.Red
        lv.Tag = Item
    Else
        If OldItem IsNot Nothing AndAlso Not OldItem.BackColor.Equals(Lv.BackColor) Then OldItem.BackColor = lv.BackColor
    End If
End Sub

 

\'----------------------------------
Dim l As System.windows.forms.Listview = e.form.controls("listview1").basecontrol
addhandler l.mousemove, addressof Listview_MouseMove
l.refresh


--  作者:有点蓝
--  发布时间:2019/12/9 15:18:00
--  
Listview.tag属性被Foxtable底层用作其它用途了,换个方式

Public Sub Listview_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
Dim Lv As System.Windows.Forms.Listview = CType(sender, System.Windows.Forms.Listview)
Dim OldItem As System.Windows.Forms.ListViewItem = Nothing

If vars("a") IsNot Nothing AndAlso Typeof vars("a") Is System.Windows.Forms.ListViewItem Then OldItem = CType(vars("a"), System.Windows.Forms.ListViewItem)
Dim Item As System.Windows.Forms.ListViewItem = Lv.GetItemAt(e.X, e.Y)

If Item IsNot Nothing Then
    If OldItem IsNot Nothing AndAlso Not OldItem.Equals(Item) Then OldItem.BackColor = lv.BackColor
    Item.BackColor = Color.Red
    vars("a") = Item
Else
    If OldItem IsNot Nothing AndAlso Not OldItem.BackColor.Equals(Lv.BackColor) Then OldItem.BackColor = lv.BackColor
End If
End Sub

--  作者:douglas738888
--  发布时间:2019/12/10 9:31:00
--  

谢谢老师指导,还有一个问题,如果鼠标离开listview控件,原来的行背景颜色不会消失,这个怎样处理  麻烦老师再指导指导

 

我在mouseLeave设置相关代码也不起作用

 

Public Sub Listview1_MouseLeave(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
Dim Lv As System.Windows.Forms.Listview = CType(sender, System.Windows.Forms.Listview)
Dim OldItem As System.Windows.Forms.ListViewItem = Nothing


If vars("b") IsNot Nothing AndAlso Typeof vars("b") Is System.Windows.Forms.ListViewItem Then OldItem = CType(vars("b"), System.Windows.Forms.ListViewItem)
Dim Item As System.Windows.Forms.ListViewItem = Lv.GetItemAt(e.X, e.Y)


If Item IsNot Nothing Then
    If OldItem IsNot Nothing AndAlso Not OldItem.Equals(Item) Then OldItem.BackColor = lv.BackColor
    Item.BackColor = Color.white

    vars("b") = Item
    If OldItem IsNot Nothing AndAlso Not OldItem.Equals(Item) Then OldItem.foreColor = lv.foreColor 
    vars("b") = Item
Else
    If OldItem IsNot Nothing AndAlso Not OldItem.BackColor.Equals(Lv.BackColor) Then OldItem.BackColor = lv.BackColor
End If
End Sub

[此贴子已经被作者于2019/12/10 9:31:50编辑过]

--  作者:有点蓝
--  发布时间:2019/12/10 9:38:00
--  

Public Sub Listview1_MouseLeave(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
Dim Lv As System.Windows.Forms.Listview = CType(sender, System.Windows.Forms.Listview)
Dim OldItem As System.Windows.Forms.ListViewItem = Nothing


If vars("b") IsNot Nothing AndAlso Typeof vars("b") Is System.Windows.Forms.ListViewItem Then OldItem = CType(vars("b"), 

    If OldItem IsNot Nothing Then 

    OldItem.BackColor = lv.BackColor 

    OldItem.foreColor = lv.foreColor 
    vars("b") = nothing

    end if
end if
End Sub


--  作者:douglas738888
--  发布时间:2019/12/10 10:30:00
--  

老师,是否受底层影响,您指导代码在鼠标离开控件后,报错

(无法将类型system.eventargs的对象强制转换为类型system.windows.forms.mouseeventargs


--  作者:有点蓝
--  发布时间:2019/12/10 10:32:00
--  

Public Sub Listview1_MouseLeave(ByVal sender As Object, ByVal e As System.Windows.Forms.EventArgs)


--  作者:douglas738888
--  发布时间:2019/12/10 12:51:00
--  

老师,还是不行,做了个例子,请老师看看,谢谢

 

 

 下载信息  [文件大小:   下载次数: ]
图片点击可在新窗口打开查看点击浏览该文件:管理项目1.foxdb


--  作者:有点蓝
--  发布时间:2019/12/10 14:09:00
--  
其实不需要全局代码的
 下载信息  [文件大小:   下载次数: ]
图片点击可在新窗口打开查看点击浏览该文件:管理项目12.zip