Help me -Dynamically creating Forms and Controls

KK_Zanok

Member
Joined
Nov 19, 2005
Messages
6
Location
Presently at chennai.
Programming Experience
Beginner
How to create form controls dynamically using code?
Plz give some sample codes.I have tried with the following code but I couldn't get output.

Imports System.Windows.Forms.GroupBox
Imports System.Windows.Forms.CheckBox
Private Sub DrugNameCheckedListbox_ItemCheck(ByVal sender As Object, ByVal e As System.Windows.Forms.ItemCheckEventArgs) Handles DrugNameCLib.ItemCheck
Dim GB1 As New GroupBox
Dim CB1 As New CheckBox
Dim CB2 As New CheckBox
Dim CB3 As New CheckBox
GB1.Name = "DrugNameGB1"
GB1.Text = "DrugTime"
CB1.Name = "MornCb"
CB1.Text = "Morning"
CB2.Name = "NoonCb"
CB2.Text = "AfterNoon"
CB3.Name = "NightCb"
CB3.Text = "Night"
GB1.GetContainerControl()
GB1.Controls.Add(CB1)
GB1.Controls.Add(CB2)
GB1.Controls.Add(CB3)
GB1.Visible =
True
CB1.Visible = True
CB2.Visible = True
CB3.Visible = True
GB1.BringToFront()
GB1.Show()
End Sub
 
The easiest way to check this is to draw controls and see what code the IDE has generated. :)

VB.NET:
Friend WithEvents GB1 As System.Windows.Forms.GroupBox
Friend WithEvents CB1 As System.Windows.Forms.CheckBox
Friend WithEvents CB2 As System.Windows.Forms.CheckBox

Friend WithEvents CB3 As System.Windows.Forms.CheckBox[INDENT]Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.SuspendLayout()
GB1 = New GroupBox
GB1.Location = New Point(20, 20)
GB1.Size = New Size(140, 100)
GB1.Name = "DrugName"
GB1.Text = "Drug Time"
CB1 = New CheckBox
CB1.Location = New Point(20, 20)
CB1.Name = "MornCB"
CB1.Text = "Morning"
CB2 = New CheckBox
CB2.Location = New Point(20, 40)
CB2.Name = "NoonCB"
CB2.Text = "Afternoon"
CB3 = New CheckBox
CB3.Location = New Point(20, 60)
CB3.Name = "NightCB"
CB3.Text = "Night"
Me.Controls.Add(GB1)
GB1.Controls.Add(CB1)
GB1.Controls.Add(CB2)
GB1.Controls.Add(CB3)
Me.ResumeLayout()

[/INDENT]End Sub

Private Sub CB1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles CB1.Click, CB2.Click, CB3.Click[INDENT]Dim CheckedBox As String = CType(sender.name, String)
Select Case CheckedBox
Case "MornCB"
Console.WriteLine("CB1")
Case "NoonCB"
Console.WriteLine("CB2")
Case "NightCB"
Console.WriteLine("CB3")
End Select

[/INDENT]End Sub
 
Back
Top