2012-02-15 10 views
0

VB .NETで構造体宣言内のコンテナにコントロールを追加する方法はありますか?私は本当にやりたい何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で動作しません。とにかく同じことを達成することはありますか?

+1

構造体宣言にコードを挿入することはできません。クラスを使用し、コードをコンストラクタに配置します。そしてvb.netプログラミングに関する本を手に入れよう。 –

答えて

1

構造体には、コントロールの作成時に発生する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 
0

あなたの構造体にサブを追加することができます。

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 
関連する問題