VB.net' textbox1未声明'因为它是在模块/表单加载期间创建的

时间:2016-08-03 14:03:26

标签: vb.net variables

我正在使用VB.net,编写Visual Basic应用程序。 我决定,而不是使用用户界面将元素拖放到我的表单上,我编写了它,我添加了一些按钮的事件,这些按钮指的是在模块中的按钮旁边创建的多个文本框,模块创建了它们,它运行表单。

到目前为止一切顺利,当我包含事件时,它们指的是尚未存在但肯定会存在现有的文本框,它拒绝运行代码。

任何帮助表示感谢。

这是创建元素并引用Event的代码。 它指的是事件本身' AddHandler btnArray(n)。Click,AddressOf SetupWindow.OK_Click'这是唯一相关的部分,我将该地块仅用于测试。

If SetupID = "SimpleArray" Then
    Dim btnArray(1) As System.Windows.Forms.Button
    For i As Integer = 0 To btnArray.GetUpperBound(0)
        btnArray(i) = New System.Windows.Forms.Button
    Next i
    For n = 0 To btnArray.GetUpperBound(0)
        With (btnArray(n))
            ' Location of button:            .Left = xPos
            ' Add buttons to a Panel:            
            SetupWindow.Controls.Add(btnArray(n)) ' Let panel hold the Buttons
            btnArray(n).TextAlign = ContentAlignment.MiddleCenter
            If n = 0 Then
                .Width = 55 ' Width of button
                .Height = 30 ' Height of button
                .Text = "OK"
                .Top = 325
                .Left = 175
                AddHandler btnArray(n).Click, AddressOf SetupWindow.OK_Click
            End If
            If n = 1 Then
                .Width = 55 ' Width of button
                .Height = 30 ' Height of button
                .Text = "Cancel"
                .Top = 325
                .Left = 50
                AddHandler btnArray(n).Click, AddressOf SetupWindow.Cancel_Click
            End If
            '.Text = Alphabet(n)
        End With
    Next

    Dim Combobox1 As System.Windows.Forms.ComboBox
    Combobox1 = New System.Windows.Forms.ComboBox
    SetupWindow.Controls.Add(Combobox1) ' Let panel hold the Buttons
    Combobox1.Width = 140 ' Width of button
    Combobox1.Height = 25 ' Height of button
    Combobox1.Top = 20
    Combobox1.Left = 16
    Combobox1.Items.Add("Write To The End Of File")
    Combobox1.Items.Add("Write to Current Location")
    Combobox1.Items.Add("Smart Wizard")
    Combobox1.SelectedIndex = 0
    '.Text = Alphabet(n)

    Dim TextBox1 As System.Windows.Forms.TextBox
    TextBox1 = New System.Windows.Forms.TextBox
    SetupWindow.Controls.Add(TextBox1) ' Let panel hold the Buttons
    TextBox1.Width = 120 ' Width of button
    TextBox1.Height = 25 ' Height of button
    TextBox1.Top = 60
    TextBox1.Left = 125

    Dim Label1 As System.Windows.Forms.Label
    Label1 = New System.Windows.Forms.Label
    SetupWindow.Controls.Add(Label1) ' Let panel hold the Buttons
    Label1.Width = 80
    Label1.Height = 25
    Label1.Top = 60
    Label1.Left = 16
    Label1.Text = "Constant"

    Dim TextBox2 As System.Windows.Forms.TextBox
    TextBox2 = New System.Windows.Forms.TextBox
    SetupWindow.Controls.Add(TextBox2) ' Let panel hold the Buttons
    TextBox2.Width = 120
    TextBox2.Height = 25
    TextBox2.Top = 100
    TextBox2.Left = 125

    Dim Label2 As System.Windows.Forms.Label
    Label2 = New System.Windows.Forms.Label
    SetupWindow.Controls.Add(Label2) ' Let panel hold the Buttons
    Label2.Width = 80
    Label2.Height = 25
    Label2.Top = 100
    Label2.Left = 16
    Label2.Text = "Equation"

    Dim RichTextBox3 As System.Windows.Forms.RichTextBox
    RichTextBox3 = New System.Windows.Forms.RichTextBox
    SetupWindow.Controls.Add(RichTextBox3) ' Let panel hold the Buttons
    RichTextBox3.Width = 120
    RichTextBox3.Height = 80
    RichTextBox3.Top = 140
    RichTextBox3.Left = 125

    Dim Label3 As System.Windows.Forms.Label
    Label3 = New System.Windows.Forms.Label
    SetupWindow.Controls.Add(Label3) ' Let panel hold the Buttons
    Label3.Width = 80
    Label3.Height = 25
    Label3.Top = 140
    Label3.Left = 16
    Label3.Text = "Initial Values"

    Dim TextBox4 As System.Windows.Forms.TextBox
    TextBox4 = New System.Windows.Forms.TextBox
    SetupWindow.Controls.Add(TextBox4) ' Let panel hold the Buttons
    TextBox4.Width = 50 ' Width of button
    TextBox4.Height = 25 ' Height of button
    TextBox4.Top = 240
    TextBox4.Left = 125

    Dim Label4 As System.Windows.Forms.Label
    Label4 = New System.Windows.Forms.Label
    SetupWindow.Controls.Add(Label4) ' Let panel hold the Buttons
    Label4.Width = 120
    Label4.Height = 25
    Label4.Top = 240
    Label4.Left = 16
    Label4.Text = "Number Of Attempts"

    SetupWindow.ShowDialog()
End If

事件存储在表单上,​​并且所有元素都标记为红色,因为它们尚未创建...只要我可以运行它就完全没问题但是使用vb,我只能运行最后一次构建。

Public Sub OK_Click(sender As Object, e As EventArgs)
    HandleSimpleArray(textbox1.text, textbox2.text, textbox3.text, textbox4.text)
End Sub

2 个答案:

答案 0 :(得分:0)

如果在表单上满足最大数量的文本框,按钮等,并在代码中删除它们,用编码元素替换它们,应该解决问题,但这只是浪费时间,不妨使用设计师。

答案 1 :(得分:0)

我相信如果您在AddHandler指令之前反转顺序并创建文本框,则可以解决它。

我不确定,但是如果运行时检查文本框之前创建的事件,则是拒绝使用指向消息运行的唯一原因(未找到文本框)。

我还建议在SETUP表单的LOAD(甚至NEW)事件中创建所有控件。我不明白为什么要用模块或其他形式创建它们。

试试吧......祝你好运。