Wednesday, February 9, 2011

Adding code to a page programmatically

To add a button to a page just by using code, we use Me.Controlls.Add(button).  The following is adding a button that says "go". 

     Dim btn As New Button
  
     btn.Text = "go"
  
     btn.Top = 45
  
     btn.Left = 190
  
     Me.Controls.Add(btn)  

This is to add a textbox to a form using code. Using dynamic text and also using Me.Controls.Add(textbox).
     Dim dynamicText As TextBox = Nothing
  
     dynamicText = New Windows.Forms.TextBox
  
     dynamicText.Name = "TimeTextBox"
  
     dynamicText.Location = New System.Drawing.Point(8, 8)
  
     dynamicText.Size = New System.Drawing.Size(232, 20)
  
     dynamicText.TabIndex = 0
  
     Me.Controls.Add(dynamicText)
  
   End Sub  

Adding a label to a form using code is pretty short.  You can set the boundaries of where the label will sit using lbl.SetBounds.
     Dim lbl As New Label
  
     lbl.SetBounds(10, 50, 100, 25)
  
     lbl.Text = "Hello World!"
  
     Me.Controls.Add(lbl)
  
   End Sub  

Here is how to add a listbox using code, also including the size and point of where the listbox is to be placed.
     Dim listBox1 As New ListBox()
  
     listBox1.Size = New System.Drawing.Size(200, 100)
  
     listBox1.Location = New System.Drawing.Point(10, 10)
  
     Me.Controls.Add(listBox1)
  
   End Sub  

The DataGridView control can display rows of data from a data source. You can extend the DataGridView control in a number of ways to build custom behaviors into your applications. The ReadOnly property indicates whether the data displayed by the cell can be edited or not. You can set ReadOnly Property in three levels. You can make entire dataGridView as ReadOnly.
 dataGridView1.ReadOnly = true
  

You can make entire row as ReadOnly:
 dataGridView1.Rows(index).ReadOnly = true  

You can make entire Column as ReadOnly
 dataGridView1.Columns(index).ReadOnly = true
  

No comments:

Post a Comment