 |
|
[.NET]
C# winapp Keyboard คีย์ได้เฉพาะภาษาอังกฤษเท่านั้น หรือใครพอมี ตย โค้ดขอแชร์หน่อย |
|
 |
|
|
 |
 |
|
คือผมลองเอาโค้ดจาก
https://www.thaicreate.com/community/vb.net-validate-keyboard-thai-english.html
มาแปลงในเว็บ Convert VB to C#
แล้วได้โค้ดชุดนี้มา
Code (C#)
01. private void TextBox2_KeyPress( object sender, System.Windows.Forms.KeyPressEventArgs e)
02. {
03.
04. switch (Strings.Asc(e.KeyChar)) {
05.
06. case 48:
07.
08.
09. e.Handled = false ;
10. break ;
11.
12. case 8:
13. case 13:
14. case 46:
15.
16.
17. e.Handled = false ;
18. break ;
19.
20. default :
21.
22. e.Handled = true ;
23.
24. MessageBox.Show( "กรุณาระบุข้อมูลเป็นภาษาอังกฤษ" );
25. break ;
26.
27. }
28.
29. }
คราวนี้ติดปัญหาตรง Errors Strings.Asc(e.KeyChar)
ลองเปลี่ยนเป็น String ของเครื่องแล้วก็ Errors .Asc อีก
เลยงง ผมจะแก้ยังไง หรือเขียนยังไงดี
Tag : .NET, Ms SQL Server 2008, Crystal Report, Win (Windows App), C#
|
ประวัติการแก้ไข 2012-03-16 16:59:03
|
 |
 |
 |
 |
Date :
2012-03-16 16:58:18 |
By :
SeedNew |
View :
4967 |
Reply :
9 |
|
 |
 |
 |
 |
|
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
เหมือนเดิมครับพี่

ลองเขียน แบบมี String ไว้ด้านหน้า แล้วพิมพ์ . ดูมันไม่มีรู้จัก ASC ตามรูปครับ

|
 |
 |
 |
 |
Date :
2012-03-17 09:36:17 |
By :
SeedNew |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ลองนี้นะครับ
Code (C#)
01. private void textBox1_KeyPress( object sender, KeyPressEventArgs e)
02. {
03.
04. switch (e.KeyChar)
05. {
06. case ( char )48:
07. e.Handled = false ;
08. break ;
09.
10. case ( char )8:
11. case ( char )13:
12. case ( char )46:
13.
14. e.Handled = false ;
15. break ;
16.
17. default :
18. e.Handled = true ;
19. MessageBox.Show( "กรุณาระบุข้อมูลเป็นภาษาอังกฤษ" );
20. break ;
21. }
22. }
หรือว่าจะ
Code (C#)
01. private void textBox1_KeyPress( object sender, KeyPressEventArgs e)
02. {
03.
04. switch (( int )e.KeyChar)
05. {
06. case 48:
07. e.Handled = false ;
08. break ;
09.
10. case 8:
11. case 13:
12. case 46:
13.
14. e.Handled = false ;
15. break ;
16.
17. default :
18. e.Handled = true ;
19. MessageBox.Show( "กรุณาระบุข้อมูลเป็นภาษาอังกฤษ" );
20. break ;
21. }
22. }
ฝากกด like หน่อยนะครับ
http://www.facebook.com/pages/PStudio-Development-Store/284410714927365
|
ประวัติการแก้ไข 2012-03-17 20:27:33
 |
 |
 |
 |
Date :
2012-03-17 20:27:11 |
By :
pStudio |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Code (C#)
กรณีเป็น string จะต้องใส่ " " ด้วยครับ ในภาษา C# ให้ความสำคัญเรื่อง DataType มากครับ เป็นภาษาที่โครงสร้างค่อนข้างแข็งแรง ลดข้อผิดพลาดระหว่าง Runtime ได้เยอะเลย
|
 |
 |
 |
 |
Date :
2012-03-17 20:38:04 |
By :
webmaster |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
จากตัวอย่าง พี่ Mr .Win กับ ตัวอย่าง 2 ตัวอย่างพี่ pStudio
ผมลองทั้ง 3 ตัวไม่ผ่านซักตัวเลย
มันรันผ่านทุกตัวนะ แต่มันไม่อ่าน case โดดไปอ่าน default: ทุกตัวเลย
พอคีย์อะไป ไม่ว่าจะเป็นตัวเลข ภาษาไทย ภาษาอังกฤษ
จะมี MessageBox ขึ้นโชว์เหมือนในรูปข้างล่าง

Code (C#)
01. private void textBoxMemberEng_KeyPress( object sender, KeyPressEventArgs e)
02. {
03.
04.
05.
06.
07.
08.
09.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
36.
37.
38.
39.
40.
41.
42.
43.
44. switch (( int )e.KeyChar)
45. {
46. case 48:
47. e.Handled = false ;
48. break ;
49.
50. case 8:
51. case 13:
52. case 46:
53. e.Handled = false ;
54. break ;
55.
56. default :
57. e.Handled = true ;
58. MessageBox.Show( "กรุณาระบุข้อมูลเป็นภาษาอังกฤษ" );
59. break ;
60. }
61.
62. }
|
ประวัติการแก้ไข 2012-03-19 11:54:06 2012-03-19 12:24:41 2012-03-19 12:32:17
 |
 |
 |
 |
Date :
2012-03-19 11:50:27 |
By :
SeedNew |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ลองดูอันนี้นะครับ
Code (C#)
01. using System;
02. using System.Collections.Generic;
03. using System.ComponentModel;
04. using System.Data;
05. using System.Drawing;
06. using System.Linq;
07. using System.Text;
08. using System.Windows.Forms;
09.
10. namespace WindowsFormsApplication3
11. {
12. public partial class Form1 : Form
13. {
14. public Form1()
15. {
16. InitializeComponent();
17. }
18.
19. private void textBox1_KeyPress( object sender, KeyPressEventArgs e)
20. {
21.
22. if ((( int )e.KeyChar >= 48 && ( int )e.KeyChar <= 122) || ( int )e.KeyChar == 8 || ( int )e.KeyChar == 13 || ( int )e.KeyChar == 46)
23. {
24. e.Handled = false ;
25. }
26. else
27. {
28. e.Handled = true ;
29. }
30. }
31. }
32. }
|
ประวัติการแก้ไข 2012-03-19 23:41:27 2012-03-19 23:44:01
 |
 |
 |
 |
Date :
2012-03-19 23:40:48 |
By :
pStudio |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ได้แล้วครับ ขอบคุณพี่ pStudio กับ พี่ Mr .Win มากๆนะครับ 
|
 |
 |
 |
 |
Date :
2012-03-20 09:27:55 |
By :
SeedNew |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
เยี่ยมครับ กระทู้นี้มีประโยชน์แน่นอน 
|
 |
 |
 |
 |
Date :
2012-03-20 10:06:12 |
By :
webmaster |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ขอแชร์ต่อแล้วกันนะครับ มึนมาหลายวัน ตอนนี้ จำตัวเลข ได้เลย
คีย์ได้เฉพาะตัวเลข
Code (C#)
01. private void textBox1_KeyPress( object sender, KeyPressEventArgs e)
02. {
03. if ((( int )e.KeyChar >= 48 && ( int )e.KeyChar <= 57) || ( int )e.KeyChar == 8 || ( int )e.KeyChar == 13 || ( int )e.KeyChar == 46)
04. {
05. e.Handled = false ;
06. }
07.
08. else
09. {
10. e.Handled = true ;
11. MessageBox.Show( "สามารถกดได้แค่ตัวเลข !!!" , "ผลการตรวจสอบ" , MessageBoxButtons.OK, MessageBoxIcon.Warning);
12. textBox1.Focus();
13. textBox1.SelectAll();
14. }
15. }
คีย์ได้เฉพาะตัวเลข . - ,Code (C#)
01. private void textBox2_KeyPress( object sender, KeyPressEventArgs e)
02. {
03. if ((( int )e.KeyChar >= 44 && ( int )e.KeyChar <= 57) || ( int )e.KeyChar == 8 || ( int )e.KeyChar == 13 )
04. { {
05. e.Handled = false ;
06. }
07.
08. else
09. {
10. e.Handled = true ;
11. MessageBox.Show( "สามารถกดได้แค่ตัวเลข !!!" , "ผลการตรวจสอบ" , MessageBoxButtons.OK, MessageBoxIcon.Warning);
12. textBox2.Focus();
13. textBox2.SelectAll();
14. }
15. }
โค๊ดดักภาษาอังกฤษ
Code (C#)
01. private void textBox3_KeyPress( object sender, KeyPressEventArgs e)
02. {
03. if ((( int )e.KeyChar >= 48 && ( int )e.KeyChar <= 122) || ( int )e.KeyChar == 8 || ( int )e.KeyChar == 13 || ( int )e.KeyChar == 46 || ( int )e.KeyChar == 32)
04. {
05. e.Handled = false ;
06. }
07.
08. else
09. {
10. e.Handled = true ;
11. MessageBox.Show( "กรุณากรอกข้อมูลเป็นภาษาอังกฤษ !!!" , "ผลการตรวจสอบ" , MessageBoxButtons.OK, MessageBoxIcon.Warning);
12. textBox3.Focus();
13. textBox3.SelectAll();
14. }
15. }
โค้ดดักภาษาไทย
Code (C#)
01. private void textBox4_KeyPress( object sender, KeyPressEventArgs e)
02. {
03. if (( int )e.KeyChar >= 161 || ( int )e.KeyChar == 8 || ( int )e.KeyChar == 13 || ( int )e.KeyChar == 46 || ( int )e.KeyChar == 32)
04. {
05. e.Handled = false ;
06. }
07.
08. else
09. {
10. e.Handled = true ;
11. MessageBox.Show( "กรุณากรอกข้อมูลภาษาไทยเท่านั้น !!!" , "ผลการตรวจสอบ" , MessageBoxButtons.OK, MessageBoxIcon.Warning);
12. textBox4.Focus();
13. textBox4.SelectAll();
14. }
15. }
ขอขอบคุณกับกระทู้ https://www.thaicreate.com/dotnet/forum/071008.htmlที่แชร์ความรู้กับสิ่งดีๆ
|
ประวัติการแก้ไข 2012-03-20 10:10:45 2012-03-20 10:37:16 2012-03-20 11:32:30
 |
 |
 |
 |
Date :
2012-03-20 10:09:13 |
By :
SeedNew |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|