SlideShare a Scribd company logo
1 of 5
Download to read offline
VB .NET 2005 By Warawut Khangkhan
การแปลงชนดของข$อม&ลใน VB.NET 2005 express Edition



                การแปลงชนดของขอมลใน VB .NET 2005 Express Edition
          ในการเข1ยนโปรแกรมโดยท56วไป เราอาจต$องการแปลงข$อม&ลจากชนดหน<6งไปเป=นอ1กชนดหน<6ง เช>น ถ$าเราม1 TextBox 2
ต5ว ท16ท@าหน$าท16ในการร5บค>าจากแปCนพมพE แล$วเราน@า TextBox ท5Fง 2 มารวมก5น (+) จะท@าให$ผลล5พธEท16เราต$องการผดพลาดได$
เนM6องจากเครM6องหมายบวก (+) เป=นการรวมก5นระหว>าง TextBox ท5Fงสอง เพราะฉะน5Fนเราจ@าเป=นต$องท@าการแปลงชน ดของ
ข$อม&ลใน TextBox ท5Fง 2 เป=นชนดข$อม&ลต5วเลขก>อน ถ<งจะได$ผลล5พธEท16ถ&กต$อง
          ใน VB .NET สามารถแปลงชน ดของข$อม&ลได$อย>า งง> า ยดาย เนM 6อ งจากข$อม&ลทPกชน ดใน VB .NET ถMอ เป=น Object
ประเภทหน<6ง ซ<6งม1เมธอด (Method) หรMอฟYงกEช56น (Function) ท1ใช$แปลงชนดข$อม&ลอย&>ภายในเร1ยบร$อยแล$ว
                                                            6
          ต5วอย>างเช>น ถ$าเราต$องการแปลงชน ดข$อม&ลของต5วแปร A ซ<6งเป=นต5วแปรชน ด Single ไปเป=นข$อม&ลชน ด String และ
เก^บผลล5พธEไว$ในต5วแปร B จะท@าได$ด5งน1F

                 Dim A As Single = 123.45           'ประกาศต5วแปร A ให$เป=นต5วแปรชนด Single
                 Dim B As String                    'ประกาศต5วแปร B ให$เป=นต5วแปรชนด String
                 B = A.ToString                     'แปลงข$อม&ลของต5วแปร A จากชนด Single ไปเป=น String และเก^บผลล5พธE
                                                     ไว$ในต5วแปร B

        Method ท5Fงหมดท1ใช$แปลงชนดข$อม&ลจะอย&>ในคลาส System.Convert ซ<6งเราสามารถเร1ยกโดยใช$ร&ปแบบ
                        6
        รปแบบ
                <ชอตวแปร>.<ชอเมธอด>
                          หรMอ
                System.Convert.<ชอเมธอด>(<ชอตวแปร>)
        จากต5วอย>าง B = A.ToString เราอาจเข1ยนอ1กร&ปแบบหน<6งได$ด5งน1F System.Convert.ToString(A) ก^ได$เช>นก5น

ตาราง สรPป Method ท16สาค5ญท1ใช$แปลงชนดของข$อม&ล
                      @     6
   Method                 Description                     Example Program                              Result
ToString     แปลงข$อม&ลชนดต5วเลข (numeric)      Dim varString As String                     'varString =
             หรMอ Date ให$เป=นต5วอ5กษร          Dim varDate As Date = Now( )                    “11/12/2008 17:33”
                                                Dim varDouble As Double = 123.45            'varString = “123.45”
                                                varString = varDate.ToString
                                                varString = varDouble.ToString
ToBool       แปลงข$อม&ลชนดสตรงให$เป=น Boolean Dim varString As String = “True”              'varBoolean = True
                                                Dim varBoolean As Boolean
                                                varBoolean = varString.ToBool
ToChar       แปลงข$อม&ลชนดต5วเลขให$เป=น Char Dim varByte As Byte = 80                       'varChar = 'P'
                                                Dim varChar As Char
                                                varChar = Convert.ToChar(varByte)


                                                          -1-
VB .NET 2005 By Warawut Khangkhan
การแปลงชนดของข$อม&ลใน VB.NET 2005 express Edition


ตาราง (ต5อ) สรPป Method ท16สาค5ญท1ใช$แปลงชนดของข$อม&ล
                            @     6
   Method                  Description                         Example Program                          Result
