Results 1 to 11 of 11

Thread: [2008] SendKeys problem

  1. #1

    Thread Starter
    Fanatic Member Lerroy_Jenkins's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    661

    [2008] SendKeys problem

    I have this code so far.

    vb Code:
    1. Private Sub btnUndo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUndo.Click
    2.         SendKeys.Send("^Z")
    3.     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?
    Lerroy

    "η β π", or "Eta Beta Pi" (Eat A Better Pie)

    01001000
    01000101
    01001100
    01010000


    My Own Code - WordCounter

    Useful Forum Links -Reputation - What is it?

  2. #2
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    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

  3. #3

    Thread Starter
    Fanatic Member Lerroy_Jenkins's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    661

    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?
    Lerroy

    "η β π", or "Eta Beta Pi" (Eat A Better Pie)

    01001000
    01000101
    01001100
    01010000


    My Own Code - WordCounter

    Useful Forum Links -Reputation - What is it?

  4. #4
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    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

  5. #5

    Thread Starter
    Fanatic Member Lerroy_Jenkins's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    661

    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.
    Lerroy

    "η β π", or "Eta Beta Pi" (Eat A Better Pie)

    01001000
    01000101
    01001100
    01010000


    My Own Code - WordCounter

    Useful Forum Links -Reputation - What is it?

  6. #6
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: [2008] SendKeys problem

    What is the expected result and what is the actual result when you run that code?

  7. #7

    Thread Starter
    Fanatic Member Lerroy_Jenkins's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    661

    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.
    Lerroy

    "η β π", or "Eta Beta Pi" (Eat A Better Pie)

    01001000
    01000101
    01001100
    01010000


    My Own Code - WordCounter

    Useful Forum Links -Reputation - What is it?

  8. #8
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: [2008] SendKeys problem

    Quote 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.

  9. #9

    Thread Starter
    Fanatic Member Lerroy_Jenkins's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    661

    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.
    Lerroy

    "η β π", or "Eta Beta Pi" (Eat A Better Pie)

    01001000
    01000101
    01001100
    01010000


    My Own Code - WordCounter

    Useful Forum Links -Reputation - What is it?

  10. #10
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: [2008] SendKeys problem

    Quote 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.

  11. #11

    Thread Starter
    Fanatic Member Lerroy_Jenkins's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    661

    Re: [2008] SendKeys problem

    This works fine for a RTB

    vb Code:
    1. If RichTextBox1.CanUndo = True Then
    2.             RichTextBox1.Undo()
    3.         End If
    Lerroy

    "η β π", or "Eta Beta Pi" (Eat A Better Pie)

    01001000
    01000101
    01001100
    01010000


    My Own Code - WordCounter

    Useful Forum Links -Reputation - What is it?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width