Undoing and Redoing Actions in an Edit Control in Desktop Windows Applications

Applies to TestComplete 15.47, last modified on January 20, 2023

Note: To learn how to simulate user actions over text edit box controls in web applications, see Working With Text Edit Controls in Web Applications.

While testing edit controls, you can use specific properties and methods of the corresponding program object to perform certain actions and obtain data stored in controls. You can call these methods and properties from your keyword tests, as well as from scripts. This topic describes how to work with the needed properties and methods from your scripts. However, when testing a control from your keyword test, you can use the same methods and properties calling them from keyword test operations. For more information, see Keyword Tests Basic Operations.

Testing an edit control, you may need to cancel the most recent operation, for example, if this operation was incorrect, and on the contrary you need to cancel the last command. In this case you should use the Undo and Redo commands. There are two approaches, which allow you to perform these commands:

  • The first one consists of simulating the Ctrl+Z or Alt+Backspace key combinations. These combinations are the shortcut keys of the mentioned commands. Ctrl+Z performs the Undo command and repeating the shortcut simulation performs the Redo command. You can simulate keystrokes via the Keys action. The following example demonstrates how you can cancel the last operation using the shortcut:

    JavaScript, JScript

    function Main()
    {
      var p, Edit;
      // Run Notepad
      WshShell.Run("notepad.exe", SW_SHOWNORMAL);
      // Obtain the edit object
      p = Sys.Process("NOTEPAD");
      Edit = p.Window("Notepad").Window("Edit");

      // Type new text
      Edit.Keys("Come back!!");

      // Perform Undo command
      Edit.Keys("^z");
      Edit.Keys("Come over here!!");

      // Perform Redo command
      Edit.Keys("^z");
      Edit.Keys("[Right] I have something important to say!!");
    }

    Python

    def Main():
      # Run Notepad 
      WshShell.Run("notepad.exe", SW_SHOWNORMAL)
      # Obtain the edit object
      p = Sys.Process("NOTEPAD")
      Edit = p.Window("Notepad").Window("Edit")
    
      # Type new text
      Edit.Keys("Come back!!")
    
      # Perform Undo command
      Edit.Keys("^z")
      Edit.Keys("Come over here!!")
    
      # Perform Redo command
      Edit.Keys("^z")
      Edit.Keys("[Right] I have something important to say!!")

    VBScript

    Sub Main
      Dim p, Edit
      ' Run Notepad
      Call WshShell.Run("notepad.exe", SW_SHOWNORMAL)
      ' Obtain the edit object
      Set p = Sys.Process("NOTEPAD")
      Set Edit = p.Window("Notepad").Window("Edit")

      ' Type new text
      Edit.Keys "Come back!!"
       
      ' Perform Undo command
      Edit.Keys "^z"
      Edit.Keys "Come over here!!"

      ' Perform Redo command
      Edit.Keys "^z"
      Edit.Keys "[Right] I have something important to say!!"
    End Sub

    DelphiScript

    procedure Main;
      var p, Edit: OleVariant;
    begin
      // Run Notepad
      WshShell.Run('notepad.exe', SW_SHOWNORMAL);
      // Obtain the edit object
      p := Sys.Process('NOTEPAD');
      Edit := p.Window('Notepad').Window('Edit');

      // Type new text
      Edit.Keys('Come back!!');

      // Perform Undo command
      Edit.Keys('^z');
      Edit.Keys('Come over here!!');

      // Perform Redo command
      Edit.Keys('^z');
      Edit.Keys('[Right] I have something important to say!!');
    end;

    C++Script, C#Script

    function Main()
    {
      var p, Edit;
      // Run Notepad
      WshShell["Run"]("notepad.exe", SW_SHOWNORMAL);

      // Obtain the edit object and fill it with text
      p = Sys["Process"]("NOTEPAD");
      Edit = p["Window"]("Notepad")["Window"]("Edit");

      // Type new text
      Edit["Keys"]("Come back!!");

      // Perform Undo command
      Edit["Keys"]("^z");
      Edit["Keys"]("Come over here!!");

      // Perform Redo command
      Edit["Keys"]("^z");
      Edit["Keys"]("[Right] I have something important to say!!");
    }

  • Using edit control’s context menu. This menu can be called by using the ClickR action. Use the Window.PopupMenu.Click method to select the desired command (Undo), or simulate the U key, which is the hot key for this command. An example below demonstrates how to perform the mentioned operations using the context menu:

    JavaScript, JScript

    function Main()
    {
      var p, Edit;
      // Run Notepad
      WshShell.Run("notepad.exe", SW_SHOWNORMAL);

      // Obtain the edit object
      p = Sys.Process("NOTEPAD");
      Edit = p.Window("Notepad").Window("Edit");

      // Type new text
      Edit.Keys ("Do cats eat bats?");

      // Perform the Undo command via the context menu
      Edit.ClickR();
      Edit.Keys ("u");
      Edit.Keys ("Do bats eat cats?");

      // Perform the Redo command via the context menu
      Edit.ClickR();
      Edit.PopupMenu.Click ("Undo");
    }

    Python

    def Main():
      # Run Notepad 
      WshShell.Run("notepad.exe", SW_SHOWNORMAL)
    
      # Obtain the edit object
      p = Sys.Process("NOTEPAD")
      Edit = p.Window("Notepad").Window("Edit")
    
      # Type new text
      Edit.Keys ("Do cats eat bats?")
    
      # Perform the Undo command via the context menu
      Edit.ClickR()
      Edit.Keys ("u")
      Edit.Keys ("Do bats eat cats?")
    
      # Perform the Redo command via the context menu 
      Edit.ClickR()
      Edit.PopupMenu.Click ("Undo")

    VBScript

    Sub Main
      Dim p, Edit
      ' Run Notepad
      Call WshShell.Run("notepad.exe", SW_SHOWNORMAL)

      ' Obtain the edit object
      Set p = Sys.Process("NOTEPAD")
      Set Edit = p.Window("Notepad").Window("Edit")

      ' Type new text
      Edit.Keys "Do cats eat bats?"

      ' Perform the Undo command via the context menu
      Edit.ClickR
      Edit.Keys "u"
      Edit.Keys "Do bats eat cats?"

      ' Perform the Redo command via the context menu
      Edit.ClickR
      Edit.PopupMenu.Click "Undo"
    End Sub

    DelphiScript

    procedure Main;
      var p, Edit: OleVariant;
    begin
      // Run Notepad
      WshShell.Run('notepad.exe', SW_SHOWNORMAL);
     
      // Obtain the edit object
      p := Sys.Process('NOTEPAD');
      Edit := p.Window('Notepad').Window('Edit');

      // Type new text
      Edit.Keys ('Do cats eat bats?');

      // Perform the Undo command via the context menu
      Edit.ClickR;
      Edit.Keys ('u');
      Edit.Keys ('Do bats eat cats?');

      // Perform the Redo command via the context menu
      Edit.ClickR;
      Edit.PopupMenu.Click ('Undo');
    end;

    C++Script, C#Script

    function Main()
    {
      var p, Edit;
      // Run Notepad
      WshShell["Run"]("notepad.exe", SW_SHOWNORMAL);
      
      // Obtain the edit object
      p = Sys["Process"]("NOTEPAD");
      Edit = p["Window"]("Notepad")["Window"]("Edit");

      // Type new text
      Edit["Keys"]("Do cats eat bats?");

      // Perform the Undo command via the context menu
      Edit["ClickR"]();
      Edit["Keys"]("u");
      Edit["Keys"]("Do bats eat cats?");

      // Perform the Redo command via the context menu
      Edit["ClickR"]();
      Edit["PopupMenu"]["Click"]("Undo");
    }

See Also

Working With Edit Controls in Desktop Windows Applications
Simulating Keystrokes
Simulating Menu Actions
Working With Text Edit Controls in Web Applications

Highlight search results