ToByte          แปลงเป=นข$อม&ลชนด Byte               Dim varChar As Char = “P”              'varByte = 80
                                                     Dim varByte As Byte
                                                     varByte = Convert.ToByte(varChar)
ToDateTime      แปลงเป=นข$อม&ลชนด DateTime           Dim varString As String =              'varDateTime =
                                                          “01/02/2002”                          01/02/2002
                                                     Dim varDateTime As DateTime
                                                     varDateTime =
                                                          Convert.ToDateTime(varString)
ToInt16         แปลงเป=นข$อม&ลอM6น ๆ ให$เป=น Short Dim varSring As String = “1234”          'varShort = 1234
                                                     Dim varShort As Short
                                                     varShort =
                                                          Convert.ToInt16(varString)
ToInt32         แปลงเป=นข$อม&ลอM6น ๆ ให$เป=น Integer Dim varInt As Integer                  'varInt = 3500
                                                     Dim varLong As Long = 3500             'varInt = 123
                                                     Dim varSingle As Single =
                                                          123.4567
                                                     varInt = Convert.ToInt32(varLong)
                                                     varInt = Convert.ToInt32(varSingle)
ToInt64         แปลงเป=นข$อม&ลอM6น ๆ ให$เป=น Long Dim varLong As Long                       'varLong = 699999999
                                                     Dim varInt As Integer =                'varLong = 123
                                                          699999999
                                                     Dim varSingle As Single = 123.456
                                                     varLong = Convert.ToInt64(varInt)
                                                     varLong = Convert.ToInt64(varSingle)
ToSingle        แปลงเป=นข$อม&ลอM6น ๆ ให$เป=น Single Dim varSingle As Single                 'varSingle = 6999
                                                     Dim varInt As Integer = 6999
                                                     varSingle = Convert.ToSingle(varInt)
ToDouble        แปลงเป=นข$อม&ลอM6น ๆ ให$เป=น Double Dim varDouble As Double                 'varDouble =
                                                     Dim varSingle =                            6999999999999.99
                                                          “6999999999999.99”
                                                     varDouble =
                                                          Convert.ToDouble(varSingle)




                                                         -2-
VB .NET 2005 By Warawut Khangkhan
การแปลงชนดของข$อม&ลใน VB.NET 2005 express Edition


ต7วอย5าง การเข;ยนโปรแกรมในการแปลงชนดของขอมล โดยไม5ใช Method
        ต5วอย>างท16จะใช$น1Fเป=นต5วอย>างท16เก16ยวก5บการค@านวณโดยการบวกเลข ซ<6งเป=นการร5บข$อม&ลจากแปCนพมพEมา 2 ค>า โดย
Control ท16ใช$ในการร5บค>าก^คMอ TextBox ซ<6งการค@านวณด$วยวธ1การบวกและไม>ม1การแปลงชน ดของข$อม&ลแล$วจะท@าหน$าท16เป=น
การรวมก5นระหว>างค>าท5 Fง 2 เช>น TextBox1 ร5บค>าเป=น 10 และ TextBox2 ร5บค>าเป=น 15 ผลล5พธEท16ได$จากการบวกก5นก^คMอ
1015


                                                                          2
                                                                           3
                                                                            4




                                                    5            6              7

ตาราง ก@าหนด Properties ท16สาค5ญของแต>ละ Control
                            @
 No.       Control                    Properties                                    Value
  1 Form                      Name                      Form1
                              Text                      การแปลงข$อม&ลโดยไม>ใช> Method
                              StartPosition             CenterScreen
                              MaximizeBox               False
                              MinimumBox                False
  2 TextBox                   Name                      txtNum1
                              TextAlign                 Center
  3 TextBox                   Name                      txtNum2
                              TextAlign                 Center
  4 TextBox                   Name                      txtResult
                              TextAlign                 Center
                              ReadOnly                  True
  5 Button                    Name                      btnCalculatorAdd
                              Text                      ค@านวณบวก



                                                          -3-
VB .NET 2005 By Warawut Khangkhan
การแปลงชนดของข$อม&ลใน VB.NET 2005 express Edition


ตาราง (ต>อ) ก@าหนด Properties ท16สาค5ญของแต>ละ Control
                                  @
 No.        Control                 Properties                                 Value
  6 Button                 Name                        btnClear
                           Text                        เคล1ยรEคา>
  7 Button                 Name                        btnExit
                           Text                        ออก

Coding:

