Using Controls in a Windows Forms Application : Introduction « GUI « VB.Net Tutorial






Option Explicit
Option Strict

Imports System
Imports System.ComponentModel
Imports System.Windows.Forms
Imports System.Resources
Imports System.Drawing

Public Class MyForm
   Inherits Form
   Private red As Button
   Private blue As Button
   Private green As Button
   
   Public Sub New()
      InitializeComponent()
   End Sub
   
   Protected Overloads Overrides Sub Dispose(disposing as Boolean)
      MyBase.Dispose(disposing)
   End Sub
   
   Private Sub InitializeComponent()
      red = New Button()
      red.Text = "Red"
      red.Location = New Point(100, 50)
      red.Size = New Size(50, 50)
      AddHandler red.Click, AddressOf button_Click
      Controls.Add(red)
      
      blue = New Button()
      blue.Text = "Blue"
      blue.Location = New Point(100, 100)
      blue.Size = New Size(50, 50)
      AddHandler blue.Click, AddressOf button_Click
      Controls.Add(blue)
      
      green = New Button()
      green.Text = "Green"
      green.Location = New Point(100, 150)
      green.Size = New Size(50, 50)
      AddHandler green.Click, AddressOf button_Click
      Controls.Add(green)
   End Sub
   
   Private Sub button_Click(sender As Object, e As EventArgs)
      If sender Is red Then
         Me.BackColor = Color.Red
      Else
         If sender Is blue Then
            Me.BackColor = Color.Blue
         Else
            Me.BackColor = Color.Green
         End If
      End If 
   End Sub

   <STAThread()> _
   Public Shared Sub Main()
      Application.Run(New MyForm())
   End Sub
End Class








14.1.Introduction
14.1.1.Your first Form window
14.1.2.Create Form by inheriting System.Windows.Forms.FormCreate Form by inheriting System.Windows.Forms.Form
14.1.3.Add button to a formAdd button to a form
14.1.4.Control Dock: Top, BottomControl Dock: Top, Bottom
14.1.5.Button PerformClick MethodButton PerformClick Method
14.1.6.Use With statement to set Form propertiesUse With statement to set Form properties
14.1.7.Essential elements of a Windows Forms application.
14.1.8.Using Controls in a Windows Forms Application