以编程方式将控件添加到表单

mad*_*lan 1 vb.net

我正在使用附加的代码在现有集合下添加另一行\控件行(单击标签时).可能会添加相当多的行,因此我不得不使用计数器(i)多次重复代码以跟踪...

这样做有更好的方法吗?

Private Sub Label10_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)      Handles LblExpandSearch.Click
    If i = 0 Then

        'TextBox7
        '
        Dim TextBox7 As New TextBox
        TextBox7.Size = New Size(302, 20)
        TextBox7.Name = "TextBox7"
        TextBox7.Location = New System.Drawing.Point(60, 135)
        Me.ExpAdvancedSearch.Controls.Add(TextBox7)

        'RadioButton5
        '
        Dim RadioButton5 As New RadioButton
        RadioButton5.AutoSize = True
        RadioButton5.Checked = True
        RadioButton5.Location = New System.Drawing.Point(77, 112)
        RadioButton5.Name = "RadioButton5"
        RadioButton5.Size = New System.Drawing.Size(55, 17)
        RadioButton5.TabIndex = 48
        RadioButton5.TabStop = True
        RadioButton5.Text = "NEAR"
        RadioButton5.UseVisualStyleBackColor = True

    ElseIf i = 1 Then

        ExpAdvancedSearch.Size_ExpandedHeight = 260

        'TextBox8
        '
        Dim TextBox8 As New TextBox
        TextBox8.Size = New Size(302, 20)
        TextBox8.Name = "TextBox8"
        TextBox8.Location = New System.Drawing.Point(60, 185)
        Me.ExpAdvancedSearch.Controls.Add(TextBox8)


        'RadioButton9
        '
        Dim RadioButton9 As New RadioButton
        RadioButton9.AutoSize = True
        RadioButton9.Checked = True
        RadioButton9.Location = New System.Drawing.Point(77, 162)
        RadioButton9.Name = "RadioButton9"
        RadioButton9.Size = New System.Drawing.Size(55, 17)
        RadioButton9.TabIndex = 48
        RadioButton9.TabStop = True
        RadioButton9.Text = "NEAR"
        RadioButton9.UseVisualStyleBackColor = True

    End If

    i = i + 1
End Sub
Run Code Online (Sandbox Code Playgroud)

Fas*_*tAl 6

嗯.. UseVisualStyleBackColor对我说'winforms'.

几点......

不要将控件全部添加到一个面板,请使用usercontrol.

然后只需添加其实例.

不要处理标签中的点击事件

使用链接标签或按钮.其他任何东西=对用户意味着什么.当然,你想到了它对你有意义!现在对用户来说,这是黑白分明.

样品...

当然很少.你想要:

  • 将项目放在可滚动面板中而不是在表单上.
  • 也可能将它们添加到uc的通用列表中.
  • 设置表格的最小/最大尺寸 - 允许合理的尺寸(允许任何高度> ~100)
  • 设置uc和控制.Anchor属性以允许合理的大小调整

uc.vb

Public Class uc
    Inherits System.Windows.Forms.UserControl

    Private components As System.ComponentModel.IContainer
    Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
    Friend WithEvents LinkLabel1 As System.Windows.Forms.LinkLabel

    Public Sub New()
        MyBase.New()

        Me.TextBox1 = New System.Windows.Forms.TextBox
        Me.LinkLabel1 = New System.Windows.Forms.LinkLabel
        Me.SuspendLayout()

        Me.TextBox1.Location = New System.Drawing.Point(8, 8)
        Me.TextBox1.Name = "TextBox1"
        Me.TextBox1.Size = New System.Drawing.Size(88, 20)
        Me.TextBox1.TabIndex = 0
        Me.TextBox1.Text = "TextBox1"

        Me.LinkLabel1.Enabled = False
        Me.LinkLabel1.Location = New System.Drawing.Point(112, 8)
        Me.LinkLabel1.Name = "LinkLabel1"
        Me.LinkLabel1.Size = New System.Drawing.Size(24, 16)
        Me.LinkLabel1.TabIndex = 1
        Me.LinkLabel1.TabStop = True
        Me.LinkLabel1.Text = "add"

        Me.Controls.Add(Me.LinkLabel1)
        Me.Controls.Add(Me.TextBox1)
        Me.Name = "uc"
        Me.Size = New System.Drawing.Size(148, 36)
        Me.ResumeLayout(False)

    End Sub

    Private _addcallback As EventHandler = Nothing
    Public Property AddCallback() As EventHandler
        Get
            Return _addcallback
        End Get
        Set(ByVal Value As EventHandler)

            _addcallback = Value
            LinkLabel1.Enabled = Not Value Is Nothing

        End Set
    End Property

    Private Sub LinkLabel1_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles LinkLabel1.LinkClicked
        If AddCallback Is Nothing Then Throw New ApplicationException("AddCallback not set on a uc") ' ALWAYS check for errors like this 
        _addcallback(Me, Nothing)
        AddCallback = Nothing ' gray myself out, can't insert in thie implementation
    End Sub
End Class
Run Code Online (Sandbox Code Playgroud)

frm.vb

Public Class frm
    Inherits System.Windows.Forms.Form

    Private components As System.ComponentModel.IContainer
    Public Sub New()
        MyBase.New()
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(292, 266)
        Me.Name = "Form1"
        Me.Text = "Form1"
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        AddClicked(Me, Nothing)
    End Sub

    Private Sub AddClicked(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim myuc As New uc
        myuc.AddCallback = AddressOf AddClicked
        If Controls.Count > 0 Then
            myuc.Top = Controls(Controls.Count - 1).Bottom
        End If
        Me.Controls.Add(myuc)
    End Sub
End Class
Run Code Online (Sandbox Code Playgroud)