Selecting Text Within 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 you may need to select a text within it. If you need to select a portion of a text, you can use following approaches:

  • You can simulate the selection by using the Keys action. In this case you should place the cursor to the desired position (see Setting Insertion Point in an Edit Control in Desktop Windows Applications) and simulate the selection by using the shortcuts. The following code example selects 'who' within the text:

    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");
      Edit.Keys("But who is to give the prizes?");

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

    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("But who is to give the prizes?")
    
      # Select a portion of the text
      Edit.Keys ("[Home][Right][Right][Right][Right]")
      Edit.Keys ("![Right]![Right]![Right]")

    VBScript

    Sub Main
      Dim p, Edit
      ' Run Notepad
      Call 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 "But who is to give the prizes?"

      ' Select a portion of the text
      Edit.Keys "[Home][Right][Right][Right][Right]"
      Edit.Keys "![Right]![Right]![Right]"
    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');
      Edit.Keys('But who is to give the prizes?');

      // Select a portion of the text
      Edit.Keys('[Home][Right][Right][Right][Right]');
      Edit.Keys('![Right]![Right]![Right]');
    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");
      Edit["Keys"]("But who is to give the prizes?");

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

  • Another approach consists in selecting text via the Drag action. Using this action you can select text between two positions. You should specify the start position, from which the selection starts, and the dragging distance in pixels, which specifies the length of the selection.

    Note: Start position is specified in coordinates, which are relative to the object.

    The Drag action requires you to specify two dragging distances, that is the horizontal distance and the vertical distance. If you perform a selection within a single-line control, the vertical dragging distance toY does not affect the selection, so you can set toY = 0 or toY = 100, and the results of the selection will be the same.

    If you work with multiline controls, you can set toY = 0, and in this case you will select a part of one string. If toY distance is not equal to 0, the selection will include several lines (for detailed information see Selecting Text in a Multiline Edit Control in Desktop Windows Applications).

    The example below demonstrates how to select text within an edit control. The code simulates typing a phrase and then selects the word 'who'.

    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");
      Edit.Keys("But who is to give the prizes?");

      // Select a part of the string
      Edit.Drag(35,10,25,0);
    }

    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("But who is to give the prizes?")
    
      # Select a part of the string
      Edit.Drag(35,10,25,0)

    VBScript

    Sub Main
      Dim p, Edit
      ' Run Notepad
      Call 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 "But who is to give the prizes?"

      ' Select a part of the string
      Edit.Drag 35,10,25,0
    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');
      Edit.Keys('But who is to give the prizes?');

      // Select a part of the string
      Edit.Drag(35,10,25,0);
    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");
      Edit["Keys"]("But who is to give the prizes?");
      
      // Select a part of the string
      Edit["Drag"](35,10,25,0);
    }

Selecting all of the text can be performed in different ways:

  • By simulating the Ctrl+A shortcut (for detailed information see Simulating Keystrokes). The following code demonstrates how to select all of the text contained in the edit control using this 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");
      Edit.Keys ("But who is to give the prizes?");

      //Select the text within edit control
      Edit.Keys("^a");
    }

    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 ("But who is to give the prizes?")
    
      # Select the text within edit control
      Edit.Keys("^a")

    VBScript

    Sub Main
      Dim p, Edit
      ' Run Notepad
      Call 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 "But who is to give the prizes?"

      ' Select the text within edit control
      Edit.Keys "^a"
    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');
      Edit.Keys ('But who is to give the prizes?');

      // Select the text within edit control
      Edit.Keys('^a');
    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");
      Edit["Keys"]("But who is to give the prizes?");

      // Select the text within edit control
      Edit["Keys"]("^a");
    }

  • By using the context menu of an edit control. This menu can be called by using the ClickR action. Then you should select the Select All command via the Window.PopupMenu.Click method or simulate the A key. The example below demonstrates how to select all of the text 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");
      Edit.Keys("But who is to give the prizes?");
       // Select text using the context menu
      Edit.ClickR();
      Edit.PopupMenu.Click("Select All");
    }

    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("But who is to give the prizes?")
      # Select text using the context menu
      Edit.ClickR()
      Edit.PopupMenu.Click("Select All")

    VBScript

    Sub Main
      Dim p, Edit
      ' Run Notepad
      Call 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 "But who is to give the prizes?"

      ' Select text using the context menu
      Edit.ClickR
      Edit.PopupMenu.Click "Select All"
    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');
      Edit.Keys('But who is to give the prizes?');

      // Select text using the context menu
      Edit.ClickR;
      Edit.PopupMenu.Click('Select All');
    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");
      Edit["Keys"]("But who is to give the prizes?");

      // Select text using the context menu
      Edit["ClickR"]();
      Edit["PopupMenu"]["Click"]("Select All");
    }

See Also

Working With Edit Controls in Desktop Windows Applications
Simulating Keystrokes
Setting Insertion Point in an Edit Control in Desktop Windows Applications
Selecting Text in a Multiline Edit Control in Desktop Windows Applications
Working With Text Edit Controls in Web Applications

Highlight search results