我制作了一个panel
,我是用语法编写的。创建此面板后,我将其添加到WinFrom。然后,我向textbox
添加了label
和panel
。如果我将面板设置为正确的高度和宽度,这一切都可以正常工作,但是我不想这样做,因为可能会有多个文本框添加到面板中。我将Autosize设置为True时遇到的问题不会相应地自动调整面板。
我的代码如下
Private Sub CreateTextBoxFieldPanel(ByVal multiLine As Boolean, ByVal textBoxName As String, ByVal labelCaption As String, tag As String, textBoxText As String)
'label plus textbox
Dim _NewPanel As Panel = CreatePanel(textBoxName, textBoxText)
Dim _NewLabel As Label = CreateLabelPanel(labelCaption)
Dim _NewTextBox As TextBox = CreateTextBoxPanel(textBoxName, multiLine, textBoxText)
_NewPanel.Controls.Add(_NewLabel)
_NewPanel.Controls.Add(_NewTextBox)
Me.Controls.Add(_NewPanel)
End Sub
Private Function CreatePanel(ByVal textBoxName As String, ByVal textBoxText As String, Optional ByVal textBoxWidth As Integer = DefaultTextBoxWidth) As Panel
'returns panel with default properties
Dim _NewPanel As New Panel
_NewPanel.Name = textBoxName
_NewPanel.Text = textBoxText
_NewPanel.Top = topForNextControl
_NewPanel.Left = 17
_NewPanel.Size = New System.Drawing.Size(0, 0)
_NewPanel.Anchor = AnchorStyles.Top Or AnchorStyles.Right
_NewPanel.TabIndex = tabIndexForNextControl
tabIndexForNextControl += 1
_NewPanel.AutoSize = True
_NewPanel.AutoSizeMode = AutoSizeMode.GrowAndShrink
Return _NewPanel
End Function
Private Function CreateLabelPanel(ByVal LabelCaption As String) As Label
'returns label with default properties
Dim _NewLabel As New Label
_NewLabel.Text = LabelCaption
_NewLabel.Top = labelInPanel
_NewLabel.Left = 0
_NewLabel.AutoSize = True
_NewLabel.Anchor = AnchorStyles.Top Or AnchorStyles.Left
_NewLabel.TabIndex = tabIndexForNextControl
_NewLabel.BackColor = Color.Transparent
tabIndexForNextControl += 1
Return _NewLabel
End Function
Private Function CreateTextBoxPanel(ByVal textBoxName As String, ByVal multiline As Boolean, ByVal textBoxText As String, Optional ByVal textBoxWidth As Integer = DefaultTextBoxWidth) As TextBox
'returns textbox with default properties
Dim _NewTextBox As New TextBox
_NewTextBox.Name = textBoxName
_NewTextBox.Multiline = multiline
_NewTextBox.Text = textBoxText
_NewTextBox.Top = labelInPanel + textBoxInPanel
_NewTextBox.Left = 0
_NewTextBox.Width = 200
If multiline Then
_NewTextBox.Height = 20
End If
_NewTextBox.Anchor = AnchorStyles.Top Or AnchorStyles.Right
_NewTextBox.TabIndex = tabIndexForNextControl
tabIndexForNextControl += 1
Return _NewTextBox
End Function