Friday, February 11, 2011

Add a button to a page programmatically

Add a button to a page programmatically

 Public Class Form1
  
   Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  
     'create new button
  
     Dim btnNew As New Button
  
     'set properties
  
     btnNew.Text = "CLICK ME"
  
     btnNew.Top = 45
  
     btnNew.Left = 175
  
     'Add the button to the form
  
     Me.Controls.Add(btnNew)
  
   End Sub
  
 End Class  

Add a textbox to a page programmatically

 Public Class Form1
  
   Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  
     'create textbox
  
     Dim txtNew As TextBox = Nothing
  
     'set textbox properties
  
     txtNew = New Windows.Forms.TextBox
  
     txtNew.Name = "NewText"
  
     txtNew.Location = New System.Drawing.Point(6, 10)
  
     txtNew.Size = New System.Drawing.Size(250, 50)
  
     txtNew.TabIndex = 0
  
     'add text box to form
  
     Me.Controls.Add(txtNew)
  
   End Sub
  
 End Class  

Add a label to a page programmatically

 Public Class Form1
  
   Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  
     'create textbox
  
     Dim lblNew As New Label
  
     'set textbox properties
  
     lblNew.SetBounds(10, 50, 100, 25)
  
     lblNew.Text = "I... Am a Label"
  
     'add text box to form
  
     Me.Controls.Add(lblNew)
  
   End Sub
  
 End Class  

Add an item list to a page programmatically
 Public Class Form1
  
   Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  
     'create item
  
     Dim lstAddress As ListBox
  
     lstAddress = New System.Windows.Forms.ListBox()
  
     'set list box properties
  
     lstAddress.IntegralHeight = False
  
     lstAddress.Location = New Point(20, 85)
  
     lstAddress.Name = "Adresses"
  
     lstAddress.Size = New System.Drawing.Size(250, 250)
  
     lstAddress.TabIndex = 0
  
     'add list box to form
  
     Me.Controls.Add(lstAddress)
  
   End Sub
  
 End Class  

No comments:

Post a Comment