ListView默认选择项问题

的确是该控件的一个bug,http://support.microsoft.com/default.aspx?scid=kb;en-us;240946
在msdn上找到了。我把它的代码修改了一下,解决了这个问题。

Option Explicit
Private m_DefualtListItem As ListItem              '默认的选择项、或者先前的选择项
Private m_MouseDownX As Single, m_MouseDownY As Single '鼠标按下时的坐标
' Index of currently depressed button (vbLeftButton or vbRightButton)
Private m_iButton As Integer

Private Sub Form_Load()
  Dim i As Integer
  For i = 1 To 20
    Call ListView1.ListItems.Add(Text:="item" & i)
  Next
End Sub

Private Sub ListView1_MouseDown(Button As Integer, _
            Shift As Integer, x As Single, y As Single)

  ' If the left- or right-mouse button is being depressed, and is not over
  ' a ListItem, set the module level variable to the button's value.
    If Button And (ListView1.HitTest(x, y) Is Nothing) Then m_iButton = Button
   
    Set m_DefualtListItem = ListView1.SelectedItem
    m_MouseDownX = x / Screen.TwipsPerPixelX
    m_MouseDownY = y / Screen.TwipsPerPixelY  
End Sub

Private Sub ListView1_MouseMove(Button As Integer, _
            Shift As Integer, x As Single, y As Single)
  ' If no mouse button is currently pressed, and the module level
  ' variable still contains a button value set in MouseDown, then
  ' a mouse button is being released, call the MouseUp and Click
  ' events, and zero the variable. (to our favor, the ListView raises
  ' a MouseMove when any mouse button is released).
    If (Button = 0) And m_iButton Then
        Call ListView1_MouseUp(m_iButton, Shift, x, y)
        m_iButton = 0
    End If
End Sub

Private Sub ListView1_MouseUp(Button As Integer, _
            Shift As Integer, x As Single, y As Single)

  ' Clear the module level variable here also in case one mouse button
  ' is depressed while the other is already down, and either is released,
  ' prevents the MouseMove code from calling unwanted events
    If Not m_DefualtListItem Is Nothing And m_iButton = 1 Then
        Dim DefaultItemRect As RECT, SelectRect As RECT, Intersect As RECT
       
        With DefaultItemRect
        .Left = m_DefualtListItem.Left
        .Top = m_DefualtListItem.Top
        .Right = m_DefualtListItem.Left + m_DefualtListItem.Width
        .Bottom = m_DefualtListItem.Top + m_DefualtListItem.Height
        End With
       
        x = x / Screen.TwipsPerPixelX
        y = y / Screen.TwipsPerPixelY
        With SelectRect
        .Left = IIf(x > m_MouseDownX, m_MouseDownX, x)
        .Right = IIf(x > m_MouseDownX, x, m_MouseDownX)
        .Top = IIf(y > m_MouseDownY, m_MouseDownY, y)
        .Bottom = IIf(y > m_MouseDownY, y, m_MouseDownY)
        End With
       
        Call IntersectRect(Intersect, DefaultItemRect, SelectRect)
        If Intersect.Right - Intersect.Left = 0 Or Intersect.Bottom - Intersect.Top = 0 Then
            m_DefualtListItem.Selected = False
        End If
    End If
    m_iButton = 0
End Sub

Microsoft Windows Common Controls 6.0---用的是这个库
listview的风格是list和multiselect。
相关的链接还有:http://support.microsoft.com/default.aspx?scid=kb;en-us;149275
http://support.microsoft.com/default.aspx?scid=kb;en-us;149326
其中称:This bug has been fixed in Visual Basic 6.0.但是好像没解决

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值