首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > VB >

怎样使Text文本框控件具备"撒消"和"恢复"功能,跟Excel那样.解决办法

2012-01-08 
怎样使Text文本框控件具备撒消和恢复功能,跟Excel那样.如题所示,谢谢指教.[解决办法] 用API,不过下面

怎样使Text文本框控件具备"撒消"和"恢复"功能,跟Excel那样.
如题所示,谢谢指教.

[解决办法]
'用API,不过下面的代码只能做到单步撤消
'说明:
Dim fb 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 WM_CUT = &H300 '剪切
Const WM_COPY = &H301 '复制
Const WM_PAST = &H302 '粘贴
Const WM_CLEAR = &H303 '清除
Const WM_UNDO = &H304 '撤消

Private Sub Command1_Click()
fb = SendMessage(text1(index1).hWnd, WM_UNDO, 0, 0)
End Sub

[解决办法]
以下代码,在窗体中有三个控件,一个textbox控件,名为text1,两个按钮控件,名字分别为command1(caption为 "撤销 ")和command2(caption为 "恢复 ")

先看看能不能看懂,不懂再发帖子吧
'******************************

Dim N As Long '键盘输入的字符个数
Dim P As Long '记录撤销的个数
Dim Q As Long '记录恢复的个数
Dim ST(), HF() 'ST()是存储键盘输入的字符,HF()是存储撤销的字符
Dim M As Long '撤销和恢复的最大个数
'****************以上是通用里的变量定义


Private Sub Command1_Click()
P = P + 1
If P <= 5 And Text1.Text <> " " Then
ReDim Preserve HF(P)
HF(P) = Mid(Text1.Text, Len(Text1.Text), 1)
Text1.Text = Mid(Text1.Text, 1, Len(Text1.Text) - 1)
Q = UBound(HF)
End If

End Sub

Private Sub Command2_Click()
If Q > = 1 Then
Text1.Text = Text1.Text + HF(Q)
End If
Q = Q - 1
P = 0
End Sub

Private Sub Form_Load()
N = 0
M = 5
ReDim ST(M)
P = 0
Q = 0
End Sub

Private Sub Text1_KeyPress(KeyAscii As Integer)
Dim M As Long
Dim i As Long, j As Long
N = N + 1
If N <= M Then
ST(N) = Chr(KeyAscii)
Else
For i = 1 To M - 1
ST(i) = ST(i + 1)
Next i
ST(M) = Chr(KeyAscii)
N = M
End If

P = 0
ReDim HF(0)
Q = 0
End Sub

热点排行