我把listview设置为多选后,用鼠标拉框,选择了某些选择项,当鼠标Up时,默认选择(就是那个用虚线框着的选择项)项总是会加入选择集合中,怎样把它去掉呢??
有时候,这个默认选择项又在选择矩形之内,这时有需要把这个选项也选择上:-(

解决方案 »

  1.   

    我想这个应该是Listview的一个Bug
      

  2.   

    winehero(编程人生) :有遇到你啦:-)
    你有没有尝试过解决这个问题?
      

  3.   

    我搜了一遍csdn,没有人问这个问题。大家都这么用吗??
    如果是bug的话,很多软件里都没有这种现象啊。我用spy看了,它们用的也是相同的控件。
      

  4.   

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

  5.   

    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 IntegerPrivate Sub Form_Load()
      Dim i As Integer
      For i = 1 To 20
        Call ListView1.ListItems.Add(Text:="item" & i)
      Next
    End SubPrivate 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 SubPrivate 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 SubPrivate 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
      

  6.   

    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.但是好像没解决