基础问题:如何实现按回车后光标自动移到下一个TXT框中去?

qblm_555 2004-12-14 11:50:50
我在一个窗体上放了很多个TXT框,不是一个控件哦(Name不同)。
已经设置好TabIndex属性,用TAB可以实现光标移动到下一个TXT框。
但我还想要实现按回车后自动移到下一个TXT框,有什么简单易用的办法?
总不能要我每个TXT框都加一个KeyPress方法判断吧?
...全文
249 10 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
fishmans 2004-12-15
  • 打赏
  • 举报
回复
已经排好tabindex的话可以在每个txt框中的keypress事件中写上if keyascii=13 then sendkeys "{tab}",没有排好就用目的txt框.setfocus
qblm_555 2004-12-15
  • 打赏
  • 举报
回复
我说过了:已经设置好TabIndex属性,用TAB可以实现光标移动到下一个TXT框
但按回车一点反应都没有啊。
cindytsai 2004-12-15
  • 打赏
  • 举报
回复
又是一个厉害的高手,先学习了再说。
of123 2004-12-15
  • 打赏
  • 举报
回复
不要使用 Sendkeys 将回车转换为 Tab。因为这样会使未打 SP2 补丁的 Window 2000 键盘锁死。这是 Win2k 的一个著名 Bug。

用下列API函数替代 Sendkeys:(设置好 TabIndex, Form 的属性 KeyPreview = True)
1
Private Declare Sub keybd_event Lib "user32" (ByVal bVk As Byte,ByVal _
bScan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As Long)

Const KEYEVENTF_KEYUP = &H2
Const VK_TAB = &H9

Private Sub Form_KeyKeyPress(KeyAscii As Integer)
If KeyAscii = vbKeyReturn Then
keybd_event VK_TAB, 0, 0, 0
keybd_event VK_TAB, 0, KEYEVENTF_KEYUP, 0
End If
End Sub


2
Private Declare Function PostMessage Lib "user32" Alias "PostMessageA" _
(ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, _
ByVal lParam As Long) As Long

Const WM_KEYDOWN As Long = &H100
Const VK_TAB As Long = &H9
Dim retVal as long

Private Sub Form_KeyPress(KeyAscii As Integer)
If KeyAscii = vbKeyReturn Then retVal = PostMessage(Me.hwnd, WM_KEYDOWN, VK_TAB, 0)
End Sub

微软有关此问题说明的原文:
FIX: SendKeys Function Locks Keyboard on Windows 2000

The information in this article applies to:
Microsoft Visual Basic Professional Edition for Windows 5.0, when used with:
the operating system: Microsoft Windows 2000 SP1
Microsoft Visual Basic Professional Edition for Windows 6.0, when used with:
the operating system: Microsoft Windows 2000 SP1
Microsoft Visual Basic Enterprise Edition for Windows 5.0, when used with:
the operating system: Microsoft Windows 2000 SP1
Microsoft Visual Basic Enterprise Edition for Windows 6.0, when used with:
the operating system: Microsoft Windows 2000 SP1

This article was previously published under Q276346


SYMPTOMS
When a Visual Basic program is running, the keyboard on your Windows 2000-based computer freezes.

This problem can also occur if the application is hosted on a Terminal Server, and if the client is running Windows 2000. The following system event is logged on the Windows 2000-based computer:

Event Type:Error
Event Source:i8042prt
Event Category:None
Event ID:27

Description:
The operation on timed out (time out is configurable via the registry).


CAUSE
This problem is the result of a bug in the Windows 2000 keyboard driver, and only occurs in conjunction with the Visual Basic SendKeys function.


RESOLUTION
To resolve this problem on a Windows 2000-based computer that is not running Terminal Services, install Windows 2000 Service Pack 2 (SP2) from the following Microsoft Web site:

http://www.microsoft.com/windows2000/downloads/servicepacks/sp2/default.asp

A supported fix is now available from Microsoft, but it is only intended to correct the problem that is described in this article. Apply it only to computers that are experiencing this specific problem. This fix may receive additional testing. Therefore, if you are not severely affected by this problem, Microsoft recommends that you wait for the next Windows 2000 service pack that contains this fix.

