在VB.NET中编写动态面板

时间:2014-01-31 21:26:52

标签: vb.net winforms user-controls

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

1 个答案:

答案 0 :(得分:2)

首先,转动option strict,因为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