访问vb.net中的新动态控件

时间:2015-12-13 13:29:53

标签: vb.net controls

首先请原谅我糟糕的语法和词汇:)

请查看此来源并运行它:

Public Class Form1
Public pointX As Integer
Public pointY As Integer = 32
Public dynamicText As TextBox
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    pointX = 330
    For i = 1 To 4
        dynamicText = New Windows.Forms.TextBox
        dynamicText.Name = "T" + Trim(Str(i))
        dynamicText.Text = ""
        dynamicText.Location = New Point(pointX, pointY)
        dynamicText.Size = New Size(100, 20)
        Me.Controls.Add(dynamicText)
        pointX = pointX - 106
    Next
End Sub

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    pointX = 330
    pointY = pointY + 26
    For i = 1 To 4
        dynamicText = New Windows.Forms.TextBox
        dynamicText.Name = "T" + Trim(Str(i))
        dynamicText.Text = ""
        dynamicText.Location = New Point(pointX, pointY)
        dynamicText.Size = New Size(100, 20)
        Me.Controls.Add(dynamicText)
        pointX = pointX - 106

        AddHandler dynamicText.Click, AddressOf printHello1
    Next
End Sub

Private Sub printHello1(ByVal sender As System.Object, ByVal e As System.EventArgs)
    MsgBox(dynamicText.Name)
    If dynamicText.Name = "T1" Then MsgBox("Oh! this is T1")
End Sub
End Class

为什么如果永远不是真的?! 为什么MsgBox(dynamicText.Name)总是返回T4 ?! 我希望所有控件都可以通过名称或数组来访问。 请帮帮我谢谢。 :)

2 个答案:

答案 0 :(得分:0)

全局变量dynamicText获取Button1_Click事件中循环中添加的最后一个TextBox的值。这恰好是名为T4的控件。在这种情况下,您实际上并不需要全局变量。您可以将sender参数强制转换为TextBox实例,因为sender参数是引发事件的控件。

Private Sub printHello1(ByVal sender As System.Object, ByVal e As System.EventArgs)
    Dim txt = CType(sender, "TextBox")
    if txt IsNot Nothing then
        MsgBox(txt.Name)
        If txt.Name = "T1" Then MsgBox("Oh! this is T1")
    End If
End Sub

您也无需在按钮单击事件中再次重新创建控件。在表单加载事件中执行的操作就足够了(您可以在那里添加AddHandler)。全局变量是危险的,尽可能避免使用它们。

答案 1 :(得分:0)

看看这是否可以接受。将一个面板放在表单底部,将Dock设置为Bottom,向面板添加一个按钮和一个TextBox。将FlowLayoutPanel放在窗体上,Dock = Fill,AutoScroll = True。

下面的代码创建了输入到TextBox中的TextBox控件的数量。每个新创建的TextBox点击事件都添加了简单的逻辑。

表格代码

Public Class Form1
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim count As Integer = 0
        If Integer.TryParse(TextBox1.Text, count) Then
            Dim demo = New TextBoxCreate(FlowLayoutPanel1, "Demos", count)
            demo.CreateTextBoxes()
        End If
    End Sub
End Class

类代码(向项目添加一个新类,将其命名为TextBoxCreate.vb)

Public Class TextBoxCreate
    Public Property TextBoxes As TextBox()
    Public Property TextBoxBaseName As String
    Public Property TextBoxCount As Integer
    Public Property ParentControl As Control

    Public Sub New(
        ByVal ParentControl As Control,
        ByVal BaseName As String,
        ByVal Count As Integer)

        Me.ParentControl = ParentControl
        Me.TextBoxBaseName = BaseName
        Me.TextBoxCount = Count
    End Sub
    Public Sub CreateTextBoxes()
        Dim Base As Integer = 10
        TextBoxes = Enumerable.Range(0, TextBoxCount).Select(
            Function(Indexer)

                Dim b As New TextBox With
                    {
                        .Name = String.Concat(TextBoxBaseName, Indexer + 1),
                        .Text = (Indexer + 1).ToString,
                        .Width = 150,
                        .Location = New Point(25, Base),
                        .Parent = Me.ParentControl,
                        .Visible = True
                    }

                AddHandler b.Click, Sub(sender As Object, e As EventArgs)
                                        Dim tb As TextBox = CType(sender, TextBox)
                                        If tb.Name = TextBoxBaseName & "1" Then
                                            tb.Text = "Got it"
                                        Else
                                            MessageBox.Show(tb.Name)
                                        End If

                                    End Sub
                Me.ParentControl.Controls.Add(b)

                Base += 30

                Return b

            End Function).ToArray
    End Sub
End Class