2. VB .NET 2005 By Warawut Khangkhan
การใชตวแปรแบบคงท&ใน VB.NET 2005 express Edition
'
ตวอย,าง การเขยนโปรแกรมการใชตวแปรแบบคงท
ตวอย:างท&จะใชน&=เปSนตวอย:างท&เ' ก&'ยวกบการคTานวณหาเสนรอบวงกลม โดยใชสGตร 2∏r ซ@'งค:า ∏ ม&ค:าเท:ากบ 3.14
'
2
3
4 5
ตาราง กTาหนด Properties ท&'สาคญของแต:ละ Control
T
No. Control Properties Value
1 Form Name Form1
Text โปรแกรมหาเสนรอบวง
StartPosition CenterScreen
MaximizeBox False
MinimumBox False
2 TextBox Name txtRadius
TextAlign Center
3 TextBox Name txtLineRadius
ReadOnly True
TextAlign Center
4 Button Name btnCalcualtor
Text คTานวณ
5 Button Name btnExit
Text ออก
-2-
3. VB .NET 2005 By Warawut Khangkhan
การใชตวแปรแบบคงท&ใน VB.NET 2005 express Edition
'
Coding:
Public Class Form1
Const pi As Single = 3.14 'pi is constant variable
Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
Application.Exit()
End Sub
Private Sub txtRadius_Enter(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtRadius.Enter
txtRadius.SelectionStart = 0
txtRadius.SelectionLength = txtRadius.Text.Length
End Sub
Private Sub txtRadius_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs)
Handles txtRadius.KeyDown
If e.KeyCode = Keys.Enter Then
btnCalculator_Click(sender, e)
txtRadius.SelectionStart = 0
txtRadius.SelectionLength = txtRadius.Text.Length
End If
End Sub
Private Sub txtRadius_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs)
Handles txtRadius.KeyPress
If (e.KeyChar < "0" Or e.KeyChar > "9") And e.KeyChar <> "." Then e.Handled = True
End Sub
Private Sub btnCalculator_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
btnCalculator.Click
txtLineRadius.Text = Format((2 * pi * Convert.ToDouble(txtRadius.Text)), "0.00")
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
txtRadius.Text = "0"
txtLineRadius.Text = "0.00"
End Sub
End Class
-3-