ชุมชนคนรักภาษาเบสิค - Visual Basic Community

 ลืมรหัสผ่าน
 ลงทะเบียน
ค้นหา
ดู: 9504|ตอบกลับ: 1

[VB.NET] การป้อนค่าลง TextBox เพื่อให้รับค่าเฉพาะที่ต้องการได้เท่านั้น

[คัดลอกลิงก์]

308

กระทู้

499

โพสต์

6025

เครดิต

ผู้ดูแลระบบ

ทองก้อน ทับทิมกรอบ

Rank: 9Rank: 9Rank: 9

เครดิต
6025

ตัวอย่างนี้จะให้สามารถกดได้เพียงตัวอักขระ (Character) คือ A - Z, 0 - 9 เครื่องหมาย - และ _ ได้เท่านั้น
  1. Private Sub TextBox1_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
  2.         '// ดักค่าการกดคีย์ที่เราต้องการ
  3.         '// ต้องการ A - Z, 0 - 9 อักขระพิเศษ - และ _
  4.         e.Handled = SpecialChar(Asc(e.KeyChar))
  5.     End Sub

  6.     Function SpecialChar(index As Byte) As Boolean
  7.         SpecialChar = True
  8.         Select Case index
  9.             '// 48 To 57 คือรหัสแอสกี้โค้ดตั้งแต่ '0' ถึง '9'
  10.             Case 48 To 57, Asc("A") To Asc("Z"), Asc("_"), Asc("-")
  11.                 SpecialChar = False
  12.             Case 8 ' Back Space
  13.                 SpecialChar = False
  14.             Case 13 ' Enter
  15.                 SpecialChar = False

  16.                 '// ตัวที่ไม่อยู่ในรายการให้ Return True
  17.             Case Else
  18.                 SpecialChar = True
  19.         End Select

  20.     End Function
คัดลอกไปที่คลิปบอร์ด

สิ่งที่ดีกว่าการให้ คือการให้แบบไม่มีที่สิ้นสุด

11

กระทู้

31

โพสต์

427

เครดิต

ผู้ดูแลบอร์ด

Rank: 7Rank: 7Rank: 7

เครดิต
427
โพสต์ 2018-5-7 10:05:50 | ดูโพสต์ทั้งหมด

แก้ไขครั้งสุดท้ายโดย puklit เมื่อ 2018-5-9 10:39

ผมขออนุญาตอาจารย์ทองก้อนเพิ่มอีกรูปแบบครับ
การระบุประเภทข้อความให้กับ Textbox ในรูปแบบ Class
โดยหากต้องการข้อมูลประเภทไหนก็ให้ Textbox เรียกใช้ผ่าน Class Clstextbox โดยใช้ Events KeyPress ดังนี้ครับ

ตัวอย่างฟอร์ม
Input_specific_values.PNG

โค้ดของ Class Clstextbox
  1. Public Class ClsTextbox

  2.     ' ระบุประเภทรูปแบบของ Textbox
  3.     Public Enum Text_type

  4.         Number_Only
  5.         Number_with_dot
  6.         Thai_alphabet
  7.         Thai_alphabet_with_number
  8.         English_alphabet
  9.         English_alphabet_with_number

  10.     End Enum

  11.     ' กำหนดรูปแบบข้อความของ Textbox แต่ละประเภท
  12.     Public Shared Sub Set_textbox_value(sender As Object, e As KeyPressEventArgs, Value_Type As Text_type)

  13.         Dim key_ascii As Byte
  14.         key_ascii = Asc(e.KeyChar) ' รับค่า e.keychar จาก textbox เป็น Key ascii

  15.         ' อนุญาตให้ใช้ปุ่ม Backspace = 8 / Enter = 13
  16.         If key_ascii = 8 Or key_ascii = 13 Then
  17.             Exit Sub
  18.         End If

  19.         Select Case (Value_Type)

  20.             Case Text_type.Number_Only

  21.                 If key_ascii >= 48 And key_ascii <= 57 Then ' 48 = 0 - 57 = 9
  22.                     e.Handled = False
  23.                 Else
  24.                     e.Handled = True
  25.                 End If

  26.             Case Text_type.Number_with_dot

  27.                 If ((key_ascii >= 48 And key_ascii <= 57) Or key_ascii = 46) Then ' 48 = 0 - 57 = 9 / 46 - "."
  28.                     e.Handled = False
  29.                 Else
  30.                     e.Handled = True
  31.                 End If

  32.             Case Text_type.Thai_alphabet

  33.                 If key_ascii >= 161 And key_ascii <= 240 Then ' ภาษาไทยรวมทั้งตัวสระ + วรรณยุกต์
  34.                     e.Handled = False
  35.                 Else
  36.                     e.Handled = True
  37.                 End If

  38.             Case Text_type.Thai_alphabet_with_number

  39.                 If key_ascii >= 48 And key_ascii <= 57 Then ' 48 = 0 - 57 = 9
  40.                     e.Handled = False
  41.                 ElseIf key_ascii >= 161 And key_ascii <= 240 Then
  42.                     e.Handled = False
  43.                 Else
  44.                     e.Handled = True
  45.                 End If

  46.             Case Text_type.English_alphabet

  47.                 If key_ascii >= 58 And key_ascii <= 122 Then ' ภาษาอังกฤษ 58 - 122
  48.                     e.Handled = False
  49.                 Else
  50.                     e.Handled = True
  51.                 End If

  52.             Case Text_type.English_alphabet_with_number

  53.                 If key_ascii >= 48 And key_ascii <= 57 Then ' 48 = 0 - 57 = 9
  54.                     e.Handled = False
  55.                 ElseIf key_ascii >= 58 And key_ascii <= 122 Then ' ภาษาอังกฤษ 58 - 122
  56.                     e.Handled = False
  57.                 Else
  58.                     e.Handled = True
  59.                 End If

  60.         End Select

  61.     End Sub


  62. End Class
