Selecting Text in a Multiline Edit Control

Applies to TestComplete 14.10, last modified on June 5, 2019

When working with a multiline control, you may need to select a portion of the text, for example, to replace it with different text or copy the selected text. The main approaches for selecting text are similar to those used for single-line edit controls (see Selecting Text Within an Edit Control). But there are some distinctions: while you can only select one string or a part of this string within a single-line edit control, a multiline edit control allows you to select several strings or the desired part of a set of strings. The following topic describes the different ways of selecting text within a multiline edit control.

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.

You can perform the selection within an edit control by using the Drag action. This action belongs to the Win32Edit object and allows you to select the text between two specified positions by simulating a dragging event. When using this action you need to specify the following parameters:

  • Horizontal and vertical start positions, which set at the beginning point of the selection in pixels. These coordinates are relative to the object.
  • Horizontal and vertical dragging distance, which determine the length of the selection in pixels. If vertical dragging distance is equal to 0, the selection will contain a portion of one string, otherwise it will contain several strings.

The following code demonstrates how to perform a selection of text between two desired positions:

JavaScript, JScript

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");
  Edit.Keys("I forgot you didn`t like cats.[Enter]");
  Edit.Keys("Not like cats!! Would you like cats if you were me?[Enter]");
  Edit.Keys("Well, perhaps not.");

  // Select the text
  Edit.Drag(30,20,40,10);
}

Python

def Main():
  # 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("I forgot you didn`t like cats.[Enter]")
  Edit.Keys("Not like cats!! Would you like cats if you were me?[Enter]")
  Edit.Keys("Well, perhaps not.")

  # Select the text
  Edit.Drag(30,20,40,10)

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 "I forgot you didn`t like cats.[Enter]"
  Edit.Keys "Not like cats!! Would you like cats if you were me?[Enter]"
  Edit.Keys "Well, perhaps not."

  ' Select the text
  Edit.Drag 30,20,40,10
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('I forgot you didn`t like cats.[Enter]');
  Edit.Keys('Not like cats!! Would you like cats if you were me?[Enter]');
  Edit.Keys('Well, perhaps not.');

  // Select the text
  Edit.Drag(30,20,40,10);
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");
  Edit["Keys"]("I forgot you didn`t like cats.[Enter]");
  Edit["Keys"]("Not like cats!! Would you like cats if you were me?[Enter]");
  Edit["Keys"]("Well, perhaps not.");

  // Select the text
  Edit["Drag"](30,20,40,10);
}

Using the Drag action implies that you must have the mouse coordinates, which specify the start position of the selection. You can detect the mouse position, for example, by recording a script (see Recording Tests), but this approach is rather difficult. A more practical way to select text within a control consists of selecting text by simulating keystrokes.

