如何在VB.NET中使用enter键? [英] How to use enter key in VB.NET ?

查看:228
本文介绍了如何在VB.NET中使用enter键?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含大约10个文本框的表单。现在我需要允许用户按下回车键。



例如 - :

I have a form with about 10 text boxes . Now I need to allow the user to press the Enter key .

For Example -:

Textbox1.Text
Textbox2.Text
Textbox3.Text
Textbox4.Text
Textbox5.Text
Textbox6.Text
Textbox7.Text
Textbox8.Text
Textbox9.Text
Textbox10.Text



当我在第一个文本框中输入详细信息时,用户需要按ENTER。如果用户按ENTER键,光标将移动Textbox2.Again按Enter光标将进入Textbox 3.所以我需要为所有文本框制作likethis?我怎么能够 ?谁能帮我 ?谢谢....


When i enter the detail in first Textbox user need to press " ENTER ". If user press ENTER button cursor will moving Textbox2.Again press "Enter" cursor will going Textbox 3.So I need to make for all textboxes likethis ? How can i ? Can anyone help me ? Thank You....

推荐答案

在每个Textbox keydown事件上试试这个

你必须编写这段代码

try this
on every Textbox keydown event you will have to write this code
Private Sub TextBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown
        If e.KeyCode = Keys.Enter Then
            TextBox2.Focus()
        End If
    End Sub
Private Sub TextBox2_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox2.KeyDown
        If e.KeyCode = Keys.Enter Then
            TextBox3.Focus()
        End If
    End Sub



等等


And so on


Don 't。

这不是正常行为 - ENTER通常接受表单输入并移动到下一个表单,并且TAB在同一表单上的字段之间移动。问题在于,如果你破坏了这种机制,你的应用程序就会与用户习惯的所有其他应用程序不同 - 这会导致你的应用程序出现混乱(并且因为他们期望发生的事情而发誓)或者在另一个应用程序中(并咒骂你,因为你的应用程序中发生的事情只是将他们的工作放在另一个应用程序中)



这不是一个好主意 - 令人困惑用户很少有帮助。



你真的有这么好的理由要这么做吗?
Don't.
This is not "normal" behaviour - ENTER conventionally accepts the form input and moves to the next form, and TAB moves between fields on the same form. The problem is that if you subvert this mechanism, your app is different from all the other ones your user is used to - and that causes confusion, either in your app (and swearing at you because what they expect to happen didn't) or in the other app (and swearing at you because what happens in your app just mucked up their work in a different one)

It isn't a good idea - confusing users is rarely helpful.

Is there a really, really good reason why you want to do this?


你可以试试保持文本框的列表,并在输入命中时将焦点与下一个 TabIndex 重点关联。



这样的东西可能对你有用;



You could try to keep a list of Textboxes and focus the one with the next TabIndex when enter is hit.

Something like this might work for you;

Public Class Form1

    Private ReadOnly textBoxes As IList(Of TextBox) = New List(Of TextBox)

    Public Sub New()
        ' This call is required by the designer.
        InitializeComponent()

        textBoxes.Add(TextBox1)
        textBoxes.Add(TextBox2)
        textBoxes.Add(TextBox3)
    End Sub

    Private Sub TextBox1_PreviewKeyDown(sender As Object, e As PreviewKeyDownEventArgs) Handles TextBox1.PreviewKeyDown, TextBox2.PreviewKeyDown, TextBox3.PreviewKeyDown
        If e.KeyCode = Keys.Enter Then
            Dim source As TextBox = CType(sender, TextBox)

            Dim nextTextbox As TextBox = textBoxes.Where(Function(tb) tb.TabIndex > source.TabIndex).OrderBy(Function(tb) tb.TabIndex).FirstOrDefault()
            If nextTextbox Is Nothing Then nextTextbox = textBoxes.First()

            nextTextbox.Focus()
        End If
    End Sub
End Class





希望这有帮助,

Fredrik



Hope this helps,
Fredrik


这篇关于如何在VB.NET中使用enter键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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