在 Visual Basic(使用代码)中,我创建了一个带有多个标签、文本框等的面板,这些标签需要在另一个面板中复制。创建重复面板的代码如下所示:
Public Class frmOrderEntry2
Dim a As Integer = 2
Dim x As Integer = 160
Public Sub frmOrderEntry2_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
'
'This loads the module name and system date
Dim today1 As String = Today.Date()
txbDate.Text = today1
txbDate.ForeColor = Color.White
txbDate.BackColor = Color.DarkBlue
txbDate.ReadOnly = True
txbLineNumber.Text = 1
End Sub
Private Sub btnCreateNewLine_(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCreateNewLine.Click
Dim b As String = Short.Parse(a)
'This creates the label "Part No."
Dim lblPrtNo As New Label()
lblPrtNo.Name = "lblPrtNo" & Convert.ToString(b)
lblPrtNo.Text = "Part Number"
lblPrtNo.ForeColor = Color.White
lblPrtNo.Font = New Font("Sans Serif", 9)
lblPrtNo.Font = New Font(lblPrtNo.Font, FontStyle.Regular)
lblPrtNo.Location = New Point(13, 8)
lblPrtNo.Size = New Size(77, 15)
Me.Controls.Add(lblPrtNo)
'
'This generates the textbox for the user to enter the Part No.
Dim txbPartNo As New TextBox()
txbPartNo.Name = "txbPartNo" & Convert.ToString(b)
txbPartNo.Text = ""
txbPartNo.ForeColor = Color.Black
txbPartNo.BackColor = Color.Yellow
txbPartNo.Font = New Font("Sans Serif", 10)
txbPartNo.Font = New Font(txbPartNo.Font, FontStyle.Bold)
txbPartNo.Location = New Point(16, 26)
txbPartNo.Size = New Size(263, 22)
txbPartNo.Cursor = Cursors.Hand
txbPartNo.AcceptsReturn = True
txbPartNo.AcceptsTab = True
txbPartNo.TabIndex = 10
AddHandler txbPartNo.TextChanged, AddressOf txbPartNo_Textchanged
Me.Controls.Add(txbPartNo)
'
' This generates the button control to do a part number search
Dim btnPartNoSearch As New Button()
btnPartNoSearch.Name = "btnPartNoSearch"
btnPartNoSearch.Text = "S"
btnPartNoSearch.FlatStyle = FlatStyle.Popup
btnPartNoSearch.FlatAppearance.BorderColor = Color.Black
btnPartNoSearch.FlatAppearance.BorderSize = 2
btnPartNoSearch.Cursor = Cursors.Hand
btnPartNoSearch.TabIndex = 11
btnPartNoSearch.ForeColor = Color.White
btnPartNoSearch.BackColor = Color.Red
btnPartNoSearch.Size = New Size(26, 23)
btnPartNoSearch.Location = New Point(285, 26)
btnPartNoSearch.Visible = True
Me.Controls.Add(btnPartNoSearch)
'This creates the Panel and sets the labels and Textboxes to be displayed.
Dim pnlOrderLine As New Panel()
pnlOrderLine.Size = New Size(1577, 122)
pnlOrderLine.Location = New Point(12, x)
pnlOrderLine.BorderStyle = BorderStyle.Fixed3D
pnlOrderLine.ForeColor = Color.White
pnlOrderLine.Controls.Add(lblPrtNo)
pnlOrderLine.Controls.Add(txbPartNo)
pnlOrderLine.Controls.Add(btnPartNoSearch)
a = a + 1
x = x + 146
End Sub
Private Sub txbPartNo_Textchanged(ByVal sender As System.Object, ByVal e As EventArgs)
Dim txbPartNo As TextBox = DirectCast(sender, TextBox)
Me.Text = txbPartNo.Name & ": " & txbPartNo.Text
txbPartNo.Show()
End Sub
使用相同的思维过程来创建重复面板,我尝试使用以下代码将该面板放入主面板:
' This set the order line panel within the main panel
'Adding to main panel
pnlMainPanel.Controls.Add(pnlOrderLine)
pnlMainPanel.Visible = True
'Adding to Form i.e. me
Me.Controls.Add(pnlOrderLine)
不用说,这是行不通的。线条面板已创建,但它们位于主面板后面。我需要它们在主面板中。
有人知道该怎么做吗?
在此先感谢您的帮助。
大学教师