以文本方式查看主题

-  Foxtable(狐表)  (http://www.foxtable.com/bbs/index.asp)
--  专家坐堂  (http://www.foxtable.com/bbs/list.asp?boardid=2)
----  求助:动态创建CreateControl后如何设置其click代码?  (http://www.foxtable.com/bbs/dispbbs.asp?boardid=2&id=107103)

--  作者:huangxueyao
--  发布时间:2017/9/21 11:35:00
--  求助:动态创建CreateControl后如何设置其click代码?
用CreateControl创建了一个button,然后想要设置的button的click方法里面的代码,应该怎么操作?


--  作者:有点甜
--  发布时间:2017/9/21 12:36:00
--  

 把代码写到窗口的click事件里

 

 如果要动态编译代码,参考 http://www.foxtable.com/webhelp/scr/1487.htm

 


--  作者:huangxueyao
--  发布时间:2017/9/21 12:50:00
--  
我是在一个窗口中动态生成多个button控件的,所以直接给窗口加click不合适。

\'判断分拣大类
If vars("分拣类别")="蔬菜" Then
    
    \'建立符合状态的订单集合
    Dim drs1 As List(of DataRow)
    drs1 = DataTables("订单表").Select("订单状态 = \'PMC已确认\'")
    
    Dim x As Integer = 1 \'名称后缀初始值
    Dim y As Integer = 20 \'水平位置初始值
    Dim z As Integer = 10 \'垂直位置初始值
    Dim k As Single = e.Form.Controls("SplitContainer1").Panel2.Width \'分栏的宽度
    
    \'遍历订单
    For Each dr1 As DataRow In drs1
        \'查询对应的大类
        Dim dr1_1 As DataRow = DataTables("基础资料表").find("对应大类 = \'" & dr1("物料类别") & "\'")
        \'如果不符合,跳过
        If dr1_1("分拣部") <> "蔬菜" Then
            Continue For
            \'如果符合
        Else
            \'遍历控件判断是否已存在对应的控件
            Dim n As Integer = 0 \'定义变量判断是否存在对应控件
            For Each c As WinForm.Control In e.Form.Controls
                If Typeof c Is WinForm.Button Then \'判断控件是否是文本框
                    Dim b As WinForm.Button = c \'使用特定类型的变量引用控件
                    If b.Text = dr1("物料名称")
                        n = 1
                    End If
                End If
            Next
            \'如果存在,则跳过
            If n = 1 Then
                Continue For
            Else                \'如果不存在,则新建控件
                Dim but As WinForm.Button
                but = e.Form.CreateControl("Button" & x , ControlTypeEnum.Button)
                x=x+1
                If y + 200 > k Then \'这里的200等于宽度加Y的初始值20
                    y = 20
                    z = z + 70
                    but.SetBounds(y, z, 180, 60) \'统一设置位置与大小(水平,垂直,宽度,高度)
                Else
                    but.SetBounds(y, z, 180, 60) \'统一设置位置与大小(水平,垂直,宽度,高度)
                    y = y+200            
                End If
                
                but.Text = dr1("物料名称")  \'显示内容
                but.ReadOnly = True \'设置为只读
                but.TextAlign = HorizontalAlignment.Center \'对齐方式
                but.Font = New Font("宋体",16)
                but.BackColor = Color.white
                but.FlatStyle = FlatStyle.Flat
                but.BorderSize = 2
                but.BorderColor = Color.red
                \'but.Enabled = False
                e.Form.Controls("SplitContainer1").Panel2.AddControl(but)
            End If
        End If
    Next
End If


但是每个button的click事件代码是一样的,都是点击打开某个窗口,请问是否可以通过自定义函数来实现?

具体代码应该怎么写?

--  作者:有点甜
--  发布时间:2017/9/21 13:23:00
--  

你在click事件可以判断控件的名字,然后实现你的操作,如

 

If e.Sender.Name = "Button1" Then

 

ElseIf e.Sender.Name Like = "Button*" then

 

End If