请问大家?

jswgn201 2008-04-10 12:52:49
如果一个窗体上有40个文本框,每个文本框的内容一样,如text1.text的代码如下:
Private Sub Text1_KeyPress(KeyAscii As Integer)
If KeyAscii = 13 Then
Text2.SetFocus
End If
End Sub
而text2.text的代码为:
Private Sub Text2_KeyPress(KeyAscii As Integer)
If KeyAscii = 13 Then
Text3.SetFocus
End If
End Sub
依次类推,如果要写完40个文本框,有没有什么办法使代码简化呀,只要写几行就可以实现以上的功能了,代码怎么写呢?
...全文
143 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
knowledge_Is_Life 2008-05-01
  • 打赏
  • 举报
回复
没遇到过这种情况.
jswgn201 2008-04-11
  • 打赏
  • 举报
回复
谢谢大家的回答,我又学到了不少东西
of123 2008-04-10
  • 打赏
  • 举报
回复
更正:

Private Sub Text1_KeyPress(Index as Integre, KeyAscii As Integer)
If (KeyAscii = 13) And (Index < Text1.Count - 1) Then
Text1(Index + 1).SetFocus
End If
End Sub
of123 2008-04-10
  • 打赏
  • 举报
回复
用控件数组:
Private Sub Text1_KeyPress(Index as Integre, KeyAscii As Integer)
If (KeyAscii = 13) And (Index < Text1.Count) Then
Text1(Index + 1).SetFocus
End If
End Sub
用户 昵称 2008-04-10
  • 打赏
  • 举报
回复
Option Explicit

'放一个text1,其index属性设置为0
Private Sub Form_Load()
Dim i As Long
For i = 1 To 20
Load Text1(i)
Next i

For i = 0 To 20
Text1(i).Left = 0
Text1(i).Top = i * 500
Text1(i).Visible = True
Next i
End Sub

Private Sub Text1_KeyDown(Index As Integer, KeyCode As Integer, Shift As Integer)
On Error Resume Next
Text1(Index + 1).SetFocus
End Sub
用户 昵称 2008-04-10
  • 打赏
  • 举报
回复
做成text的数组

7,763

社区成员

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

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