|
-
Apr 22nd, 2008, 09:33 AM
#1
Thread Starter
Fanatic Member
[2008] SendKeys problem
I have this code so far.
vb Code:
Private Sub btnUndo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUndo.Click
SendKeys.Send("^Z")
End Sub
I wanted the Undo button to be able to Undo the changes anywhere on the form (In any of the text boxes, quite a few!)
But this does not work. Any ideas?
-
Apr 22nd, 2008, 10:16 AM
#2
Re: [2008] SendKeys problem
That's becuase you're not sending the key to the right destination. When you click a button, the button is the control that has focus, and your sendkey sends the keys to it. What you need to do is looping thru all the controls on the form and call Undo method if the control is a textbox.
Code:
Private Sub btnUndo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUndo.Click
For Each ctrl As Control In Me.Controls
If TypeOf ctrl Is TextBox Then
DirectCast(ctrl, TextBox).Undo()
End If
Next
End Sub
-
Apr 22nd, 2008, 10:33 AM
#3
Thread Starter
Fanatic Member
Re: [2008] SendKeys problem
Ahh right I see, well may I ask what happens if there is a richtextbox aswell?
Just create the For loop twice? Once for textbox and a second for richtextbox?
-
Apr 22nd, 2008, 10:54 AM
#4
Re: [2008] SendKeys problem
No, you can just add a ElseIf block to the existing code
Code:
If TypeOf ctrl Is TextBox Then
DirectCast(ctrl, TextBox).Undo()
ElseIf TypeOf ctrl Is RichTextBox Then
DirectCast(ctrl, RichTextBox).Undo()
End If
-
Apr 23rd, 2008, 03:33 AM
#5
Thread Starter
Fanatic Member
Re: [2008] SendKeys problem
Hey, thanks for the help Stanav, I have taken into account what you have said, And I have tried this code. (I had to look up DirectCast as well, Oops).
But this still does not allow me to do what I want.
-
Apr 23rd, 2008, 05:09 AM
#6
Re: [2008] SendKeys problem
What is the expected result and what is the actual result when you run that code?
-
Apr 23rd, 2008, 05:28 AM
#7
Thread Starter
Fanatic Member
Re: [2008] SendKeys problem
Sorry should have stated.
The expected result is if I type "Hello" into a text box and press the Undo button, then the "Hello" disapeares.
Just as .Undo does.
-
Apr 23rd, 2008, 07:00 AM
#8
Re: [2008] SendKeys problem
 Originally Posted by Lerroy_Jenkins
Sorry should have stated.
The expected result is if I type "Hello" into a text box and press the Undo button, then the "Hello" disapeares.
Just as .Undo does.
No, the undo method of a textbox or richtextbox will undo the last changes on the text. That is, if the textbox starts out blank, you then types "Hello" to it, when you call undo, the textbox will change back to blank. Now if you call undo again, it will call undo once more, it'll put "Hello" back. Now that you have "Hello" in the textbox, if you type in " World" and then call undo, it'll remove " World" and leave the textbox with only "Hello".
In your case, if you want the textbox to be blank, you need to either set its text to string.empty or just call the Clear method. Don't call undo.
-
Apr 23rd, 2008, 07:07 AM
#9
Thread Starter
Fanatic Member
Re: [2008] SendKeys problem
If the person types Hello my name is Lerroy
and then they press Undo, just as Ctrl Z, I want Lerroy to disappear.
-
Apr 23rd, 2008, 07:26 AM
#10
Re: [2008] SendKeys problem
 Originally Posted by Lerroy_Jenkins
If the person types Hello my name is Lerroy
and then they press Undo, just as Ctrl Z, I want Lerroy to disappear.
That's not what Undo is... Undo, like the name suggests, will undo the last change. So if your textbox starts out blank and you type in "Hello my name is Lerroy", when you call undo, the textbox will be back to blank because it senses the whole sentence as the last change. If you want to remove just the last word from the text, you have to code it yourself using string.lastindexof to find the position of the last space then use string.substring to get the substring without the last word, you then set that substring to your textbox.text.
-
Apr 23rd, 2008, 07:28 AM
#11
Thread Starter
Fanatic Member
Re: [2008] SendKeys problem
This works fine for a RTB
vb Code:
If RichTextBox1.CanUndo = True Then RichTextBox1.Undo() End If
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|