Entering Text into 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.

Working with an edit control often implies modifying text within it. The text displayed within the edit control can be changed in different ways:

The following sample code demonstrates how to enter new text in an edit control:

JavaScript, JScript

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

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

  // Enter text into the edit control
  Edit.SetText("I should like to have it explained");
}

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")

  # Enter text into the edit control
  Edit.SetText("I should like to have it explained")

VBScript

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

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

  ' Enter text into the edit control
  Edit.SetText "I should like to have it explained"
End Sub

DelphiScript

procedure Main;
  var p, Edit;
begin
  // Run Notepad
  WshShell.Run('notepad.exe', SW_SHOWNORMAL);

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

  // Enter text into the edit control
  Edit.SetText('I should like to have it explained');
end;

C++Script, C#Script

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

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

  // Enter text into the edit control
  Edit["SetText"]("I should like to have it explained");
}

If you need to delete text displayed in the edit control, you can do any of the following:

  • Simulate Delete or Backspace keystrokes via the Keys action. Here is an example:

    JavaScript, JScript

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

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

      // Delete text using keystrokes
      Edit.Keys("[BS][BS]");
    }

    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")
      Edit.Keys("White Rabbittt")
    
      # Delete text using keystrokes
      Edit.Keys("[BS][BS]")

    VBScript

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

      ' Obtain the edit object and fill it with text
      Set p = Sys.Process("NOTEPAD")
      Set Edit = p.Window("Notepad").Window("Edit")
      Edit.Keys "White Rabbittt"

      ' Delete text using keystrokes
      Edit.Keys "[BS][BS]"
    End Sub

    DelphiScript

    procedure Main;
      var p, Edit;
    begin
      // Run Notepad
      WshShell.Run('notepad.exe', SW_SHOWNORMAL);

      // Obtain the edit object
      p := Sys.Process('NOTEPAD');
      Edit := p.Window('Notepad').Window('Edit');
      Edit.Keys('White Rabbittt');

      // Delete text using keystrokes
      Edit.Keys('[BS][BS]');
    end;

    C++Script, C#Script

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

      // Obtain the edit object
      var p = Sys["Process"]("NOTEPAD");
      var Edit = p["Window"]("Notepad")["Window"]("Edit");
      Edit["Keys"]("White Rabbittt");

      // Delete text using keystrokes
      Edit["Keys"]("[BS][BS]");
    }

  • Use the Cut or Delete items of the edit control’s context menu. In this case, you should do the following:

    • Select the text to be deleted (for more information on how to do this, see Selecting Text Within an Edit Control in Desktop Windows Applications).

    • Call the context menu by using the ClickR action or by simulating the Application keystroke.

    • Select the Cut or Delete command from the menu. You can perform this operation by using the Window.PopupMenu.Click method or by simulating a T or D keystroke - these are hot keys of these commands.

      The following code demonstrates how to delete text using the context menu:

      JavaScript, JScript

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

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

        // Select a portion of the text
        Edit.Keys("![Left]![Left]![Left]");

        // Delete the selected text using the context menu
        Edit.ClickR();
        Edit.PopupMenu.Click("Delete");

        // Select a portion of the text
        Edit.Keys("^a");

        // Cut the selected text using the context menu
        Edit.ClickR();
        Edit.PopupMenu.Click("Cut");

        // Post the clipboard content to the log
        Log.Message(Sys.Clipboard);
      }

      Python

      def PopupMenuExample():
        # Run Notepad 
        WshShell.Run("notepad.exe", SW_SHOWNORMAL)
      
        # Obtain the edit object
        p = Sys.Process("NOTEPAD")
        Edit = p.Window("Notepad").Window("Edit")
        Edit.Keys("White Rabbitttt")
      
        # Select a portion of the text
        Edit.Keys("![Left]![Left]![Left]")
      
        # Delete the selected text using the context menu
        Edit.ClickR()
        Edit.PopupMenu.Click("Delete")
      
        # Select a portion of the text
        Edit.Keys("^a")
      
        # Cut the selected text using the context menu
        Edit.ClickR()
        Edit.PopupMenu.Click("Cut")
      
        # Post the clipboard content to the log
        Log.Message(Sys.Clipboard)

      VBScript

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

        ' Obtain the edit object and fill it with text
        Set p = Sys.Process("NOTEPAD")
        Set Edit = p.Window("Notepad").Window("Edit")
        Edit.Keys "White Rabbitttt"

        ' Select a portion of the text
        Edit.Keys "![Left]![Left]![Left]"

        ' Delete the selected text using the context menu
        Edit.ClickR
        Edit.PopupMenu.Click "Delete"

        ' Select a portion of the text
        Edit.Keys "^a"

        ' Cut the selected text using the context menu
        Edit.ClickR
        Edit.PopupMenu.Click "Cut"

        ' Post the clipboard content to the log
        Log.Message Sys.Clipboard
      End Sub

      DelphiScript

      procedure Main;
        var p, Edit;
      begin
        // Run Notepad
        WshShell.Run('notepad.exe', SW_SHOWNORMAL);

        // Obtain the edit object
        p := Sys.Process('NOTEPAD');
        Edit := p.Window('Notepad').Window('Edit');
        Edit.Keys('White Rabbitttt');

        // Select a portion of the text
        Edit.Keys('![Left]![Left]![Left]');

        // Delete the selected text using the context menu
        Edit.ClickR;
        Edit.PopupMenu.Click('Delete');

        // Select a portion of the text
        Edit.Keys('^a');

        // Cut the selected text using the context menu
        Edit.ClickR;
        Edit.PopupMenu.Click('Cut');

        // Post the clipboard content to the log
        Log.Message(Sys.Clipboard);
      end;

      C++Script, C#Script

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

        // Obtain the edit object
        var p = Sys["Process"]("NOTEPAD");
        var Edit = p["Window"]("Notepad")["Window"]("Edit");
        Edit["Keys"]("White Rabbitttt");

        // Select a portion of the text
        Edit["Keys"]("![Left]![Left]![Left]");

        // Delete the selected text using the context menu
        Edit["ClickR"]();
        Edit["PopupMenu"]["Click"]("Delete");

        // Select a portion of the text
        Edit["Keys"]("^a");

        // Cut the selected text using the context menu
        Edit["ClickR"]();
        Edit["PopupMenu"]["Click"]("Cut");

        // Post the clipboard content to the log
        Log["Message"](Sys["Clipboard"]);
       }

  • Call the SetText action with an empty string as a parameter, or assign an empty value to the wText property. In this case, the whole text displayed in the edit control will be deleted. The example below demonstrates how to perform this operation:

    JavaScript, JScript

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

      // Obtain the edit object
      var p = Sys.Process("NOTEPAD");
      var Edit = p.Window("Notepad").Window("Edit");
      Edit.Keys("There was nothing so very remarkable in that.");

      // Delete the whole text displayed in the control
      Edit.SetText("");
    }

    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")
      Edit.Keys("There was nothing so very remarkable in that.")
    
      # Delete the whole text displayed in the control
      Edit.SetText("")

    VBScript

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

      ' Obtain the edit object and fill it with text
      Set p = Sys.Process("NOTEPAD")
      Set Edit = p.Window("Notepad").Window("Edit")
      Edit.Keys "There was nothing so very remarkable in that."

      ' Delete the whole text displayed in the control
      Edit.SetText ""
    End Sub

    DelphiScript

    procedure Main;
      var p, Edit:OleVariant;
    begin
      // 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');
      Edit.Keys('There was nothing so very remarkable in that.');

      // Delete the whole text displayed in the control
      Edit.SetText('');
    end;

    C++Script, C#Script

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

      // Obtain the edit object and fill it with text
      var p = Sys.Process("NOTEPAD");
      var Edit = p["Window"]("Notepad")["Window"]("Edit");
      Edit["Keys"]("There was nothing so very remarkable in that.");

      // Delete the whole text displayed in the control
      Edit["SetText"]("");
     }

Note: When you change the text of the edit control using the wText property or the SetText action, the text change event in the tested application is raised twice.

See Also

Working With Edit Controls in Desktop Windows Applications
Simulating Keystrokes
Simulating Menu Actions
Copying and Pasting Text in an Edit Control in Desktop Windows Applications
Replacing Text in an Edit Control in Desktop Windows Applications
Setting Insertion Point in an Edit Control in Desktop Windows Applications
wText Property (Edit Controls)
Working With Text Edit Controls in Web Applications

Highlight search results