按下ctrl键就可复选,要实现shift键复选可在keydown,keypress,keyup事件中编码解决,设置标志表明shift键正处于按下状态,在mousedown事件中编码记录下选中的行号,并让该行高亮即可。

解决方案 »

  1.   

    我要实现比较方便的复选,就是不用一次次的点击记录,而是多行复选,我用book实现不了,不知是不是使用方法不对?谁有更好的方法?
      

  2.   

    感谢您使用微软产品。DataGrid对象的SelBooks属性保存了选中的多行记录的books,您可以用SelBooks集合的Add方法把多行记录设为选定。您需要计算要选入的记录相对于当前活跃记录的位置,作为DataGrid对象的GetBook方法的参数,获得该行的BookMark,再使用DataGrid1.SelBooks的Add方法,API函数GetKeyState用于判断shift键的状态,做出相应的操作。如下例,Private Const VK_SHIFT = &H10
    Private Declare Function GetKeyState Lib "user32" (ByVal nVirtKey As Long) As IntegerPrivate last_selPrivate Sub Form_Load()
    last_sel = 0
    End SubPrivate Sub DataGrid1_SelChange(Cancel As Integer)   If GetKeyState(VK_SHIFT) < 0 Then
            relative = last_sel - DataGrid1.Row
            Text1.Text = relative
            If relative < 0 Then
              For j% = relative To -1
                 DataGrid1.SelBooks.Add (DataGrid1.GetBook(j%))
              Next j%
            Else
              For j% = 1 To relative
                 DataGrid1.SelBooks.Add (DataGrid1.GetBook(j%))
              Next j%
            End If
        Else
            
            last_sel = DataGrid1.Row
            Text1.Text = last_sel
        End IfEnd Sub
    详细信息请参考:
    Add Method (Columns, SelBooks, Splits Collections)
    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/DBGrid98/html/damthinsert.asp
    SelBooks Collection
    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/DBGrid98/html/daobjselbooks.asp
    GetKeyState 
    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/keybinpt_4z51.asp
    - 微软全球技术中心 VB技术支持本贴子以“现状”提供且没有任何担保,同时也没有授予任何权利。具体事项可参见使用条款
    (http://support.microsoft.com/directory/worldwide/zh-cn/community/terms_chs.asp)。
    为了为您创建更好的讨论环境,请参加我们的用户满意度调查
    (http://support.microsoft.com/directory/worldwide/zh-cn/community/survey.asp?key=(S,49854782))。