You can select text within a multiline edit control in different ways depending on what you want to select:

  • If you need to select a part of a string you must place the cursor to the desired position and simulate the selection of the text. The approaches that you can use to set cursor position are described in the Placing Cursor Within a Multiline Edit Control topic. Selecting text in this case is similar to selecting text within a single-line edit control. The following example demonstrates how to select a part of a string within a multiline control:

    JavaScript, JScript

    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");
      Edit.Keys("'Well! I've often seen a cat without a grin,' thought Alice;[Enter]");
      Edit.Keys("'but a grin without a cat!!'");

      // Place cursor
      Edit.Keys("[Home][Right][Right][Right][Right][Right][Right][Right]");

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

    Python

    def Main():
      # 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("'Well! I've often seen a cat without a grin,' thought Alice[Enter]")
      Edit.Keys("'but a grin without a cat!!'")
    
      # Place cursor
      Edit.Keys("[Home][Right][Right][Right][Right][Right][Right][Right]")
    
      # Select the text
      Edit.Keys("![Right]![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 "'Well! I've often seen a cat without a grin,' thought Alice;[Enter]"
      Edit.Keys "'but a grin without a cat!!'"

      ' Place cursor
      Edit.Keys "[Home][Right][Right][Right][Right][Right][Right][Right]"

      ' Select the text
      Edit.Keys "![Right]![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 and fill it with text
      p := Sys.Process('NOTEPAD');
      Edit := p.Window('Notepad').Window('Edit');
      Edit.Keys('''Well! I`ve often seen a cat without a grin,'' thought Alice;[Enter]');
      Edit.Keys('but a grin without a cat! ');

      // Place cursor
      Edit.Keys('[Home][Right][Right][Right][Right][Right][Right][Right]');

      // Select the text
      Edit.Keys('![Right]![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 and fill it with text
      p = Sys["Process"]("NOTEPAD");
      Edit = p["Window"]("Notepad")["Window"]("Edit");
      Edit["Keys"]("'Well! I`ve often seen a cat without a grin,' thought Alice;[Enter]");
      Edit["Keys"]("'but a grin without a cat! '");

      // Place cursor
      Edit["Keys"]("[Home][Right][Right][Right][Right][Right][Right][Right]");

      // Select the text
      Edit["Keys"]("![Right]![Right]![Right]![Right]");
     }

  • If you need to select one string or several strings, you must place the cursor to the beginning of the string and then you can select them in the following ways:
    • Simulate the Shift+End keys to select the desired string.
    • Simulate the Shift+Down or Shift+Up keys to select a particular string or perform this operation several times to select a set of strings.

    The code example below demonstrates how to select the entire strings:

    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("'Well! I've often seen a cat without a grin,'[Enter]");
      Edit.Keys("thought Alice;[Enter]");
      Edit.Keys("'but a grin without a cat!!'");

      //Place cursor
      Edit.Keys("[Up][Up][Home]");

      //Select one string entirely
      Edit.Keys("![End]");

      //Place cursor
      Edit.Keys("[Right]");

      //Select a set of strings
      Edit.Keys("![Down]![End]");
    }

    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("'Well! I've often seen a cat without a grin,'[Enter]")
      Edit.Keys("thought Alice[Enter]")
      Edit.Keys("'but a grin without a cat!!'")
    
      # Place cursor
      Edit.Keys("[Up][Up][Home]")
    
      # Select one string entirely
      Edit.Keys("![End]")
    
      # Place cursor
      Edit.Keys("[Right]")
    
      # Select a set of strings
      Edit.Keys("![Down]![End]")

    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 "'Well! I've often seen a cat without a grin,' [Enter]"
      Edit.Keys "thought Alice;[Enter]"
      Edit.Keys "'but a grin without a cat!!'"

      'Place cursor
      Edit.Keys "[Up][Up][Home]"

      'Select one string entirely
      Edit.Keys "![End]"

      'Place cursor
      Edit.Keys "[Right]"

      'Select a set of strings
      Edit.Keys "![Down]![End]"
    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('''Well! I`ve often seen a cat without a grin,''[Enter]');
      Edit.Keys('thought Alice;[Enter]');
      Edit.Keys('''but a grin without a cat!!'' ');

      //Place cursor
      Edit.Keys('[Up][Up][Home]');

      //Select one string entirely
      Edit.Keys('![End]');

      //Place cursor
      Edit.Keys('[Right]');

      //Select a set of strings
      Edit.Keys('![Down]![End]');
    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");
      Edit["Keys"]("'Well! I`ve often seen a cat without a grin,'[Enter]");
      Edit["Keys"]("thought Alice;[Enter]");
      Edit["Keys"]("'but a grin without a cat!!'");

      //Place cursor
      Edit["Keys"]("[Up][Up][Home]");

      //Select one string entirely
      Edit["Keys"]("![End]");

      //Place cursor
      Edit["Keys"]("[Right]");

      //Select a set of strings
      Edit["Keys"]("![Down]![End]");
    }

  • If you need to select a text portion, you must place the cursor at the beginning of the text you want to select and then perform the selection. You can select whole strings of the required text by the methods mentioned above, and after that select the rest of the strings by simulating keystrokes. The example below illustrates how you can perform this operation:

    JavaScript, JScript

    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");
      Edit.Keys("I forgot you didn`t like cats.[Enter]");
      Edit.Keys("Not like cats!! Would you like cats if you were me?[Enter]");
      Edit.Keys("Well, perhaps not.");

      // Place cursor
      Edit.Keys("[Up][Up][Home][Right][Right]");

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

    Python

    def Main():
      # 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("I forgot you didn`t like cats.[Enter]")
      Edit.Keys("Not like cats!! Would you like cats if you were me?[Enter]")
      Edit.Keys("Well, perhaps not.")
    
      # Place cursor
      Edit.Keys("[Up][Up][Home][Right][Right]")
    
      # Select a portion of the text
      Edit.Keys("![Down]")
      Edit.Keys("![Right]![Right]![Right]![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 "I forgot you didn`t like cats.[Enter]"
      Edit.Keys "Not like cats!! Would you like cats if you were me?[Enter]"
      Edit.Keys "Well, perhaps not."

      ' Place cursor
      Edit.Keys "[Up][Up][Home][Right][Right]"

      ' Select a portion of the text
      Edit.Keys "![Down]"
      Edit.Keys "![Right]![Right]![Right]![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('I forgot you didn`t like cats.[Enter]');
      Edit.Keys('Not like cats!! Would you like cats if you were me?[Enter]');
      Edit.Keys('Well, perhaps not.');

      // Place cursor
      Edit.Keys('[Up][Up][Home][Right][Right]');

      // Select a portion of the text
      Edit.Keys('![Down]');
      Edit.Keys('![Right]![Right]![Right]![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 and fill it with text
      p = Sys["Process"]("NOTEPAD");
      Edit = p["Window"]("Notepad")["Window"]("Edit");
      Edit["Keys"]("I forgot you didn`t like cats.[Enter]");
      Edit["Keys"]("Not like cats!! Would you like cats if you were me?[Enter]");
      Edit["Keys"]("Well, perhaps not.");

      // Place cursor
      Edit["Keys"]("[Up][Up][Home][Right][Right]");

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

  • Selecting all of text within a multiline control is similar to selecting all of the text within a single-line control. It can be done by simulating hot keys or using the context menu. For more information see Selecting Text Within an Edit Control. The following example demonstrates how to select all of the text within a multiline edit control:

    JavaScript, JScript

    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");
      Edit.Keys("I quite forgot you didn`t like cats.[Enter]");
      Edit.Keys("Not like cats!! Would YOU like cats if you were me?[Enter]");
      Edit.Keys("Well, perhaps not.");

      // Select all text
      Edit.Keys("^a");
    }

    Python

    def Main():
      # 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("I quite forgot you didn`t like cats.[Enter]")
      Edit.Keys("Not like cats!! Would YOU like cats if you were me?[Enter]")
      Edit.Keys("Well, perhaps not.")
    
      # Select all text
      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 "I forgot you didn`t like cats.[Enter]"
      Edit.Keys "Not like cats!! Would you like cats if you were me?[Enter]"
      Edit.Keys "Well, perhaps not."

      ' Select all text
      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('I quite forgot you didn`t like cats.[Enter]');
      Edit.Keys('Not like cats!! Would YOU like cats if you were me?[Enter]');
      Edit.Keys('Well, perhaps not.');

      // Select all text
      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 and fill it with text
      p = Sys["Process"]("NOTEPAD");
      Edit = p["Window"]("Notepad")["Window"]("Edit");
      Edit["Keys"]("I quite forgot you didn`t like cats.[Enter]");
      Edit["Keys"]("Not like cats!! Would YOU like cats if you were me?[Enter]");
      Edit["Keys"]("Well, perhaps not.");

      // Select all text
      Edit["Keys"]("^a");
     }

See Also

Working With Multiline Edit Controls - Specifics
Selecting Text Within an Edit Control
Placing Cursor Within a Multiline Edit Control
Recording Tests

Highlight search results