คัดลอกไปที่คลิปบอร์ด


กรณีที่ต้องการกำหนดให้ Textbox ใช้รูปแบบข้อความที่สามารถป้อนได้ในแต่ละรูปแบบ ให้เรียกใช้ผ่าน Class Clstextbox โดยใช้ Events KeyPress ดังนี้
  1. Public Class Form1

  2.     ' Number_Only
  3.     Private Sub TextBox1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TextBox1.KeyPress
  4.         ClsTextbox.Set_textbox_value(sender, e, ClsTextbox.Text_type.Number_Only)
  5.     End Sub

  6.     'Number_with_dot
  7.     Private Sub TextBox2_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TextBox2.KeyPress
  8.         ClsTextbox.Set_textbox_value(sender, e, ClsTextbox.Text_type.Number_with_dot)
  9.     End Sub

  10.     ' Thai_alphabet
  11.     Private Sub TextBox3_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TextBox3.KeyPress
  12.         ClsTextbox.Set_textbox_value(sender, e, ClsTextbox.Text_type.Thai_alphabet)
  13.     End Sub

  14.     ' Thai_alphabet_with_number
  15.     Private Sub TextBox4_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TextBox4.KeyPress
  16.         ClsTextbox.Set_textbox_value(sender, e, ClsTextbox.Text_type.Thai_alphabet_with_number)
  17.     End Sub

  18.     ' English_alphabet
  19.     Private Sub TextBox5_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TextBox5.KeyPress
  20.         ClsTextbox.Set_textbox_value(sender, e, ClsTextbox.Text_type.English_alphabet)
  21.     End Sub

  22.     ' English_alphabet_with_number
  23.     Private Sub TextBox6_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TextBox6.KeyPress
  24.         ClsTextbox.Set_textbox_value(sender, e, ClsTextbox.Text_type.English_alphabet_with_number)
  25.     End Sub
คัดลอกไปที่คลิปบอร์ด


ท่านที่สนใจสามารถดาวน์โหลดโค้ด VB2017 ได้ที่นี่ครับ
Textbox_valueVB.rar (186.68 KB, ดาวน์โหลดแล้ว: 12028)

0

กระทู้

4

โพสต์

30

เครดิต

Newbie

Rank: 1

เครดิต
30
โพสต์ 2018-5-7 12:30:50 | ดูโพสต์ทั้งหมด

ขอบคุณครับอาจารย์ thongkorn และคุณ puklit
ขออภัย! คุณไม่ได้รับสิทธิ์ในการดำเนินการในส่วนนี้ กรุณาเลือกอย่างใดอย่างหนึ่ง ลงชื่อเข้าใช้ | ลงทะเบียน

รายละเอียดเครดิต

ข้อความล้วน|อุปกรณ์พกพา|ประวัติการแบน|G2GNet.com  

GMT+7, 2024-4-19 09:34 , Processed in 0.296620 second(s), 5 queries , File On.

Powered by Discuz! X3.4, Rev.62

Copyright © 2001-2020 Tencent Cloud.

ตอบกระทู้ ขึ้นไปด้านบน ไปที่หน้ารายการกระทู้