範例1
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim id As Integer
If RadioButton1.Checked = True Then id = 1
If RadioButton2.Checked = True Then id = 2
If RadioButton3.Checked = True Then id = 3
TextBox1.Text = Choose(id, "國文", "英文", "數學")
End Sub
End Class
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
ListBox1.Items.Add(ComboBox1.SelectedItem)
End Sub
End Class
範例2 點餐系統
Public Class Form1
Dim s As Integer
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
s = 0
If RadioButton1.Checked = True Then
s = s + 200
End If
If RadioButton2.Checked = True Then
s = s + 150
End If
If RadioButton3.Checked = True Then
s = s + 100
End If
If CheckBox1.Checked = True Then
s = s + 50
End If
If CheckBox2.Checked = True Then
s = s + 30
End If
If CheckBox3.Checked = True Then
s = s + 100
End If
If CheckBox4.Checked = True Then
s = s + 40
End If
TextBox1.Text = s
End Sub
End Class
範例3 顯示圖片
Public Class Form1
Dim gp As Graphics '宣告桌布
Dim img As Image '宣告圖檔
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click '按鈕按下時
gp.DrawImage(img, 0, 0, img.Width, img.Height) '將圖檔顯示在桌布
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load '畫面載入時
gp = PictureBox1.CreateGraphics '產生桌布
img = Image.FromFile("c:\test.jpg") '指定圖檔位置
End Sub
End Class
Public Class Form1
Dim gp As Graphics '宣告桌布
Dim img As Image '宣告圖檔
Dim img1 As Image
Dim s As Integer
範例4 動態切換圖片
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load '畫面載入時
gp = PictureBox1.CreateGraphics '產生桌布
img = Image.FromFile("c:\test.jpg") '指定圖檔位置
img1 = Image.FromFile("c:\test1.jpg") '指定圖檔位置
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
If s = 0 Then
gp.DrawImage(img, 0, 0, img.Width, img.Height) '將圖檔顯示在桌布
s = 1
else
gp.DrawImage(img1, 0, 0, img.Width, img.Height) '將圖檔顯示在桌布
s = 0
End If
End Sub
End Class
範例五
Imports System.Drawing '匯入繪圖元件
Public Class Form1
Dim gp As Graphics '宣告繪圖物件
Dim pn As Pen '宣告畫筆
Dim w, h As Integer
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
w = 40 '設定長方形長寬
h = 20
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
w = 20 '設定正方形長寬
h = 20
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
gp.Clear(Color.White) '清除為白色
End Sub
Private Sub PictureBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown
gp.DrawRectangle(pn, e.X, e.Y, w, h) '繪圖在畫筆的座標
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
gp = PictureBox1.CreateGraphics '建立繪圖物件
End Sub
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
pn = New Pen(Color.Blue, 1) '宣告畫筆藍色
End Sub
Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
pn = New Pen(Color.Red, 1) '宣告畫筆紅色
End Sub
Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
pn = New Pen(Color.Green, 1) '宣告畫筆綠色
End Sub
End Class
---------------------------------亂數繪圖-------------------------
Imports System.Drawing '匯入系統變數
Public Class Form1
Dim Gp As Graphics '定義繪圖物件
Dim pn As Pen '定義畫筆
Dim x, y, w, h As Integer '設定XY軸位置及WH寬高
Dim r, g, b As Integer '設定RGB三原色
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Randomize() '產生亂數
For i = 1 To 100 '產生100個迴圈
x = Int(Rnd() * 600) '產生亂數
y = Int(Rnd() * 400)
w = Int(Rnd() * 100)
h = Int(Rnd() * 100)
r = Int(Rnd() * 256)
g = Int(Rnd() * 256)
b = Int(Rnd() * 256)
pn = New Pen(Color.FromArgb(r, g, b)) '設定畫筆顏色
Gp.DrawEllipse(pn, x, y, w, h) '畫橢圓
Next i
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Gp = PictureBox1.CreateGraphics '設定物件
End Sub
End Class
------拉霸程式------------------------
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Timer1.Enabled = False
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Randomize()
Label1.Text = Int(Rnd() * 10)
Label2.Text = Int(Rnd() * 10)
Label3.Text = Int(Rnd() * 10)
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Timer1.Enabled = True
End Sub
End Class