我计划在不久的将来创建一个关于测验的项目,现在我正在练习一些代码,这是我的问题。我在private sub Button_Click
内有一个变量。所有变量都可以正常工作但我无法在其他Private sub Button3_Click
中访问它,因为它是一个私有变量,因此我决定将它们变为公共变量。
然后当我运行它时,它只显示我数据库中的最后一项,然后我返回原始代码并显示数据库中的所有数据,当我检查它时,我从radiobutton创建一个事件自动插入数据进入我的桌子。所以我需要变量标签但我无法在我的活动中访问它。
注意:当数据库具有除panel1,button2和button3之外的数据时,我的所有控件都将以编程方式添加。
这是我的原始代码:
'This is for adding controls like labels and radiobutton dependeng in a number of data from database
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Try
Dim conn As OleDbConnection = GetDBConnection()
Dim cmd As New OleDbCommand("SELECT * FROM tblEcobags ORDER BY N ASC", conn)
cmd.Prepare()
Dim dataReader As OleDbDataReader = cmd.ExecuteReader()
Do While dataReader.Read
Dim Cpanel As New Panel()
Dim count As Integer = Panel1.Controls.OfType(Of Panel)().ToList().Count
count = Panel1.Controls.OfType(Of Panel)().ToList().Count
Cpanel.Location = New Point(10, (25 * count) * 2.2)
Cpanel.Size = New Size(537, 51)
Cpanel.Name = "Cpanel" & (count + 1)
Cpanel.BackColor = Color.White
Panel1.Controls.Add(Cpanel)
Dim label As New Label()
count = Cpanel.Controls.OfType(Of Label)().ToList().Count
label.Location = New Point(10, (25 * count))
label.AutoSize = True
label.Name = "label_" & (count + 1)
label.Text = dataReader.Item("N") & "."
label.Font = New Font(label.Font.FontFamily, 10, FontStyle.Bold)
Cpanel.Controls.Add(label)
Dim lblQ As New Label()
count = Cpanel.Controls.OfType(Of TextBox)().ToList().Count
lblQ.Location = New System.Drawing.Point(40, 25 * count)
lblQ.AutoSize = True
lblQ.Name = "lblQ" & (count + 1)
lblQ.Text = dataReader.Item("ProductDes")
lblQ.Font = New Font(lblQ.Font.FontFamily, 11)
Cpanel.Controls.Add(lblQ)
Dim rbChoiceA As New RadioButton()
count = Cpanel.Controls.OfType(Of RadioButton)().ToList().Count
rbChoiceA.Location = New Point(40, 25)
rbChoiceA.AutoSize = True
rbChoiceA.Name = "rb" & (count + 1)
rbChoiceA.Text = dataReader.Item("Each")
rbChoiceA.Font = New Font(rbChoiceA.Font.FontFamily, 10)
AddHandler rbChoiceA.CheckedChanged, AddressOf rbChoiceA_Checked
Cpanel.Controls.Add(rbChoiceA)
Dim rbChoiceB As New RadioButton()
count = Cpanel.Controls.OfType(Of RadioButton)().ToList().Count
rbChoiceB.Location = New Point(200, 25)
rbChoiceB.AutoSize = True
rbChoiceB.Name = "rb" & (count + 1)
rbChoiceB.Text = dataReader.Item("PerDozen")
rbChoiceB.Font = New Font(rbChoiceB.Font.FontFamily, 10)
Cpanel.Controls.Add(rbChoiceB)
Loop
Button2.Enabled = False
conn.Dispose()
conn.Close()
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
'this is an event that i can't access my "label" that I add programmatically
Private Sub rbChoiceA_CheckedChanged(ByVal sender As Object, ByVal e As EventArgs)
Try
Dim conn As OleDbConnection = GetDBConnection()
Dim cmd As New OleDbCommand("INSERT INTO tblSubmit(N)VALUES('" & must be label.text & "') ", conn)
cmd.ExecuteNonQuery()
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
答案 0 :(得分:0)
您可以在数据库或文本文件或SQL或程序中将变量设置为静态
比如string myvar = "static";
答案 1 :(得分:0)
很抱歉我的描述不好,但我现在已经开始工作了。我曾经在任何函数之外声明它,但我犯的错误是用括号表示变量,我不知道它之间的区别所以我需要知道没有括号的那个。 这是我修改过的声明,因此任何函数都必须可以访问
Dim label as Label()
我的button2_Click
事件中的更新代码出错了label = new label() ' I got error here
但是当我删除声明中的括号时,它就可以了。
Dim label as label
所以我可以问一下它之间的区别是什么,为什么我使用的那个没有工作而不是工作的下面。提前感谢您的回复。