VB 文本框的光标位置
我建了两个文本框,都是多行的,文本框1不带滚动条,文本框2带垂直滚动条,我想实现的目的是,当文本框1中不能完全显示时,就在文本框2中编辑;当文本框2中能完全显示时,就在文...
我建了两个文本框,都是多行的,文本框1不带滚动条,文本框2带垂直滚动条,我想实现的目的是,当文本框1中不能完全显示时,就在文本框2中编辑;当文本框2中能完全显示时,就在文本框1中编辑。还希望文本框切换后光标仍在原编辑位置。我的代码如下:
Option Explicit
Dim wenbenkuangyizhongguangbiaoweizhi As Long
Dim wenbenkuangerzhongguangbiaoweizhi As Long
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lparam As Any) As Long
Const EM_GETLINECOUNT = &HBA
Private Sub wenbenkuangyibian()
Dim w As Long
w = SendMessage(Text1.hwnd, EM_GETLINECOUNT, 0, 0)
Me.Caption = w
Me.FontSize = Text1.FontSize
If w > (Text1.Height - 60) \ TextHeight("A") Then
Text2.Text = Text1.Text
wenbenkuangyizhongguangbiaoweizhi = Text1.SelStart
Text2.Visible = True
Text1.Visible = False
If Text2.Visible = True Then
Text2.SetFocus
End If
End If
End Sub
Private Sub wenbenkuangerbian()
Dim w As Long
w = SendMessage(Text2.hwnd, EM_GETLINECOUNT, 0, 0)
Me.Caption = w
Me.FontSize = Text2.FontSize
If w > (Text2.Height - 60) \ TextHeight("A") Then
Else
Text1.Text = Text2.Text
wenbenkuangerzhongguangbiaoweizhi = Text2.SelStart
Text1.Visible = True
Text2.Visible = False
If Text1.Visible = True Then
Text1.SetFocus
End If
End If
End Sub
Private Sub Form_Load()
Text1.Visible = True
Text2.Visible = False
Text1.Text = ""
Text2.Text = ""
End Sub
Private Sub Text1_Change()
wenbenkuangyibian
End Sub
Private Sub Text1_GotFocus()
Text1.SelStart = wenbenkuangerzhongguangbiaoweizhi
Text1.SelLength = 0
End Sub
Private Sub Text2_Change()
wenbenkuangerbian
End Sub
Private Sub Text2_GotFocus()
Text2.SelStart = wenbenkuangyizhongguangbiaoweizhi
Text2.SelLength = 0
End Sub
我发现的问题是,当在文本框1中编辑时,输入的内容刚好超出文本框1显示的范围或稍多两三个字符时,能够实现上述的目的,如果此时输入的内容比较长,则切换到文本框2后,光标的位置却不在原编辑位置,而是定在了首位置。请高手朋友给指点一下,我的代码哪儿出现了错误,如何修改才能实现我的目的? 展开
Option Explicit
Dim wenbenkuangyizhongguangbiaoweizhi As Long
Dim wenbenkuangerzhongguangbiaoweizhi As Long
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lparam As Any) As Long
Const EM_GETLINECOUNT = &HBA
Private Sub wenbenkuangyibian()
Dim w As Long
w = SendMessage(Text1.hwnd, EM_GETLINECOUNT, 0, 0)
Me.Caption = w
Me.FontSize = Text1.FontSize
If w > (Text1.Height - 60) \ TextHeight("A") Then
Text2.Text = Text1.Text
wenbenkuangyizhongguangbiaoweizhi = Text1.SelStart
Text2.Visible = True
Text1.Visible = False
If Text2.Visible = True Then
Text2.SetFocus
End If
End If
End Sub
Private Sub wenbenkuangerbian()
Dim w As Long
w = SendMessage(Text2.hwnd, EM_GETLINECOUNT, 0, 0)
Me.Caption = w
Me.FontSize = Text2.FontSize
If w > (Text2.Height - 60) \ TextHeight("A") Then
Else
Text1.Text = Text2.Text
wenbenkuangerzhongguangbiaoweizhi = Text2.SelStart
Text1.Visible = True
Text2.Visible = False
If Text1.Visible = True Then
Text1.SetFocus
End If
End If
End Sub
Private Sub Form_Load()
Text1.Visible = True
Text2.Visible = False
Text1.Text = ""
Text2.Text = ""
End Sub
Private Sub Text1_Change()
wenbenkuangyibian
End Sub
Private Sub Text1_GotFocus()
Text1.SelStart = wenbenkuangerzhongguangbiaoweizhi
Text1.SelLength = 0
End Sub
Private Sub Text2_Change()
wenbenkuangerbian
End Sub
Private Sub Text2_GotFocus()
Text2.SelStart = wenbenkuangyizhongguangbiaoweizhi
Text2.SelLength = 0
End Sub
我发现的问题是,当在文本框1中编辑时,输入的内容刚好超出文本框1显示的范围或稍多两三个字符时,能够实现上述的目的,如果此时输入的内容比较长,则切换到文本框2后,光标的位置却不在原编辑位置,而是定在了首位置。请高手朋友给指点一下,我的代码哪儿出现了错误,如何修改才能实现我的目的? 展开
若以下回答无法解决问题,邀请你更新回答
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询