Results 1 to 5 of 5

Thread: Text Box undo

  1. #1

    Thread Starter
    New Member
    Join Date
    Oct 2007
    Posts
    3

    Question Text Box undo

    How do I get undo to work with a textbox. The undo in the right click menu doesn't work.

    I'm new to VB so could you explain it to me as well without trying to loose me.

    Thanks in advance.

    EDIT: Sorry load of rubbish could a mod delete this please.
    Last edited by wickedgenius; Oct 18th, 2007 at 02:25 PM. Reason: Fixed problem

  2. #2
    Lively Member RickyH's Avatar
    Join Date
    Oct 2007
    Posts
    92

    Re: Text Box undo

    Quote Originally Posted by wickedgenius
    How do I get undo to work with a textbox. The undo in the right click menu doesn't work.

    I'm new to VB so could you explain it to me as well without trying to loose me.

    Thanks in advance.

    EDIT: Sorry load of rubbish could a mod delete this please.
    sounds like you may have added the standard buttons on a context menu. what you need to do is while in designing the form, click on the context menu below the form and it should appear. double-click on the Undo and you should go to code view. You will notice it is empty or some sort of comment in there. Now, do soemthing like this:

    vb Code:
    1. Public Class Form1
    2. #Region "   declarations   "
    3.     Dim strUndoData As String = ""
    4.     Dim strRedoData As String = ""
    5. #End Region
    6.  
    7.     Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
    8.         If Not strUndoData <> "" Then
    9.             strUndoData = TextBox1.Text
    10.         End If
    11.     End Sub
    12.  
    13.     Private Sub ctxMenu_UndoMePlease_OhYesPlease_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ctxMenu_UndoMePlease_OhYesPlease.Click
    14.         strRedoData = TextBox1.Text
    15.         TextBox1.Text = strUndoData
    16.         strUndoData = ""
    17.         ctxMenu_UndoMePlease_OhYesPlease.Enabled = False
    18.         ctxMenu_RedoMePlease_OhGodNotAgain.Enabled = True
    19.     End Sub
    20.  
    21.     Private Sub ctxMenu_RedoMePlease_OhGodNotAgain_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ctxMenu_RedoMePlease_OhGodNotAgain.Click
    22.         TextBox1.Text = strRedoData
    23.         strUndoData = strRedoData
    24.         strRedoData = ""
    25.         ctxMenu_RedoMePlease_OhGodNotAgain.Enabled = False
    26.         ctxMenu_UndoMePlease_OhYesPlease.Enabled = True
    27.     End Sub
    28. End Class
    Last edited by RickyH; Oct 18th, 2007 at 02:55 PM.

  3. #3
    Lively Member RickyH's Avatar
    Join Date
    Oct 2007
    Posts
    92

    Re: Text Box undo

    Quote Originally Posted by wickedgenius
    I'm new to VB so could you explain it to me as well without trying to loose me.

    Basically, you need someplace to store the undo data. The above is just a simple setup where you can only undo one level. You can get more complicated and provide multiple levels of undo, but this should suffice for a simple example.

    As you can see, you need to store the data you are changing back to, much like having another textbox on the screen that when you change something, it will be stored there until time comes that whenyou decide to change back, you grab the data from the hidden textbox and put it in the visible text box. in this case, instead of using a hidden textbox, we are using a hidden string variable named strUndoData & strRedoData.

  4. #4
    Junior Member
    Join Date
    Apr 2007
    Posts
    22

    Re: Text Box undo

    Dosen't a textbox have a Undo Property, I could be wrong though...

  5. #5
    Junior Member Snizbatch's Avatar
    Join Date
    Sep 2002
    Location
    Jacksonville, FL
    Posts
    26

    Re: Text Box undo

    As long as there is text in the textbox I am able to right click and select undo. If done multiple times, it will flip-flop between values. There is a note in help stating "The Undo method does not work with the KeyPress or TextChanged events." You may want to go here and read about the process: http://msdn2.microsoft.com/en-us/lib...base.undo.aspx


    Jim

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