Public Class Form1

  Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
     Application.Exit()
  End Sub

  Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
     txtNum1.Text = "0"
     txtNum2.Text = "0"
     txtResult.Text = "0"
  End Sub

  Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
     txtNum1.Text = "0"
     txtNum2.Text = "0"
     txtResult.Text = "0"
  End Sub

  Private Sub btnCalculatorAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
btnCalculatorAdd.Click
     txtResult.Text = txtNum1.Text + txtNum2.Text
  End Sub

  Private Sub txtNum1_Enter(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtNum1.Enter
     txtNum1.SelectionStart = 0
     txtNum1.SelectionLength = txtNum1.Text.Length
  End Sub

  Private Sub txtNum2_Enter(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtNum2.Enter
     txtNum2.SelectionStart = 0
     txtNum2.SelectionLength = txtNum2.Text.Length
  End Sub




                                                          -4-
VB .NET 2005 By Warawut Khangkhan
การแปลงชนดของข$อม&ลใน VB.NET 2005 express Edition


  Private Sub txtNum1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles
txtNum1.KeyDown
     If e.KeyCode = Keys.Enter Then txtNum2.Focus()
  End Sub

  Private Sub txtNum2_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs)
Handles txtNum2.KeyDown
     If e.KeyCode = Keys.Enter Then btnCalculatorAdd.Focus()
  End Sub
End Class


Test Program:




ต7วอย5าง การเข;ยนโปรแกรมในการแปลงชนดของขอมล โดยใช Method
        เราจะใช$ต5วอย>างเก16ยวก5บการค@านวณโดยการบวกเลข ซ<6งหน$าจอและการก@าหนดคPณสมบ5ต (Properties) จะม1ล5กษณะ
คล$ายด5งต5วอย>างข$างต$น เพ1ยงแต>ได$ท@าการแก$ไข Code เพ1ยงบางส>วนเท>าน5น
                                                                      F
        ท@าการแก$ไข Code ท16ปP{ม “ค@านวณบวก” จาก Code เดม คMอ
                 txtResult.Text = txtNum1.Text + txtNum2.Text
        แก$ไขใหม>เป=น
                  txtResult.Text = Convert.ToInt32(txtNum1.Text) + Convert.ToInt32(txtNum2.Text)
       เพ1 ย งเท> าน1 F โปรแกรมของเราก^ ส มบ& รณE แล$ ว ถ$ า เราทดสอบอ1 ก คร5 F ง โดยก@ า หนดให$ TextBox1 ร5 บค> า เป= น 10 และ
TextBox2 ร5บค>าเป=น 15 ผลล5พธEทได$จากการบวกก5นก^คอ 25 ท5นท1
                                 16                     M

Test Program:




                                                             -5-

More Related Content

More from Warawut

Database design
Database designDatabase design
Database designWarawut
 
Business Computer Project 4
Business Computer Project 4Business Computer Project 4
Business Computer Project 4Warawut
 
Object-Oriented Programming 10
Object-Oriented Programming 10Object-Oriented Programming 10
Object-Oriented Programming 10Warawut
 
Object-Oriented Programming 9
Object-Oriented Programming 9Object-Oriented Programming 9
Object-Oriented Programming 9Warawut
 
Object-Oriented Programming 8
Object-Oriented Programming 8Object-Oriented Programming 8
Object-Oriented Programming 8Warawut
 
Object-Oriented Programming 7
Object-Oriented Programming 7Object-Oriented Programming 7
Object-Oriented Programming 7Warawut
 
Object-Oriented Programming 6
Object-Oriented Programming 6Object-Oriented Programming 6
Object-Oriented Programming 6Warawut
 
Management Information System 6
Management Information System 6Management Information System 6
Management Information System 6Warawut
 
Management Information System 5
Management Information System 5Management Information System 5
Management Information System 5Warawut
 
Management Information System 4
Management Information System 4Management Information System 4
Management Information System 4Warawut
 
Object-Oriented Programming 5
Object-Oriented Programming 5Object-Oriented Programming 5
Object-Oriented Programming 5Warawut
 
Business Computer Project 3
Business Computer Project 3Business Computer Project 3
Business Computer Project 3Warawut
 
Management Information System 3
Management Information System 3Management Information System 3
Management Information System 3Warawut
 
Business Computer Project 2
Business Computer Project 2Business Computer Project 2
Business Computer Project 2Warawut
 
Chapter 2 Strategy & Information System
Chapter 2 Strategy & Information SystemChapter 2 Strategy & Information System
Chapter 2 Strategy & Information SystemWarawut
 
Object-Oriented Programming 4
Object-Oriented Programming 4Object-Oriented Programming 4
Object-Oriented Programming 4Warawut
 
Business Computer Project 1
Business Computer Project 1Business Computer Project 1
Business Computer Project 1Warawut
 
Chapter 1 Organization & MIS
Chapter 1 Organization & MISChapter 1 Organization & MIS
Chapter 1 Organization & MISWarawut
 
Object-Oriented Programming 3
Object-Oriented Programming 3Object-Oriented Programming 3
Object-Oriented Programming 3Warawut
 
Object-Oriented Programming 2
Object-Oriented Programming 2Object-Oriented Programming 2
Object-Oriented Programming 2Warawut
 

More from Warawut (20)

Database design
Database designDatabase design
Database design
 
Business Computer Project 4
Business Computer Project 4Business Computer Project 4
Business Computer Project 4
 
Object-Oriented Programming 10
Object-Oriented Programming 10Object-Oriented Programming 10
Object-Oriented Programming 10
 
Object-Oriented Programming 9
Object-Oriented Programming 9Object-Oriented Programming 9
Object-Oriented Programming 9
 
Object-Oriented Programming 8
Object-Oriented Programming 8Object-Oriented Programming 8
Object-Oriented Programming 8
 
Object-Oriented Programming 7
Object-Oriented Programming 7Object-Oriented Programming 7
Object-Oriented Programming 7
 
Object-Oriented Programming 6
Object-Oriented Programming 6Object-Oriented Programming 6
Object-Oriented Programming 6
 
Management Information System 6
Management Information System 6Management Information System 6
Management Information System 6
 
Management Information System 5
Management Information System 5Management Information System 5
Management Information System 5
 
Management Information System 4
Management Information System 4Management Information System 4
Management Information System 4
 
Object-Oriented Programming 5
Object-Oriented Programming 5Object-Oriented Programming 5
Object-Oriented Programming 5
 
Business Computer Project 3
Business Computer Project 3Business Computer Project 3
Business Computer Project 3
 
Management Information System 3
Management Information System 3Management Information System 3
Management Information System 3
 
Business Computer Project 2
Business Computer Project 2Business Computer Project 2
Business Computer Project 2
 
Chapter 2 Strategy & Information System
Chapter 2 Strategy & Information SystemChapter 2 Strategy & Information System
Chapter 2 Strategy & Information System
 
Object-Oriented Programming 4
Object-Oriented Programming 4Object-Oriented Programming 4
Object-Oriented Programming 4
 
Business Computer Project 1
Business Computer Project 1Business Computer Project 1
Business Computer Project 1
 
Chapter 1 Organization & MIS
Chapter 1 Organization & MISChapter 1 Organization & MIS
Chapter 1 Organization & MIS
 
Object-Oriented Programming 3
Object-Oriented Programming 3Object-Oriented Programming 3
Object-Oriented Programming 3
 
Object-Oriented Programming 2
Object-Oriented Programming 2Object-Oriented Programming 2
Object-Oriented Programming 2
 

Convert Data type

  • 1. VB .NET 2005 By Warawut Khangkhan การแปลงชนดของข$อม&ลใน VB.NET 2005 express Edition การแปลงชนดของขอมลใน VB .NET 2005 Express Edition ในการเข1ยนโปรแกรมโดยท56วไป เราอาจต$องการแปลงข$อม&ลจากชนดหน<6งไปเป=นอ1กชนดหน<6ง เช>น ถ$าเราม1 TextBox 2 ต5ว ท16ท@าหน$าท16ในการร5บค>าจากแปCนพมพE แล$วเราน@า TextBox ท5Fง 2 มารวมก5น (+) จะท@าให$ผลล5พธEท16เราต$องการผดพลาดได$ เนM6องจากเครM6องหมายบวก (+) เป=นการรวมก5นระหว>าง TextBox ท5Fงสอง เพราะฉะน5Fนเราจ@าเป=นต$องท@าการแปลงชน ดของ ข$อม&ลใน TextBox ท5Fง 2 เป=นชนดข$อม&ลต5วเลขก>อน ถ<งจะได$ผลล5พธEท16ถ&กต$อง ใน VB .NET สามารถแปลงชน ดของข$อม&ลได$อย>า งง> า ยดาย เนM 6อ งจากข$อม&ลทPกชน ดใน VB .NET ถMอ เป=น Object ประเภทหน<6ง ซ<6งม1เมธอด (Method) หรMอฟYงกEช56น (Function) ท1ใช$แปลงชนดข$อม&ลอย&>ภายในเร1ยบร$อยแล$ว 6 ต5วอย>างเช>น ถ$าเราต$องการแปลงชน ดข$อม&ลของต5วแปร A ซ<6งเป=นต5วแปรชน ด Single ไปเป=นข$อม&ลชน ด String และ เก^บผลล5พธEไว$ในต5วแปร B จะท@าได$ด5งน1F Dim A As Single = 123.45 'ประกาศต5วแปร A ให$เป=นต5วแปรชนด Single Dim B As String 'ประกาศต5วแปร B ให$เป=นต5วแปรชนด String B = A.ToString 'แปลงข$อม&ลของต5วแปร A จากชนด Single ไปเป=น String และเก^บผลล5พธE ไว$ในต5วแปร B Method ท5Fงหมดท1ใช$แปลงชนดข$อม&ลจะอย&>ในคลาส System.Convert ซ<6งเราสามารถเร1ยกโดยใช$ร&ปแบบ 6 รปแบบ <ชอตวแปร>.<ชอเมธอด> หรMอ System.Convert.<ชอเมธอด>(<ชอตวแปร>) จากต5วอย>าง B = A.ToString เราอาจเข1ยนอ1กร&ปแบบหน<6งได$ด5งน1F System.Convert.ToString(A) ก^ได$เช>นก5น ตาราง สรPป Method ท16สาค5ญท1ใช$แปลงชนดของข$อม&ล @ 6 Method Description Example Program Result ToString แปลงข$อม&ลชนดต5วเลข (numeric) Dim varString As String 'varString = หรMอ Date ให$เป=นต5วอ5กษร Dim varDate As Date = Now( ) “11/12/2008 17:33” Dim varDouble As Double = 123.45 'varString = “123.45” varString = varDate.ToString varString = varDouble.ToString ToBool แปลงข$อม&ลชนดสตรงให$เป=น Boolean Dim varString As String = “True” 'varBoolean = True Dim varBoolean As Boolean varBoolean = varString.ToBool ToChar แปลงข$อม&ลชนดต5วเลขให$เป=น Char Dim varByte As Byte = 80 'varChar = 'P' Dim varChar As Char varChar = Convert.ToChar(varByte) -1-
  • 2. VB .NET 2005 By Warawut Khangkhan การแปลงชนดของข$อม&ลใน VB.NET 2005 express Edition ตาราง (ต5อ) สรPป Method ท16สาค5ญท1ใช$แปลงชนดของข$อม&ล @ 6 Method Description Example Program Result ToByte แปลงเป=นข$อม&ลชนด Byte Dim varChar As Char = “P” 'varByte = 80 Dim varByte As Byte varByte = Convert.ToByte(varChar) ToDateTime แปลงเป=นข$อม&ลชนด DateTime Dim varString As String = 'varDateTime = “01/02/2002” 01/02/2002 Dim varDateTime As DateTime varDateTime = Convert.ToDateTime(varString) ToInt16 แปลงเป=นข$อม&ลอM6น ๆ ให$เป=น Short Dim varSring As String = “1234” 'varShort = 1234 Dim varShort As Short varShort = Convert.ToInt16(varString) ToInt32 แปลงเป=นข$อม&ลอM6น ๆ ให$เป=น Integer Dim varInt As Integer 'varInt = 3500 Dim varLong As Long = 3500 'varInt = 123 Dim varSingle As Single = 123.4567 varInt = Convert.ToInt32(varLong) varInt = Convert.ToInt32(varSingle) ToInt64 แปลงเป=นข$อม&ลอM6น ๆ ให$เป=น Long Dim varLong As Long 'varLong = 699999999 Dim varInt As Integer = 'varLong = 123 699999999 Dim varSingle As Single = 123.456 varLong = Convert.ToInt64(varInt) varLong = Convert.ToInt64(varSingle) ToSingle แปลงเป=นข$อม&ลอM6น ๆ ให$เป=น Single Dim varSingle As Single 'varSingle = 6999 Dim varInt As Integer = 6999 varSingle = Convert.ToSingle(varInt) ToDouble แปลงเป=นข$อม&ลอM6น ๆ ให$เป=น Double Dim varDouble As Double 'varDouble = Dim varSingle = 6999999999999.99 “6999999999999.99” varDouble = Convert.ToDouble(varSingle) -2-
  • 3. VB .NET 2005 By Warawut Khangkhan การแปลงชนดของข$อม&ลใน VB.NET 2005 express Edition ต7วอย5าง การเข;ยนโปรแกรมในการแปลงชนดของขอมล โดยไม5ใช Method ต5วอย>างท16จะใช$น1Fเป=นต5วอย>างท16เก16ยวก5บการค@านวณโดยการบวกเลข ซ<6งเป=นการร5บข$อม&ลจากแปCนพมพEมา 2 ค>า โดย Control ท16ใช$ในการร5บค>าก^คMอ TextBox ซ<6งการค@านวณด$วยวธ1การบวกและไม>ม1การแปลงชน ดของข$อม&ลแล$วจะท@าหน$าท16เป=น การรวมก5นระหว>างค>าท5 Fง 2 เช>น TextBox1 ร5บค>าเป=น 10 และ TextBox2 ร5บค>าเป=น 15 ผลล5พธEท16ได$จากการบวกก5นก^คMอ 1015 2 3 4 5 6 7 ตาราง ก@าหนด Properties ท16สาค5ญของแต>ละ Control @ No. Control Properties Value 1 Form Name Form1 Text การแปลงข$อม&ลโดยไม>ใช> Method StartPosition CenterScreen MaximizeBox False MinimumBox False 2 TextBox Name txtNum1 TextAlign Center 3 TextBox Name txtNum2 TextAlign Center 4 TextBox Name txtResult TextAlign Center ReadOnly True 5 Button Name btnCalculatorAdd Text ค@านวณบวก -3-
  • 4. VB .NET 2005 By Warawut Khangkhan การแปลงชนดของข$อม&ลใน VB.NET 2005 express Edition ตาราง (ต>อ) ก@าหนด Properties ท16สาค5ญของแต>ละ Control @ No. Control Properties Value 6 Button Name btnClear Text เคล1ยรEคา> 7 Button Name btnExit Text ออก Coding: Public Class Form1 Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click Application.Exit() End Sub Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load txtNum1.Text = "0" txtNum2.Text = "0" txtResult.Text = "0" End Sub Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click txtNum1.Text = "0" txtNum2.Text = "0" txtResult.Text = "0" End Sub Private Sub btnCalculatorAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculatorAdd.Click txtResult.Text = txtNum1.Text + txtNum2.Text End Sub Private Sub txtNum1_Enter(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtNum1.Enter txtNum1.SelectionStart = 0 txtNum1.SelectionLength = txtNum1.Text.Length End Sub Private Sub txtNum2_Enter(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtNum2.Enter txtNum2.SelectionStart = 0 txtNum2.SelectionLength = txtNum2.Text.Length End Sub -4-
  • 5. VB .NET 2005 By Warawut Khangkhan การแปลงชนดของข$อม&ลใน VB.NET 2005 express Edition Private Sub txtNum1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtNum1.KeyDown If e.KeyCode = Keys.Enter Then txtNum2.Focus() End Sub Private Sub txtNum2_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtNum2.KeyDown If e.KeyCode = Keys.Enter Then btnCalculatorAdd.Focus() End Sub End Class Test Program: ต7วอย5าง การเข;ยนโปรแกรมในการแปลงชนดของขอมล โดยใช Method เราจะใช$ต5วอย>างเก16ยวก5บการค@านวณโดยการบวกเลข ซ<6งหน$าจอและการก@าหนดคPณสมบ5ต (Properties) จะม1ล5กษณะ คล$ายด5งต5วอย>างข$างต$น เพ1ยงแต>ได$ท@าการแก$ไข Code เพ1ยงบางส>วนเท>าน5น F ท@าการแก$ไข Code ท16ปP{ม “ค@านวณบวก” จาก Code เดม คMอ txtResult.Text = txtNum1.Text + txtNum2.Text แก$ไขใหม>เป=น txtResult.Text = Convert.ToInt32(txtNum1.Text) + Convert.ToInt32(txtNum2.Text) เพ1 ย งเท> าน1 F โปรแกรมของเราก^ ส มบ& รณE แล$ ว ถ$ า เราทดสอบอ1 ก คร5 F ง โดยก@ า หนดให$ TextBox1 ร5 บค> า เป= น 10 และ TextBox2 ร5บค>าเป=น 15 ผลล5พธEทได$จากการบวกก5นก^คอ 25 ท5นท1 16 M Test Program: -5-