【发布时间】:2023-03-31 15:43:01
【问题描述】:
我的编程经验很短暂,我已经编写了几个应用程序,但在 VB.NET 方面还是个新手。
我正在编写一个需要在面板中显示信息的应用程序。这些面板需要在运行时根据来自数据库的信息创建。数据库包含信息,我将创建面板并将数据放入其中。数据将是动态标签,这些标签会随着用户的信息而更新。
我的问题是我以前从未真正创建过动态控件,而且我似乎遇到了问题。如果我在要创建的数据库中选择一项,那么它工作正常。不止一个是我遇到问题的地方。它遍历代码并将控件添加到表单中,但实际上只显示它创建的最后一个面板。其他面板应该在表单上的空间是空白的。
另外,我想在创建后引用动态创建的面板。在创建它们时,我会给它们一个唯一的名称。显然我不能直接在 IDE 中引用他们的名字,还有其他方法可以引用我给他们的名字吗?
我的代码如下,如果有人能指出我所缺少的和做错的正确方向,将不胜感激。
Dim cmd03 As OleDb.OleDbCommand = New OleDb.OleDbCommand(Nothing, strConnection)
Dim strMachineID As String = String.Empty
Dim strMachineIP As String = String.Empty
Dim strMTB As String = String.Empty
Dim MachinePanel As New Panel
Dim MachineName As New Label
Dim MachineStatus As New Label
Dim RunningProg As New Label
Dim RunningPart As New Label
Dim PartsComplete As New Label
Dim startpointX As Integer = 12
Dim startpointY As Integer = 82
Try
cmd03.CommandText = "Select * FROM tblMachines WHERE tblMachines.Monitor = TRUE"
strConnection.Open()
Dim myreader02 As OleDb.OleDbDataReader = cmd03.ExecuteReader(CommandBehavior.CloseConnection)
While myreader02.Read
strMachineID = myreader02.GetValue(0)
strMachineIP = myreader02.GetValue(10)
strMTB = myreader02.GetValue(1)
MachinePanel.Name = "pnl" & strMTB & strMachineID
MachinePanel.Size = New Point(918, 120)
MachinePanel.Location = New Point(startpointX, startpointY)
MachinePanel.BackColor = Color.White
'MachinePanel.Visible = True
'MachinePanel.Enabled = True
MachineName.Name = "lbl" & strMTB & strMachineID
MachineName.Text = strMTB & Chr(32) & strMachineID
MachineName.Font = New Font("Bookman Old Style", 26, FontStyle.Bold)
MachineName.Location = New Point(4, 34)
MachineName.AutoSize = True
MachineStatus.Name = "lbl" & strMTB & strMachineID & "status"
MachineStatus.Text = "?Unknown?"
MachineStatus.Font = New Font("Bookman Old Style", 22.2, FontStyle.Bold)
MachineStatus.Location = New Point(380, 34)
MachineStatus.AutoSize = True
MachineStatus.Enabled = True
MachineStatus.Visible = True
RunningProg.Name = "lbl" & strMTB & strMachineID & "prog"
RunningProg.Text = "prog????"
RunningProg.Font = New Font("Bookman Old Style", 12, FontStyle.Bold)
RunningProg.Location = New Point(760, 9)
RunningProg.AutoSize = True
RunningPart.Name = "lbl" & strMTB & strMachineID & "part"
RunningPart.Text = "part????"
RunningPart.Font = New Font("Bookman Old Style", 12, FontStyle.Bold)
RunningPart.Location = New Point(760, 44)
RunningPart.AutoSize = True
PartsComplete.Name = "lbl" & strMTB & strMachineID & "complete"
PartsComplete.Text = "complete????"
PartsComplete.Font = New Font("Bookman Old Style", 12, FontStyle.Bold)
PartsComplete.Parent = MachinePanel
PartsComplete.Location = New Point(760, 78)
PartsComplete.AutoSize = True
' PartsComplete.Enabled = True
'PartsComplete.Visible = True
Me.Controls.Add(MachinePanel)
MachinePanel.Controls.Add(MachineName)
MachinePanel.Controls.Add(MachineStatus)
MachinePanel.Controls.Add(RunningProg)
MachinePanel.Controls.Add(RunningPart)
MachinePanel.Controls.Add(PartsComplete)
MachinePanel.Visible = True
MachinePanel.Enabled = True
MachinePanel.Show()
startpointY = startpointY + 140
End While
Catch ex As Exception
Finally
strConnection.Close()
End Try
End Sub
【问题讨论】:
-
您必须将代码
Dim MachinePanel As New Panel等移动到您的循环中。现在,您只需创建一次控件。
标签: vb.net winforms user-controls