如何向我的文本框添加热键功能 [英] How to add hot key function to my TextBox

查看:90
本文介绍了如何向我的文本框添加热键功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好.我有一段VB代码.我想做的是让TextBox适应某些字符.但是,在使用以下代码后,像"Ctrl + V","Ctrl + C"之类的热键不再起作用.如何修改以下代码,使文本框支持键盘上的热键?

Hey, everybody. I have a piece of VB code. What I want to do is to let the TextBox to accpet certain charaters. However, after I use the following code, the hot key like "Ctrl + V", "Ctrl + C", etc are no longer working. How can I modify the following code to let the Textbox support the hot keys from the Keyboard?

Private Sub TextBox1_KeyPress(ByVal sender As   Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles   TextBox1.KeyPress
    Try
        If Char.IsDigit(e.KeyChar) Or e.KeyChar = ". "   Or e.KeyChar = Chr(8) Then
            If e.KeyChar = ". " And InStr(Me.Text, ". ") > 0 Then
                   e.Handled = True
            Else
                   e.Handled = False
            End If
         Else
            e.Handled = True
         End If
     Catch ex As Exception
     End Try
End Sub


提前非常感谢您

[修改:使用前置标签!!!!并花费一些额外的时间来正确设置代码格式(例如,删除多余的空格并修复标签).这样,您将获得更多的响应,并且它们往往会更有帮助]


Thank you very much in advance

[Modifed: use pre tags!!!! and take the little extra time to properly format the code (as in remove extra spaces and fix tabs). You''ll get more responses that way and they''ll tend to be more helpful]

推荐答案

cnatsa71写道:
cnatsa71 wrote:

否则e.Handled =如果为True,则结束

Else e.Handled = True End If


不确定-请尝试删除上面的e.Handled代码...


Not sure - but try by removing the e.Handled code above...


由于您是通过"KeyPress"执行操作的,因此必须检查Ctrl + V
事件:
Since you''re doing things by ''KeyPress'' you''ll have to check the Ctrl+V
event:
Private Sub TextBox1_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown

        If (e.Control) Then
            If (e.KeyCode = Keys.V) Then
                Text1 = Clipboard.GetText
                TextBox1.SelectedText = Text1
            End If
        End If

    End Sub


检查Text1是否全为数字.如果是这样,则将它们放在TextBox1中.


Check if Text1 are all numbers. If so, put them in TextBox1.


Abhinav说过您的问题所在,但我不明白您认为您正在使用该代码.

首先,除非Me.Text不存在,否则该代码中绝对不会抛出异常.这意味着您在那里不需要Try/Catch块.其次,不要将所有内容放在尝试/捕获"块中.如果要使用旧方法,.NET仍然会出现On Error Goto垃圾邮件. Try/Catch旨在允许您处理可能引发错误的特定语句.

现在,您的代码到底是做什么的? (因为它没有多大意义)

这是您的If语句当前的工作方式...

e.KeyChar是数字吗?或者,e.KeyChar是否等于字符串."? (永远不会因为单个字符不能等于两个字符而已)还是e.KeyChar等于8的Char表示形式? (您已经检查过它是否为数字,那么为什么还要检查它是否为特定数字?)(实际上,我刚刚意识到您正在检查退格键)

   如果其中任何一个为真,则检查e.KeyChar是否等于."
   (再也不会这样)或Me.Text是否包含."?

       如果其中任何一个是正确的,则不允许按键


您真正允许KeyPress的唯一时间是,如果按下的键是一个数字,并且Me.Text中没有.".

另一个答案显示了如何检查Ctrl + V,但真正的问题是您到底想使用该代码做什么???
Abhinav said what your problem was, but I don''t understand what you''re thought you were doing with that code.

First, absolutely nothing in that code could throw an exception, unless Me.Text doesn''t exist. That means that you do not need a Try/Catch block there. Secondly, don''t put everything in a Try/Catch block. If you want the old method, .NET still has the On Error Goto junk. Try/Catch is meant to allow you to handle specific statements that could throw an error.

Now, what exactly does your code do? (because it doesn''t make a lot of sense)

Here''s how your If statements currently work...

Is e.KeyChar a digit? Or, is e.KeyChar equal to the string ". "? (which it never will be because a single char can''t be equal to two characters) Or is e.KeyChar equal to the Char representation of 8? (which you already checked to see if it was a digit so why would you check if it was a specific digit?) (Actually, I just realized that you were checking for backspace)

    If any one of those is true, then check if e.KeyChar is equal to ". "
    (which again it will never be) or does Me.Text contain ". "?

       If any of those are true, don''t allow a keypress


The only time you actually allow a KeyPress is if the key pressed was a digit and Me.Text didn''t have ". " in it.

The other answer showed you how to check for Ctrl+V, but the real issue is What in the world were you trying to do with that code????


这篇关于如何向我的文本框添加热键功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