以文本方式查看主题

-  Foxtable(狐表)  (http://foxtable.net/bbs/index.asp)
--  专家坐堂  (http://foxtable.net/bbs/list.asp?boardid=2)
----  [求助]循环增加控件  (http://foxtable.net/bbs/dispbbs.asp?boardid=2&id=66784)

--  作者:moxuejiahui
--  发布时间:2015/4/13 21:15:00
--  [求助]循环增加控件
我在窗口中希望达到循环增加控件要怎么弄呢,帮我看看下面的代码吧,为什么只增加一个呢
Dim cz As WinForm.ComboBox = e.Form.Controls("ComboBox1")
Dim jjk As WinForm.TextBox = e.Form.Controls("TextBox6")
Dim fs As WinForm.ComboBox = e.Form.Controls("ComboBox2")

If cz.text = "" Then
    MessageBox.show("请先选择材质","提示")
Else
    If fs.text = "横裁" Then
        Dim maxc As Integer
        Dim maxk As Integer
        maxc = DataTables("规格需求").Compute("max(长)","材质 =\'" & cz.text & "\' and 剩余欠量 > 0 and 不套裁 = false")
        maxk = DataTables("规格需求").Compute("max(宽)","长 = "& maxc &" And 剩余欠量 > 0 And 不套裁 = false")
        Dim dc As DataRow = DataTables("规格需求").find("长 = "& maxc &" And 宽 = "& maxk &"")
        Dim xq As Integer = dc("剩余欠量")
        Dim hengchu1 As Integer = Int(1200 / (maxk + 4))
        Dim Ary() As Integer = {xq,hengchu1}
        Array.Sort(ary)
        Dim sas As Integer = ary(0)
        Dim i As Integer
        For i = 1 To sas
            Dim x1 As Integer = maxc / 3.5
            Dim x2 As Integer = maxk / 3.5
            Dim wzx As Integer = 5 + (i - 1) * (maxc + 1)
            Dim Panel1 As WinForm.Panel
            Panel1 = e.Form.CreateControl("Panel1", ControlTypeEnum.Panel)
            Panel1.SetBounds(wzx, 5, x2, x1)
            e.Form.Controls("SplitContainer1").Panel2.AddControl(Panel1)
            e.Form.Controls("Panel1").BackColor = Color.Orangered
            Dim lbl As WinForm.Label
            lbl = e.Form.CreateControl("Label1", ControlTypeEnum.Label)
            Dim gg1 As String = maxc & "*" & maxk
            lbl.Text = gg1
            lbl.Left = x2 / 2 - 30
            lbl.Top = x1 / 2 - 15
            e.Form.Controls("Panel1").AddControl(lbl)
            Dim nd As DataRow = DataTables("临时").addnew()
            nd("规格编号") = dc("规格编号")
            nd("数量") = 1
            Dim syh1 As Integer = 1
        Next
    End If
End If

--  作者:有点酸
--  发布时间:2015/4/13 21:20:00
--  

1、你用messagebox.show(sas),显示sas的值,如果是1,肯定增加一个

2、不是不是1,肯定增加不止1个,可能你增加的控件位置重叠,看起来只有一个

3、增加完成之后,你可以用e.form.Controls.Count,看看总的控件数量


--  作者:moxuejiahui
--  发布时间:2015/4/13 21:26:00
--  
不是1,因为增加三行,控件位置是随i变化的,所以不应该是重叠啊,我考虑会不会是因为增加了第一个后有了控件名所以在循环中就不能增加相同名称的控件了啊,可是这样的话要怎么定义控件名也随循环变动呢


--  作者:moxuejiahui
--  发布时间:2015/4/13 21:27:00
--  
因为我设置了两个按钮,代码相同,但是控件位置有改变,分别点击后却只能增加一个控件,这样看的话应该是控件名重复的原因了
--  作者:moxuejiahui
--  发布时间:2015/4/13 21:33:00
--  
哪位大大再帮我看看啊
--  作者:客人
--  发布时间:2015/4/13 21:45:00
--  
这样改的话是不是能够形成动态控件名了啊,可是还是增加一个
 For i = 1 To sas
            Dim x1 As Integer = maxc / 3.5
            Dim x2 As Integer = maxk / 3.5
            Dim wzx As Integer = 5 + (i - 1) * (maxc + 1)
            Dim Panel1 As WinForm.Panel
            Panel1 = e.Form.CreateControl("Panel"& i &"", ControlTypeEnum.Panel)
            Panel1.SetBounds(wzx, 5, x2, x1)
            e.Form.Controls("SplitContainer1").Panel2.AddControl(Panel1)
            e.Form.Controls("Panel"& i &"").BackColor = Color.Orangered
            Dim lbl As WinForm.Label
            lbl = e.Form.CreateControl("Label"& i &"", ControlTypeEnum.Label)
            Dim gg1 As String = maxc & "*" & maxk
            lbl.Text = gg1
            lbl.Left = x2 / 2 - 30
            lbl.Top = x1 / 2 - 15
            e.Form.Controls("Panel"& i &"").AddControl(lbl)
            Dim nd As DataRow = DataTables("临时").addnew()
            nd("规格编号") = dc("规格编号")
            nd("数量") = 1
            Dim syh1 As Integer = 1
        Next


--  作者:ybil
--  发布时间:2015/4/13 21:56:00
--  
这样?
Dim cz As WinForm.ComboBox = e.Form.Controls("ComboBox1")
Dim jjk As WinForm.TextBox = e.Form.Controls("TextBox6")
Dim fs As WinForm.ComboBox = e.Form.Controls("ComboBox2")

If cz.text = "" Then
    MessageBox.show("请先选择材质","提示")
Else
    If fs.text = "横裁" Then
        Dim maxc As Integer
        Dim maxk As Integer
        maxc = DataTables("规格需求").Compute("max(长)","材质 =\'" & cz.text & "\' and 剩余欠量 > 0 and 不套裁 = false")
        maxk = DataTables("规格需求").Compute("max(宽)","长 = "& maxc &" And 剩余欠量 > 0 And 不套裁 = false")
        Dim dc As DataRow = DataTables("规格需求").find("长 = "& maxc &" And 宽 = "& maxk &"")

        Dim gg1 As String = maxc & "*" & maxk
        Dim x1 As Integer = maxc / 3.5
        Dim x2 As Integer = maxk / 3.5
        Dim wzx As Integer = 5 + (i - 1) * (maxc + 1)

        Dim xq As Integer = dc("剩余欠量")
        Dim hengchu1 As Integer = Int(1200 / (maxk + 4))
        Dim Ary() As Integer = {xq,hengchu1}
        Array.Sort(ary)
        Dim sas As Integer = ary(0)

            
        Dim Panel1 As WinForm.Panel
        Dim lbl As WinForm.Label
    
        For i As Integer = 1 To sas            
            Panel1 = e.Form.CreateControl("Panel" & i, ControlTypeEnum.Panel)
            Panel1.SetBounds(wzx, 5, x2, x1)
            e.Form.Controls("SplitContainer1").Panel2.AddControl(Panel1)
            e.Form.Controls("Panel1").BackColor = Color.Orangered
            
            lbl = e.Form.CreateControl("Label" & i, ControlTypeEnum.Label)
            lbl.Text = gg1
            lbl.Left = x2 / 2 - 30
            lbl.Top = x1 / 2 - 15
            e.Form.Controls("Panel1").AddControl(lbl)
            Dim nd As DataRow = DataTables("临时").addnew()
            nd("规格编号") = dc("规格编号")
            nd("数量") = 1
            \'Dim syh1 As Integer = 1
        Next
    End If
End If

--  作者:有点酸
--  发布时间:2015/4/13 21:56:00
--  

肯定不会只增加一个,你在代码的最前面和最后面用Messagebox.show(e.Form.controls.count),看看执行前和执行后,控件数量的变化。

搞不定就做个简单例子发上来。


--  作者:客人
--  发布时间:2015/4/13 22:02:00
--  
呵呵,是我高错了,容器尺寸小,所以不显示增加的控件
这里应该/3.5,呵呵
谢谢各位
Dim wzx As Integer = 5 + ((i - 1) * (maxk + 4)) / 3.5