VB.NET中的编程动态面板



我的编程经验很短,我编写了几个应用程序,但在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

首先,将选项严格按照MachinePanel.Size = New Point(918, 120)接受Size结构而不是Point结构。其次,您需要在每个周期中创建每个对象的新实例。

While myreader02.Read
    MachinePanel = New Panel() '
    MachineName = New Label()
    MachineStatus = New Label()
    RunningProg = New Label()
    RunningPart = New Label()
    PartsComplete = New Label()
    With MachinePanel
        .Name = "pnl" & strMTB & strMachineID
        .Size = New Size(918, 150)
        .Location = New Point(startpointX, startpointY)
        .BackColor = Color.White
    End With
    With MachineName
        .Name = "lbl" & strMTB & strMachineID
        .Text = strMTB & Chr(32) & strMachineID
        .Font = New Font("Bookman Old Style", 26, FontStyle.Bold)
        .Location = New Point(0, 0) '<- In relation to MachinePanel.ClientRectangle
        .AutoSize = True
    End With
    With MachineStatus
        .Name = "lbl" & strMTB & strMachineID & "status"
        .Text = "?Unknown?"
        .Font = New Font("Bookman Old Style", 22.2, FontStyle.Bold)
        .Location = New Point(0, 30)
        .AutoSize = True
        .Enabled = True
        .Visible = True
    End With
    With RunningProg
        .Name = "lbl" & strMTB & strMachineID & "prog"
        .Text = "prog????"
        .Font = New Font("Bookman Old Style", 12, FontStyle.Bold)
        .Location = New Point(0, 60)
        .AutoSize = True
    End With
    With RunningPart
        .Name = "lbl" & strMTB & strMachineID & "part"
        .Text = "part????"
        .Font = New Font("Bookman Old Style", 12, FontStyle.Bold)
        .Location = New Point(0, 90)
        .AutoSize = True
    End With
    With PartsComplete
        .Name = "lbl" & strMTB & strMachineID & "complete"
        .Text = "complete????"
        .Font = New Font("Bookman Old Style", 12, FontStyle.Bold)
        .Parent = MachinePanel
        .Location = New Point(0, 120)
        .AutoSize = True
    End With
    MachinePanel.Controls.AddRange(New Control() {MachineName, MachineStatus, RunningProg, RunningPart, PartsComplete})
    Me.Controls.Add(MachinePanel)
    startpointY = startpointY + 150
End While

最新更新