| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 5566 人关注过本帖, 3 人收藏
标题:分享:在表格grid控件中增加复选框checkbox的方法(更新:增加grid中“鼠标 ...
只看楼主 加入收藏
zxl931
Rank: 2
等 级:论坛游民
帖 子:31
专家分:42
注 册:2007-1-8
结帖率:33.33%
收藏(3)
已结贴  问题点数:20 回复次数:12 
分享:在表格grid控件中增加复选框checkbox的方法(更新:增加grid中“鼠标+键盘”选择行记录)
grid添加复选框及选择记录.rar (8.49 KB)

分享1:在表格grid控件中增加复选框checkbox的方法
图片附件: 游客没有浏览图片的权限,请 登录注册

在命令窗口运行以下代码看效果:
说明:只有鼠标在checkbox上点击时才选中,点击其他地方无效。

程序代码:

PUBLIC oform1

oform1=NEWOBJECT("form1")
oform1.Show
RETURN


*-- 时间戳:   03/28/19 04:45:09 PM

DEFINE CLASS form1 AS form


        Height = 428
        Width = 461
        DoCreate = .T.
        AutoCenter = .T.
        Caption = "Form1"
        Name = "Form1"


        ADD OBJECT grid1 AS grid WITH ;
                ColumnCount = 3, ;
                DeleteMark = .F., ;
                Height = 384, ;
                Left = 24, ;
                RecordSource = "table1", ;
                Top = 24, ;
                Width = 421, ;
                AllowCellSelection = .F., ;
                Name = "Grid1", ;
                Column1.Alignment = 2, ;
                Column1.ControlSource = "", ;
                Column1.Sparse = .F., ;
                Column1.Name = "Column1", ;
                Column2.ControlSource = "", ;
                Column2.Name = "Column2", ;
                Column3.ControlSource = "", ;
                Column3.Name = "Column3"


        PROCEDURE Load
                CREATE CURSOR Table1 (Fselect L, Fname C(10), Fage N(3))

                INSERT INTO Table1(Fselect , Fname , Fage) VALUES (.T., "姓名1", 30)
                INSERT INTO Table1(Fselect , Fname , Fage) VALUES (.T., "姓名2", 60)
                INSERT INTO Table1(Fselect , Fname , Fage) VALUES (.T., "姓名3", 41)
                INSERT INTO Table1(Fselect , Fname , Fage) VALUES (.T., "姓名4", 25)
                INSERT INTO Table1(Fselect , Fname , Fage) VALUES (.T., "姓名5", 10)
                INSERT INTO Table1(Fselect , Fname , Fage) VALUES (.T., "姓名6", 28)
                INSERT INTO Table1(Fselect , Fname , Fage) VALUES (.T., "姓名7", 40)
                INSERT INTO Table1(Fselect , Fname , Fage) VALUES (.T., "姓名8", 20)
                INSERT INTO Table1(Fselect , Fname , Fage) VALUES (.T., "姓名9", 25)
                INSERT INTO Table1(Fselect , Fname , Fage) VALUES (.T., "姓名10",30)

                GO TOP 
        ENDPROC


        PROCEDURE init        
            thisform.grid1.Column1.addobject("check1","checkbox")
            thisform.grid1.column1.check1.AutoSize =.T.
            thisform.grid1.column1.check1.Centered = .T.
            thisform.grid1.column1.check1.Caption = ""
            thisform.grid1.column1.check1.BackStyle = 0
            thisform.grid1.column1.check1.Alignment = 2
                thisform.grid1.Column1.CurrentControl = "Check1"
        ENDPROC


        PROCEDURE grid1.Click
                Local lnwhere, locheckbox, lnleftchk,llclickchkbox
                Dimension lamouseinfo[1]

                m.llclickchkbox = .F.
                *--------------------------------
                * 检测点击时鼠标所在表格的位置
                *--------------------------------
                * 检测鼠标位置,aMouseInfo第三和第四行包含鼠标指针相对于当前表单的坐标
                Amouseobj(lamouseinfo,1)
                * lnWhere=3 在表格单元格
                If Thisform.grid1.GridHitTest(lamouseinfo[3],lamouseinfo[4], @lnwhere) And lnwhere=3
                        m.locheckbox    = Thisform.grid1.column1.check1
                        m.lnleftchk     = Objtoclient(m.locheckbox,2)  &&返回复选框控件相对于表单的左边距
                        m.llclickchkbox = Between(lamouseinfo[3], m.lnleftchk, m.lnleftchk + locheckbox.Width)  &&鼠标是否在复选框上
                Endif


                If m.llclickchkbox
                    SELECT table1
                        Replace fSelect With !fSelect &&如果点击checkbox更新选择状态
                Endif
        ENDPROC


ENDDEFINE


---------------------------------------------------------
分享2:在grid中“鼠标+键盘”选择行记录的方法
图片附件: 游客没有浏览图片的权限,请 登录注册

在命令窗口运行以下代码看效果:
说明:选择方式:Ctrl+鼠标单击,Ctrl+鼠标拖动,shift+鼠标单击。
程序代码:
PUBLIC oform1

oform1=NEWOBJECT("form1")
oform1.SHOW
RETURN


*-- 时间戳:   03/29/19 11:33:09 AM

DEFINE CLASS form1 AS FORM


    HEIGHT = 428
    WIDTH = 461
    DOCREATE = .T.
    AUTOCENTER = .T.
    CAPTION = "Form1"
    NAME = "Form1"


    ADD OBJECT grid1 AS GRID WITH ;
        COLUMNCOUNT = 3, ;
        DELETEMARK = .F., ;
        HEIGHT = 384, ;
        LEFT = 24, ;
        RECORDSOURCE = "table1", ;
        TOP = 24, ;
        WIDTH = 421, ;
        ALLOWCELLSELECTION = .F., ;
        NAME = "Grid1", ;
        highlightbackcolor = RGB(182,202,234),;
        highlightforecolor = 0,;
        column1.ALIGNMENT = 2, ;
        column1.CONTROLSOURCE = "", ;
        column1.SPARSE = .F., ;
        column1.NAME = "Column1", ;
        column2.CONTROLSOURCE = "", ;
        column2.NAME = "Column2", ;
        column3.CONTROLSOURCE = "", ;
        column3.NAME = "Column3"


    PROCEDURE LOAD
        CREATE CURSOR table1 (fselect l, fname c(10), fage N(3))

        INSERT INTO table1(fselect , fname , fage) VALUES (.T., "姓名01",30)
        INSERT INTO table1(fselect , fname , fage) VALUES (.T., "姓名02",60)
        INSERT INTO table1(fselect , fname , fage) VALUES (.T., "姓名03",41)
        INSERT INTO table1(fselect , fname , fage) VALUES (.T., "姓名04",25)
        INSERT INTO table1(fselect , fname , fage) VALUES (.T., "姓名05",10)
        INSERT INTO table1(fselect , fname , fage) VALUES (.T., "姓名06",28)
        INSERT INTO table1(fselect , fname , fage) VALUES (.T., "姓名07",40)
        INSERT INTO table1(fselect , fname , fage) VALUES (.T., "姓名08",20)
        INSERT INTO table1(fselect , fname , fage) VALUES (.T., "姓名09",25)
        INSERT INTO table1(fselect , fname , fage) VALUES (.T., "姓名10",55)
        INSERT INTO table1(fselect , fname , fage) VALUES (.T., "姓名11",16)
        INSERT INTO table1(fselect , fname , fage) VALUES (.T., "姓名12",38)
        INSERT INTO table1(fselect , fname , fage) VALUES (.T., "姓名13",44)
        INSERT INTO table1(fselect , fname , fage) VALUES (.T., "姓名14",60)
        INSERT INTO table1(fselect , fname , fage) VALUES (.T., "姓名15",29)
        INSERT INTO table1(fselect , fname , fage) VALUES (.T., "姓名16",68)
        INSERT INTO table1(fselect , fname , fage) VALUES (.T., "姓名17",80)
        INSERT INTO table1(fselect , fname , fage) VALUES (.T., "姓名18",50)
        INSERT INTO table1(fselect , fname , fage) VALUES (.T., "姓名19",22)
        
        GO TOP
    ENDPROC


    PROCEDURE INIT
        THISFORM.grid1.column1.ADDOBJECT("check1","checkbox")
        THISFORM.grid1.column1.CURRENTCONTROL = "Check1"
        THISFORM.grid1.column1.check1.AUTOSIZE =.T.
        THISFORM.grid1.column1.check1.CENTERED = .T.
        THISFORM.grid1.column1.check1.CAPTION = ""
        THISFORM.grid1.column1.check1.BACKSTYLE = 0
        THISFORM.grid1.column1.check1.ALIGNMENT = 2
        
        THISFORM.grid1.column1.header1.caption = "选择"
        THISFORM.grid1.column2.header1.caption = "姓名"
        THISFORM.grid1.column3.header1.caption = "年龄"
        
        THISFORM.ADDOBJECT("TEMPGOTFOCUSTEXTBOX","TextBox")

        THIS.ADDPROPERTY("MouseState","")
        THIS.ADDPROPERTY("KeyPressState","")
        THIS.ADDPROPERTY("nStartRec",0)
        THIS.ADDPROPERTY("nEndRec",0)
        THIS.ADDPROPERTY("nMinRecno",0)
        THIS.ADDPROPERTY("nMaxRecno",0)

        LOCAL lcselitembackcolor,lcunselitembackcolor,lcstr
        WITH THISFORM.grid1.column1
            lcselitembackcolor   = "RGB(182,202,234)"
            lcunselitembackcolor = TRANSFORM(THISFORM.grid1.BACKCOLOR)
            lcstr="IIF(fSelect," + lcselitembackcolor + "," + lcunselitembackcolor + ")"
            .PARENT.SETALL("DynamicBackColor",lcstr,"COLUMN")
        ENDWITH

    ENDPROC
    
    

    PROCEDURE grid1.MOUSEDOWN
        LPARAMETERS nbutton, nshift, nxcoord, nycoord

        LOCAL lccursor,llonselect,lnwhere_out,lnrow_out,lncol_out,lnoldendrec,lnrecno
        LOCAL ARRAY laselid(1)

        m.lccursor="table1"

        SELECT COUNT(*) ;
            FROM (m.lccursor) ;
            WHERE NOT DELETED() ;
            INTO ARRAY nreccount

        IF nreccount(1)=0
            RETURN
        ENDIF

        THIS.GRIDHITTEST(nxcoord,nycoord,@lnwhere_out,@lnrow_out,@lncol_out)

        SELECT COUNT(*) FROM (m.lccursor) WHERE fselect INTO ARRAY selid

        WITH THISFORM
            DO CASE
                CASE nbutton=2 AND nshift=0  &&右击鼠标
                    m.llonselect=EVALUATE(m.lccursor+".fSelect")
                    m.lnrecno=RECNO()
                    DO CASE
                        CASE m.llonselect  AND selid(1)#0    &&在选中区,并且之前是选中操作
                            .tempgotfocustextbox.SETFOCUS
                            .mousestate="RC_InSel"
                        CASE !m.llonselect AND selid(1)#0  &&不在选中区,并且之前是选中操作
                            UPDATE (m.lccursor) SET fselect=.F. WHERE fselect
                            .mousestate="RC_OutSel"
                            GO m.lnrecno IN  (m.lccursor)
                        CASE !m.llonselect AND selid(1)=0
                            .mousestate="RC_NoSel"
                    ENDCASE

                CASE nbutton=1 AND nshift=0 AND lnwhere_out=3 &&左击鼠标

                    .nstartrec=RECNO(m.lccursor)
                    .nendrec=0

                    UPDATE (m.lccursor) SET fselect=.F. WHERE fselect
                    GO .nstartrec IN (m.lccursor)

                    .mousestate="LC_OutSel"

                CASE nbutton=1 AND nshift=1    &&Shift+单击鼠标
                    m.lnoldendrec =.nendrec
                    .nendrec=RECNO(m.lccursor)

                    IF NOT INLIST(.mousestate,"shift+leftclick","ctrl+leftclick")
                        UPDATE (m.lccursor) SET fselect=.F. WHERE fselect
                    ENDIF

                    IF .nendrec#0
                        UPDATE (m.lccursor) SET fselect=.F. WHERE BETWEEN(RECNO(),MIN(m.lnoldendrec,.nstartrec),MAX(m.lnoldendrec,.nstartrec))
                    ENDIF


                    UPDATE (m.lccursor) SET fselect=.T. WHERE BETWEEN(RECNO(),MIN(.nendrec,.nstartrec),MAX(.nendrec,.nstartrec))

                    GO .nendrec IN (m.lccursor)
                    .mousestate="shift+leftclick"

                CASE    nbutton=1 AND nshift=2  &&Ctrl+单击鼠标

                    .nstartrec=RECNO(m.lccursor)
                    .nendrec=0

                    .nminrecno = RECNO(m.lccursor)
                    .nmaxrecno = RECNO(m.lccursor)
                    nchildid   = RECNO(m.lccursor)

                    IF NOT INLIST(.mousestate,"shift+leftclick","ctrl+leftclick")
                        UPDATE (m.lccursor) SET fselect=.F. WHERE fselect
                    ENDIF


                    UPDATE (m.lccursor) SET fselect=!fselect WHERE RECNO()=nchildid

                    GO .nstartrec IN (m.lccursor)

                    .mousestate="ctrl+leftclick"

            ENDCASE

            IF nbutton=1 AND INLIST(nshift,1,2)
                .tempgotfocustextbox.SETFOCUS
            ENDIF

            SELECT (m.lccursor) &&此语句很重要,曾出现不能一次单击选中的问题

        ENDWITH

    ENDPROC
    
    
    PROCEDURE grid1.MOUSEMOVE
        LPARAMETERS nbutton, nshift, nxcoord, nycoord


        LOCAL lnwhere_out,lnrow_out,lncol_out

        LOCAL lccursor

        m.lccursor="table1"

        WITH THISFORM
            IF  nbutton=1 AND nshift=2 &&INLIST(nShift,0,2)
                .grid1.GRIDHITTEST(nxcoord,nycoord,@lnwhere_out,@lnrow_out,@lncol_out)
                .grid1.ACTIVATECELL(lnrow_out,lncol_out)

                .nendrec   = RECNO(m.lccursor)

                .nminrecno = MIN(.nendrec,.nstartrec,.nminrecno)
                .nmaxrecno = MAX(.nendrec,.nstartrec,.nmaxrecno)

                *将之前选中区域全部取消
                UPDATE (m.lccursor) SET fselect = .F. WHERE BETWEEN(RECNO(),.nminrecno,.nmaxrecno)

                *更新选中区域
                UPDATE (m.lccursor) SET fselect = .T. WHERE BETWEEN(RECNO(),MIN(.nendrec,.nstartrec),MAX(.nendrec,.nstartrec))

                GO .nendrec IN  (m.lccursor)

                .tempgotfocustextbox.SETFOCUS

                *thisform.oCurrentEditTree=this

            ENDIF
        ENDWITH
    ENDPROC

ENDDEFINE


[此贴子已经被作者于2019-3-29 14:12编辑过]

搜索更多相关主题的帖子: INSERT INTO VALUES 姓名 thisform 
2019-03-28 17:29
sdta
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
来 自:江苏省连云港市
等 级:版主
威 望:335
帖 子:9841
专家分:27213
注 册:2012-2-5
收藏
得分:4 
雨田作品均为精品
收藏学习

坚守VFP最后的阵地
2019-03-28 19:02
schtg
Rank: 12Rank: 12Rank: 12
来 自:Usa
等 级:贵宾
威 望:67
帖 子:1740
专家分:3329
注 册:2012-2-29
收藏
得分:4 
正如楼上所说,江南雨田作品必为精品!
收藏学习,谢谢!
2019-03-28 21:06
isealer
Rank: 3Rank: 3
等 级:论坛游侠
威 望:4
帖 子:33
专家分:110
注 册:2017-8-25
收藏
得分:4 
赞!标记一下,多谢分享。
2019-03-29 08:20
wengjl
Rank: 14Rank: 14Rank: 14Rank: 14
等 级:贵宾
威 望:109
帖 子:2215
专家分:3882
注 册:2007-4-27
收藏
得分:4 
谢谢!学习收藏

只求每天有一丁点儿的进步就可以了
2019-03-29 08:38
yewxj
Rank: 3Rank: 3
等 级:论坛游侠
威 望:5
帖 子:157
专家分:153
注 册:2015-6-18
收藏
得分:4 
收藏学习,谢谢!
2019-03-29 09:02
wangzhiyi
Rank: 7Rank: 7Rank: 7
等 级:贵宾
威 望:34
帖 子:366
专家分:684
注 册:2014-4-9
收藏
得分:4 
收藏学习,谢谢!
2019-03-29 10:11
schtg
Rank: 12Rank: 12Rank: 12
来 自:Usa
等 级:贵宾
威 望:67
帖 子:1740
专家分:3329
注 册:2012-2-29
收藏
得分:0 
@江南雨田,高!
2019-03-29 13:32
movie001
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2012-11-25
收藏
得分:0 
回复 楼主 zxl931
学习学习
2019-04-07 15:20
星光悠蓝
Rank: 9Rank: 9Rank: 9
来 自:山水甲天下
等 级:贵宾
威 望:52
帖 子:525
专家分:1278
注 册:2010-1-11
收藏
得分:0 
值得收藏!
2019-04-08 09:58
快速回复:分享:在表格grid控件中增加复选框checkbox的方法(更新:增加grid中“ ...
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.035928 second(s), 11 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved