-
Hi,
There are various command buttons on my form. When clicked, they load text into a textbox.
For example,
Command1_click()
Text1.text="Welcome to part one"
end sub
So there are a lot of buttons like this that can be clicked in any order.
I want a different button, a 'Back' button, that when clicked will make the text that was previously in the textbox, before they clicked the command button, appear.
Does anybody know how I could do this?
Thanks.
-
Store the Previous string in a variable.
For example
Code:
Private Sub Command1_Click()
TmpText = Text1
Text1 = "Hello!"
End Sub
Code:
Private Sub cmdBack_Click()
Text1 = TmpText
End Sub
-
Like this? All I get, when I click Commandback, is a blank box. I'll try more stuff, but if you have any code in the meantime....
Private Sub Command1_Click()
TmpText = Text1
Text1 = "Hello!"
End Sub
Private Sub Command2_Click()
TmpText = Text1
Text1 = "Bonjour!"
End Sub
Private Sub Command3_Click()
TmpText = Text1
Text1 = "Conas ata tu!"
End Sub
Private Sub Command4_Click()
TmpText = Text1
Text1 = "Como estas?"
End Sub
Private Sub Commandback_Click()
Text1.Text = TmpText
End Sub
-
WM_UNDO
How about using the SendMessage API + WM_UNDO Constant?
Assume you have a TextBox Control call Text1 and an Undo button call CmdUndo on your project.
Code:
Option Explicit
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
Private Const WM_UNDO = &H304
Private Sub CmdUndo_Click()
SendMessage Text1.hwnd, WM_UNDO, 0, 0
End Sub
-
In christophe's example, you need to put 'Dim TmpText as string' in the General section of your form code, otherwise it won't work.
-
I think the SendMessage API is engough for this problem and it save to declare one more variable call TmpText and write extra code to store the previous text on each time the TextBox text change. :)
-
Tag
You could always use the textbox.tag property to save the value and then you wouldn't need to bother with module scoped variables or an API call. You can also implement one proc. to reverse all form changes.
Try :
Private Sub Command1_Click()
Text1.Tag = Text1
Text1 = "Hello!"
End Sub
Private Sub cmdUndo_Click()
Dim cDummy as Control
For Each cDummy In Form1.Controls
If TypeOf(cDummy) Is Textbox Then
cDummy.Text = cDummy.Tag
End If
Next
End Sub
-
Chris, I tried your code, but it only seems to change textbox if textbox has the focus. (When a command button is clicked the button has the focus, so it doesn't alter the textbox).
Paul Warren, I tried your code and it worked somewhat (after I changed (cDummy) to cDummy.
But it only works once. If I have 5 command buttons and I click through them all (which changes text1 as the clicks occur), and then I click cmdUndo, it only returns it to the text of the last clicked command button.
Is it possible to have it so that, for example if CmdUndo is clicked 3 times, it will return the text of that command button etc...?
Thanks.
-
Why did my message
turn out like that?
It's messy. Change it quick.
-
Try this,,
in a form add 6 command buttons, named like this :
command1(0), command1(1), command1(2), command1(3), command1(4)
and command2 which is for "Back" and 1 textbox names text1
then pase the following code
Code:
Dim MyText(4), CurText
Private Sub Command1_Click(Index As Integer)
Text1.Text = MyText(Index)
CurText = Index
If Index = 0 Then
Command2.Enabled = False
Else
Command2.Enabled = True
End If
End Sub
Private Sub Command2_Click()
If CurText <> 0 Then
Text1.Text = MyText(CurText - 1)
CurText = CurText - 1
End If
If CurText = 0 Then Command2.Enabled = False
End Sub
Private Sub Form_Load()
MyText(0) = "Hello"
MyText(1) = "Welcome"
MyText(2) = "Bye"
MyText(3) = "See ya"
MyText(4) = "It works"
End Sub
let me know if it works or not
-
Yeh Mih,
I think it works,
Thanks. And now I'm going to analyse how it works.
-
hehehehehe
you don't need to analyze any thing,,,just read the code and you will see how easy and powerfull it is,,,,,
Any help i'm ready,,,you can e-mail me if you want
[email protected]