Reputation: 785
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?
Upvotes: 0
Views: 2351
Reputation: 4348
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
Upvotes: 0
Reputation: 4007
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
Upvotes: 1