To resolve this problem immediately, contact Microsoft Product Support Services to obtain the fix. For a complete list of Microsoft Product Support Services phone numbers and information about support costs, visit the following Microsoft Web site:

http://support.microsoft.com/default.aspx?scid=fh;EN-US;CNTACTMS

NOTE: In special cases, charges that are ordinarily incurred for support calls may be canceled if a Microsoft Support Professional determines that a specific update will resolve your problem. The typical support costs will apply to additional support questions and issues that do not qualify for the specific update in question.

The English version of this fix should have the following file attributes or later:
Date Time Version Size File name
-----------------------------------------------------------
05/18/2000 07:17p 5.0.2195.2096 48,592 I8042prt.sys


To unlock the keyboard manually, on the Start menu, point to Settings, and then click Control Panel. Click Keyboard, and then reset the Keyboard Refresh Rate.


WORKAROUND
There are also two programmatic workarounds to this problem:
Workaround 1
Replace the SendKeys function with the keybd_event API function as follows:
Private Declare Sub keybd_event Lib "user32" (ByVal bVk As Byte,ByVal _
bScan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As Long)

Const KEYEVENTF_KEYUP = &H2
Const VK_TAB = &H9

keybd_event VK_TAB, 0, 0, 0
keybd_event VK_TAB, 0, KEYEVENTF_KEYUP, 0

Workaround 2
Replace the Sendkeys function with the PostMessage API function as follows:
Private Declare Function PostMessage Lib "user32" Alias "PostMessageA" _
(ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, _
ByVal lParam As Long) As Long

Const WM_KEYDOWN As Long = &H100
Const VK_TAB As Long = &H9
Dim retVal as long

retVal = PostMessage(Me.hwnd, WM_KEYDOWN, VK_TAB, 0)


STATUS
Microsoft has confirmed that this is a bug in the Microsoft products that are listed at the beginning of this article.


MORE INFORMATION
Steps to Reproduce Behavior
1. On a computer that is running Windows 2000, create a new Standard EXE project in Visual Basic. Form1 is created by default.
2. Set the Keypreview property of Form1 to True.
3. Add six TextBoxes to Form1.
4. Add the following code to the General Declarations section of Form1:
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = 13 Then
SendKeys "{tab}"
End If
End Sub

5. Run the project.
6. Press and hold down the Enter key on the numeric keypad, and note that the keyboard locks in approximately five seconds.

For additional information, click the article number below to view the article in the Microsoft Knowledge Base:

262798 PS/2 Keyboard/Mouse Not Recognized When Plugged Into Running Computer


Last Reviewed: 10/16/2002
Keywords: kbBug kbDSupport kbprb kbQFE KB276346

TaoHuang 2004-12-15
  • 打赏
  • 举报
回复
Private Sub Form_KeyPress(KeyAscii As Integer)
On Error Resume Next
If KeyAscii = vbKeyReturn Then
SendKeys "{tab}"
KeyAscii = 0
End If
End Sub
要將Form的KeyPreview屬性設為True,並設好Control的TabIndex順序。這樣在按回車時會自動到下一個控件。
sirious 2004-12-15
  • 打赏
  • 举报
回复
或者
keydown
if index< M and keycode=vbkeyreturn then text1(index+1).setfocus
sirious 2004-12-15
  • 打赏
  • 举报
回复

keydown
if index=0 and keycode=vbkeyreturn then
text(1).setfocus
end if
if index=1 and keycode=vbreturn then
text1(2).setfocus
end if
..
.
.
litaoa 2004-12-15
  • 打赏
  • 举报
回复
嗯。就是很麻烦滴,并且我用那个SETFOCUS的时候有时候会出错,不知道是什么问题,你可以试试SENDKEYS。。不知道好不好用。
wangwl 2004-12-15
  • 打赏
  • 举报
回复
要每个都加的。除非用控件数组。
lhy0922 2004-12-14
  • 打赏
  • 举报
回复
text控件里面不是有一个tabindex属性吗,你只要在那里面设置1,2,3去排序不就得了

7,785

社区成员

发帖
与我相关
我的任务
社区描述
VB 基础类
社区管理员
  • VB基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