VB.Net Tutorial/GUI/Introduction

Материал из VB Эксперт
Перейти к: навигация, поиск

Add button to a form

<source lang="vbnet">imports System imports System.Drawing imports System.Windows.Forms namespace WinForms

 public class HelloWorld : inherits System.Windows.Forms.Form
   Private WithEvents btn as Button
   public sub New()
           Text = "Hello World"
     btn = new Button()
     btn.Location = new Point(50,50)
     btn.Text = "Goodbye"
     Controls.Add(btn)
   end sub
   public shared sub Main() 
     Application.Run(new HelloWorld())
   end sub
   private sub btn_Click(ByVal sender as object,ByVal e as EventArgs) Handles btn.Click
     Application.Exit()
   end sub
 end class

end namespace</source>

Button PerformClick Method

<source lang="vbnet">imports System imports System.Drawing imports System.Windows.Forms public class ButtonPerformClick : inherits Form

 dim btn1 as Button
 dim btn2 as Button
 public sub New()
   Size = new Size(200,100)
   btn1 = new Button()
   btn1.Parent = me
   btn1.Text = "Button1"
   btn1.Location = new Point(10,10)
   AddHandler btn1.Click, AddressOf btn1_Click
   btn2 = new Button()
   btn2.Parent = me
   btn2.Text = "Button1"
   btn2.Location = new Point(100,10)
   AddHandler btn2.Click, AddressOf btn2_Click
 end sub
 private sub btn1_Click(ByVal sender as object,ByVal e as EventArgs)
   MessageBox.Show("Button1 clicked.")
   btn2.PerformClick()
 end sub
 private sub btn2_Click(ByVal sender as object,ByVal e as EventArgs)
   MessageBox.Show("Button2 clicked.")
 end sub
 public shared sub Main() 
   Application.Run(new ButtonPerformClick())
 end sub

end class</source>

Control Dock: Top, Bottom

<source lang="vbnet">imports System imports System.Drawing imports System.Windows.Forms public class ControlDock : inherits Form

 public sub New()
       Text = "Control Docking"
   Size = new Size(350,400)
   dim yButtonSize as integer = Font.Height * 2
   dim btnFirst as new Button()
   btnFirst.Parent = me
   btnFirst.Text = "First Button"
   btnFirst.Height = yButtonSize
   btnFirst.Dock = DockStyle.Top
   dim btnSecond as new Button()
   btnSecond.Parent = me
   btnSecond.Text = "Second Button"
   btnSecond.Height = yButtonSize
   btnSecond.Dock = DockStyle.Bottom
 end sub
 public shared sub Main() 
   Application.Run(new ControlDock())
 end sub

end class</source>

Create Form by inheriting System.Windows.Forms.Form

<source lang="vbnet">imports System.Windows.Forms namespace WinForms

 public class HelloWorld : inherits System.Windows.Forms.Form
   public sub New()
           Text = "Hello World"
   end sub
   shared sub Main() 
     Application.Run(new HelloWorld())
   end sub
 end class

end namespace</source>

Use With statement to set Form properties

<source lang="vbnet">Imports System.Windows.Forms public class FormWith

  public Shared Sub Main
   Dim Form2 As New Form()
   With Form2
     .Cursor = System.Windows.Forms.Cursors.Cross
     .Enabled = True
     .FormBorderStyle = FormBorderStyle.Sizable
     .Height = 400
     .HelpButton = True
     .MaximizeBox = True
     .MinimizeBox = True
     .Name = "Form2"
     .ShowInTaskbar = True
     .StartPosition = FormStartPosition.CenterParent
     .Text = "New Form"
     .Width = 500
     .WindowState = FormWindowState.Normal
     .ShowDialog()
   End With
  End Sub

End class</source>

Your first Form window

<source lang="vbnet">Imports System Imports System.Windows.Forms Class MyFirstForm

   Inherits Form
   Private WithEvents mybutton As Button
   Public Sub New()
       Me.Text = "Hello, WinForms!"
       mybutton = New Button()
       mybutton.Text = "Click Me!"
       Me.Controls.Add(mybutton)
   End Sub
   Public Sub mybutton_Click(sender As Object, e As EventArgs) Handles mybutton.Click
       MessageBox.Show("Message")
   End Sub

End Class Class MyFirstApp

   Shared Sub Main()
       Dim myform As Form = New MyFirstForm()
       Application.Run(myform)
   End Sub

End Class</source>