积极答复者
从按键值到字符串值的转换

问题
答案
-
不知道这样可以不?
死马当活马医了:)虽然代码有点啰嗦
1 ''' <summary>把键盘按键值转换为Char类型单字符</summary> 2 ''' <param name="Key">按键值</param> 3 ''' <param name="Shift">大小写状态</param> 4 ''' <returns></returns> 5 ''' <remarks></remarks> 6 Private Function KeyConvert(ByVal Key As System.Windows.Forms.Keys, Optional ByVal Shift As Boolean = False) As Char 7 If Shift Then 8 Select Case Key 9 Case Keys.A To Keys.Z '65-90 10 Return Chr(Key) 11 Case Keys.D0 12 Return ")"c 13 Case Keys.D1 14 Return "!"c 15 Case Keys.D2 16 Return "@"c 17 Case Keys.D3 18 Return "#"c 19 Case Keys.D4 20 Return "$"c 21 Case Keys.D5 22 Return "%"c 23 Case Keys.D6 24 Return "^"c 25 Case Keys.D7 26 Return "&"c 27 Case Keys.D8 28 Return "*"c 29 Case Keys.D9 30 Return "("c 31 Case Keys.NumPad0 To Keys.NumPad9 '96-105 32 Return Chr(Key - 48) 33 '------------------------- 34 Case Keys.Tab, Keys.Space '9'32 35 Return Chr(Key) 36 Case Keys.OemMinus '189 _ 37 Return Chr(95) 38 Case Keys.OemOpenBrackets '{ 39 Return "{"c 40 Case Keys.OemCloseBrackets '} 41 Return "}"c 42 Case Keys.OemPipe '220 | 43 Return Chr(124) 44 Case Keys.Oemcomma 45 Return "<"c 46 Case Keys.OemPeriod 47 Return ">"c 48 Case Keys.OemQuestion 49 Return "?"c 50 Case Keys.OemSemicolon ':; 51 Return ":"c 52 Case Keys.OemQuotes '" 53 Return Chr(34) 54 Case Keys.Multiply '106 55 Return Chr(42) '* 56 Case Keys.Add, Keys.Oemplus '107 187 57 Return Chr(43) '+ 58 Case Keys.Subtract '109 - 59 Return Chr(45) 60 End Select 61 62 Else 63 Select Case Key 64 Case Keys.A To Keys.Z '65-90 65 Return Chr(Key + 32) 66 Case Keys.D0 To Keys.D9 '48-57 67 Return Chr(Key) 68 Case Keys.NumPad0 To Keys.NumPad9 '96-105 69 Return Chr(Key - 48) 70 '------------------------- 71 Case Keys.Tab, Keys.Space '9'32 72 Return Chr(Key) 73 Case Keys.Subtract '- 74 Return Chr(45) 75 Case Keys.Oemplus '= 76 Return Chr(45) 77 Case Keys.OemOpenBrackets '[ 78 Return "["c 79 Case Keys.OemCloseBrackets '] 80 Return "]"c 81 Case Keys.OemPipe '220 \ 82 Return Chr(92) 83 Case Keys.OemSemicolon ':; 84 Return ";"c 85 Case Keys.OemQuotes '" 86 Return Chr(39) 87 Case Keys.Oemcomma 88 Return ","c 89 Case Keys.Decimal, Keys.OemPeriod '110 90 Return Chr(46) 91 Case Keys.Divide, Keys.OemQuestion '111 191 / 92 Return Chr(47) 93 Case Keys.Multiply '* 94 Return Chr(42) 95 Case Keys.Add '+ 96 Return Chr(43) 97 Case Keys.OemMinus '- 98 Return Chr(45) 99 End Select 100 End If 101 End Function - 已标记为答案 abcjackson 2009年2月1日 13:03
-
abcjackson 说:韦恩卑鄙 说:abcjackson 说:
文本框的.KeyDown事件中,参数e As System.Windows.Forms.KeyEventArgs可用获得按键的键盘值,已知该按键值限定在0-9和26个字母范围内,该值有没有现成的函数可以转换为字符呢?
谢谢:)
private void textBox1_KeyPress(object sender, KeyPressEventArgs e){
Console.WriteLine(e.KeyChar);}
参考这段代码 其实 KeyPress event 传递的就是你要的keychar
如果你执意要用key down event
就比较麻烦
ADO.net,很有趣
你说的是,不过KeyPress发生的时候,按键已经改变了文本框的Text,我还有TextChange事件要处理其他问题,所以考虑起来想在KeyDown事件中检查按键.谢谢
这个时候 其实只需要设置 textbox是readonly就可以了 然后根据keychar来操作textbox值 不必要一定用keydown
当然 你这个函数真的很用心呵呵
最近30天回答问题被论坛清除了 大家踊跃提问 踊跃标记正确 帮我重回top10阿~~~5555- 已标记为答案 abcjackson 2009年2月1日 13:03
全部回复
-
abcjackson 说:
文本框的.KeyDown事件中,参数e As System.Windows.Forms.KeyEventArgs可用获得按键的键盘值,已知该按键值限定在0-9和26个字母范围内,该值有没有现成的函数可以转换为字符呢?
谢谢:)
private void textBox1_KeyPress(object sender, KeyPressEventArgs e){
Console.WriteLine(e.KeyChar);}
参考这段代码 其实 KeyPress event 传递的就是你要的keychar
如果你执意要用key down event
就比较麻烦
ADO.net,很有趣 -
韦恩卑鄙 说:abcjackson 说:
文本框的.KeyDown事件中,参数e As System.Windows.Forms.KeyEventArgs可用获得按键的键盘值,已知该按键值限定在0-9和26个字母范围内,该值有没有现成的函数可以转换为字符呢?
谢谢:)
private void textBox1_KeyPress(object sender, KeyPressEventArgs e){
Console.WriteLine(e.KeyChar);}
参考这段代码 其实 KeyPress event 传递的就是你要的keychar
如果你执意要用key down event
就比较麻烦
ADO.net,很有趣
你说的是,不过KeyPress发生的时候,按键已经改变了文本框的Text,我还有TextChange事件要处理其他问题,所以考虑起来想在KeyDown事件中检查按键.谢谢
-
不知道这样可以不?
死马当活马医了:)虽然代码有点啰嗦
1 ''' <summary>把键盘按键值转换为Char类型单字符</summary> 2 ''' <param name="Key">按键值</param> 3 ''' <param name="Shift">大小写状态</param> 4 ''' <returns></returns> 5 ''' <remarks></remarks> 6 Private Function KeyConvert(ByVal Key As System.Windows.Forms.Keys, Optional ByVal Shift As Boolean = False) As Char 7 If Shift Then 8 Select Case Key 9 Case Keys.A To Keys.Z '65-90 10 Return Chr(Key) 11 Case Keys.D0 12 Return ")"c 13 Case Keys.D1 14 Return "!"c 15 Case Keys.D2 16 Return "@"c 17 Case Keys.D3 18 Return "#"c 19 Case Keys.D4 20 Return "$"c 21 Case Keys.D5 22 Return "%"c 23 Case Keys.D6 24 Return "^"c 25 Case Keys.D7 26 Return "&"c 27 Case Keys.D8 28 Return "*"c 29 Case Keys.D9 30 Return "("c 31 Case Keys.NumPad0 To Keys.NumPad9 '96-105 32 Return Chr(Key - 48) 33 '------------------------- 34 Case Keys.Tab, Keys.Space '9'32 35 Return Chr(Key) 36 Case Keys.OemMinus '189 _ 37 Return Chr(95) 38 Case Keys.OemOpenBrackets '{ 39 Return "{"c 40 Case Keys.OemCloseBrackets '} 41 Return "}"c 42 Case Keys.OemPipe '220 | 43 Return Chr(124) 44 Case Keys.Oemcomma 45 Return "<"c 46 Case Keys.OemPeriod 47 Return ">"c 48 Case Keys.OemQuestion 49 Return "?"c 50 Case Keys.OemSemicolon ':; 51 Return ":"c 52 Case Keys.OemQuotes '" 53 Return Chr(34) 54 Case Keys.Multiply '106 55 Return Chr(42) '* 56 Case Keys.Add, Keys.Oemplus '107 187 57 Return Chr(43) '+ 58 Case Keys.Subtract '109 - 59 Return Chr(45) 60 End Select 61 62 Else 63 Select Case Key 64 Case Keys.A To Keys.Z '65-90 65 Return Chr(Key + 32) 66 Case Keys.D0 To Keys.D9 '48-57 67 Return Chr(Key) 68 Case Keys.NumPad0 To Keys.NumPad9 '96-105 69 Return Chr(Key - 48) 70 '------------------------- 71 Case Keys.Tab, Keys.Space '9'32 72 Return Chr(Key) 73 Case Keys.Subtract '- 74 Return Chr(45) 75 Case Keys.Oemplus '= 76 Return Chr(45) 77 Case Keys.OemOpenBrackets '[ 78 Return "["c 79 Case Keys.OemCloseBrackets '] 80 Return "]"c 81 Case Keys.OemPipe '220 \ 82 Return Chr(92) 83 Case Keys.OemSemicolon ':; 84 Return ";"c 85 Case Keys.OemQuotes '" 86 Return Chr(39) 87 Case Keys.Oemcomma 88 Return ","c 89 Case Keys.Decimal, Keys.OemPeriod '110 90 Return Chr(46) 91 Case Keys.Divide, Keys.OemQuestion '111 191 / 92 Return Chr(47) 93 Case Keys.Multiply '* 94 Return Chr(42) 95 Case Keys.Add '+ 96 Return Chr(43) 97 Case Keys.OemMinus '- 98 Return Chr(45) 99 End Select 100 End If 101 End Function - 已标记为答案 abcjackson 2009年2月1日 13:03
-
abcjackson 说:韦恩卑鄙 说:abcjackson 说:
文本框的.KeyDown事件中,参数e As System.Windows.Forms.KeyEventArgs可用获得按键的键盘值,已知该按键值限定在0-9和26个字母范围内,该值有没有现成的函数可以转换为字符呢?
谢谢:)
private void textBox1_KeyPress(object sender, KeyPressEventArgs e){
Console.WriteLine(e.KeyChar);}
参考这段代码 其实 KeyPress event 传递的就是你要的keychar
如果你执意要用key down event
就比较麻烦
ADO.net,很有趣
你说的是,不过KeyPress发生的时候,按键已经改变了文本框的Text,我还有TextChange事件要处理其他问题,所以考虑起来想在KeyDown事件中检查按键.谢谢
这个时候 其实只需要设置 textbox是readonly就可以了 然后根据keychar来操作textbox值 不必要一定用keydown
当然 你这个函数真的很用心呵呵
最近30天回答问题被论坛清除了 大家踊跃提问 踊跃标记正确 帮我重回top10阿~~~5555- 已标记为答案 abcjackson 2009年2月1日 13:03