在 VB .NET 中的结构声明内向容器添加控件

发布于 2025-01-05 21:15:59 字数 393 浏览 1 评论 0原文

有没有办法在 VB .NET 的结构声明中向容器添加控件?

我真正想做的是:

Structure LabelContainer
    Dim pnlContainer As New Panel
    Dim lblTime As New Label
    Dim lblStudent As New Label
    Dim lblTeacher As New Label
    lblTime.Parent = pnlContainer
    lblStudent.Parent = pnlContainer
    lblTeacher.Parent = pnlContainer
End Structure

但这在 VB .NET 中不起作用。有没有办法达到同样的目的?

Is there any way of adding a control to a container within a structure declaration in VB .NET?

What I would really like to do is:

Structure LabelContainer
    Dim pnlContainer As New Panel
    Dim lblTime As New Label
    Dim lblStudent As New Label
    Dim lblTeacher As New Label
    lblTime.Parent = pnlContainer
    lblStudent.Parent = pnlContainer
    lblTeacher.Parent = pnlContainer
End Structure

But this doesn't work in VB .NET. Is there anyway of achieving the same thing?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

赤濁 2025-01-12 21:15:59

结构对控件所需事件的处理非常有限,例如创建控件时触发的 InitializeComponent() 事件。有关更多详细信息,请参阅:

http://www.codeproject.com /Articles/8607/Using-Structures-in-VB-NET

您可以做的是创建一个从面板继承的类,例如:

Public Class LabelContainer
    Inherits Panel
    Friend WithEvents lblTeacher As System.Windows.Forms.Label
    Friend WithEvents lblStudent As System.Windows.Forms.Label
    Friend WithEvents lblTime As System.Windows.Forms.Label

    Private Sub InitializeComponent()
        Me.lblTime = New System.Windows.Forms.Label()
        Me.lblStudent = New System.Windows.Forms.Label()
        Me.lblTeacher = New System.Windows.Forms.Label()
        Me.SuspendLayout()
        '
        'lblTime
        '
        Me.lblTime.AutoSize = True
        Me.lblTime.Location = New System.Drawing.Point(0, 0)
        Me.lblTime.Name = "lblTime"
        Me.lblTime.Size = New System.Drawing.Size(100, 23)
        Me.lblTime.TabIndex = 0
        Me.lblTime.Text = "Label1"
        '
        'lblStudent
        '
        Me.lblStudent.AutoSize = True
        Me.lblStudent.Location = New System.Drawing.Point(0, 0)
        Me.lblStudent.Name = "lblStudent"
        Me.lblStudent.Size = New System.Drawing.Size(100, 23)
        Me.lblStudent.TabIndex = 0
        Me.lblStudent.Text = "Label2"
        '
        'lblTeacher
        '
        Me.lblTeacher.AutoSize = True
        Me.lblTeacher.Location = New System.Drawing.Point(0, 0)
        Me.lblTeacher.Name = "lblTeacher"
        Me.lblTeacher.Size = New System.Drawing.Size(100, 23)
        Me.lblTeacher.TabIndex = 0
        Me.lblTeacher.Text = "Label3"
        Me.ResumeLayout(False)

    End Sub
End Class

Structures have very limited handling of the events required by controls, such as the InitializeComponent() event that is fired when a control is created. See this for more details:

http://www.codeproject.com/Articles/8607/Using-Structures-in-VB-NET

What you could do is create a class that inherits from a panel instead, for example:

Public Class LabelContainer
    Inherits Panel
    Friend WithEvents lblTeacher As System.Windows.Forms.Label
    Friend WithEvents lblStudent As System.Windows.Forms.Label
    Friend WithEvents lblTime As System.Windows.Forms.Label

    Private Sub InitializeComponent()
        Me.lblTime = New System.Windows.Forms.Label()
        Me.lblStudent = New System.Windows.Forms.Label()
        Me.lblTeacher = New System.Windows.Forms.Label()
        Me.SuspendLayout()
        '
        'lblTime
        '
        Me.lblTime.AutoSize = True
        Me.lblTime.Location = New System.Drawing.Point(0, 0)
        Me.lblTime.Name = "lblTime"
        Me.lblTime.Size = New System.Drawing.Size(100, 23)
        Me.lblTime.TabIndex = 0
        Me.lblTime.Text = "Label1"
        '
        'lblStudent
        '
        Me.lblStudent.AutoSize = True
        Me.lblStudent.Location = New System.Drawing.Point(0, 0)
        Me.lblStudent.Name = "lblStudent"
        Me.lblStudent.Size = New System.Drawing.Size(100, 23)
        Me.lblStudent.TabIndex = 0
        Me.lblStudent.Text = "Label2"
        '
        'lblTeacher
        '
        Me.lblTeacher.AutoSize = True
        Me.lblTeacher.Location = New System.Drawing.Point(0, 0)
        Me.lblTeacher.Name = "lblTeacher"
        Me.lblTeacher.Size = New System.Drawing.Size(100, 23)
        Me.lblTeacher.TabIndex = 0
        Me.lblTeacher.Text = "Label3"
        Me.ResumeLayout(False)

    End Sub
End Class
凤舞天涯 2025-01-12 21:15:59

您可以将子添加到您的结构中:

Structure LabelContainer
    Dim pnlContainer As Panel
    Dim lblTime As Label
    Dim lblStudent As Label
    Dim lblTeacher As Label
    Sub AddControls()
        lblTime.Parent = pnlContainer
        lblStudent.Parent = pnlContainer
        lblTeacher.Parent = pnlContainer
    End Sub
End Structure

You can add a sub to your Struct:

Structure LabelContainer
    Dim pnlContainer As Panel
    Dim lblTime As Label
    Dim lblStudent As Label
    Dim lblTeacher As Label
    Sub AddControls()
        lblTime.Parent = pnlContainer
        lblStudent.Parent = pnlContainer
        lblTeacher.Parent = pnlContainer
    End Sub
End Structure
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文