1

你好朋友我的新项目有一个大问题,如何通过在 vb.net 中编码来创建对象和新创建的对象事件

这是我的代码我可以用数组创建对象但我不能应用任何事件

    Dim C_R_ARRY(2) As TableLayoutPanel
    For x As Integer = 2 To 0 Step -1

        ' part 1
        Dim C_A_LABEL_1(2) As Label
        Dim C_A_LABEL_1_VALUE() As String = {"ADAM", "SOLY", "HESHAM"}
        C_A_LABEL_1(x) = New Label
        C_A_LABEL_1(x).Text = C_A_LABEL_1_VALUE(x)
        C_A_LABEL_1(x).Margin = New System.Windows.Forms.Padding(0)
        C_A_LABEL_1(x).TextAlign = System.Drawing.ContentAlignment.MiddleLeft
        C_A_LABEL_1(x).Dock = System.Windows.Forms.DockStyle.Fill

        ' part 1
        Dim C_A_LABEL_2(2) As Label
        Dim C_A_LABEL_2_VALUE() As String = {"122", "231", "102"}
        C_A_LABEL_2(x) = New Label
        C_A_LABEL_2(x).Text = C_A_LABEL_2_VALUE(x) & " +"
        C_A_LABEL_2(x).Margin = New System.Windows.Forms.Padding(0)
        C_A_LABEL_2(x).TextAlign = System.Drawing.ContentAlignment.MiddleLeft
        C_A_LABEL_2(x).Dock = System.Windows.Forms.DockStyle.Fill

        ' part 3
        Dim C_A_TEXT(2) As TextBox
        Dim C_A_TEXT_ID() As Integer = {1, 2, 3}
        C_A_TEXT(x) = New TextBox
        C_A_TEXT(x).Width = 100
        C_A_TEXT(x).Name = "TEXT" & x

        ' part 0
        C_R_ARRY(x) = New TableLayoutPanel
        C_R_ARRY(x).AutoSize = True
        C_R_ARRY(x).ColumnCount = 6
        C_R_ARRY(x).ColumnStyles.Add(New System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 140.0!))
        C_R_ARRY(x).ColumnStyles.Add(New System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 140.0!))
        C_R_ARRY(x).ColumnStyles.Add(New System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 140.0!))
        C_R_ARRY(x).ColumnStyles.Add(New System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 140.0!))
        C_R_ARRY(x).ColumnStyles.Add(New System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 140.0!))
        C_R_ARRY(x).ColumnStyles.Add(New System.Windows.Forms.ColumnStyle())
        C_R_ARRY(x).Dock = System.Windows.Forms.DockStyle.Top
        C_R_ARRY(x).Location = New System.Drawing.Point(0, 0)
        C_R_ARRY(x).Margin = New System.Windows.Forms.Padding(0)
        C_R_ARRY(x).RowCount = 1
        C_R_ARRY(x).RowStyles.Add(New System.Windows.Forms.RowStyle())
        C_R_ARRY(x).Size = New System.Drawing.Size(620, 100)
        C_R_ARRY(x).TabIndex = 0
        C_R_ARRY(x).Padding = New System.Windows.Forms.Padding(5, 5, 5, 10)



        C_R_ARRY(x).Controls.Add(C_A_LABEL_1(x), 0, 0)
        C_R_ARRY(x).Controls.Add(C_A_LABEL_2(x), 1, 0)
        C_R_ARRY(x).Controls.Add(C_A_TEXT(x), 2, 0)

        Panel3.Controls.Add(C_R_ARRY(x))

    Next

    Private Sub TEXT1_TextChanged(sender As Object, e As EventArgs) Handles = TEXT1.TextChanged
        ' not working
    End Sub

谢谢你的时间 :)

4

1 回答 1

2

您应该删除Handles方法声明的部分,并用于AddHandler动态订阅事件:

AddHandler C_A_TEXT(x).TextChanged, AddressOf TEXT1_TextChanged

我还强烈建议您使用更传统(不那么喧闹)的变量名称。此外,如果您避免完全限定所有名称,您的代码将更加简洁。所以这:

C_R_ARRY(x).ColumnStyles.Add(New System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 140.0!))

...可能会变成:

panels(x).ColumnsStyles.Add(New ColumnStyle(SizeType.Absolute, 140.0F))

(我使用F了而不是!作为Single后缀,只是因为它对我作为 C# 程序员来说看起来更自然。我不知道在 VB 代码中哪个更常见。)

于 2013-02-24T00:12:55.260 回答