我正在使用VB.net編寫Visual Basic應用程序。 我決定不使用用戶界面將元素拖放到我的表單上,而是編寫了它,我爲這些按鈕中的一些添加了事件,這些按鈕指向在模塊中的按鈕旁邊創建的多個文本框,一次該模塊創建了它們,它運行表單。VB.net'textbox1未聲明',因爲它是在模塊/表單加載期間創建的
到目前爲止這麼好,當我包含事件,那指的是不存在但肯定會存在的文本框,它拒絕運行代碼。
任何幫助表示讚賞謝謝。
這是創建元素並引用事件的代碼。 它指的是這樣的事件'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
你如何關聯事件? –
您需要提供一些代碼 – djv
您是否嘗試過[添加和刪除處理程序](https://msdn.microsoft.com/en-us/library/6yyk8z93(v = vs.90).aspx)? –