NGWS SDK Documentation  

This is preliminary documentation and subject to change.
To comment on this topic, please send us email at ngwssdk@microsoft.com. Thanks!

TextBoxBase.SelectedText

The currently selected text in the control.

[Visual Basic]
Overridable Public Property SelectedText As String
[C#]
public string SelectedText {virtual get; virtual set;}
[C++]
public: __property virtual String* get_SelectedText();
public: __property virtual void set_SelectedText(String*);
[JScript]
public function get SelectedText() : String;
public function set SelectedText(String);

Property Value

A string that represents the currently selected text in the text box.

Remarks

You can assign text to this property to change the text currently selected in the text box. If no text is currently selected in the text box this property returns a zero-length string.

Example [Visual Basic]

The following example uses TextBox, a derived class. It provides System.WinForms.MenuItem.Click event-handling methods for MenuItem objects that perform Cut, Copy, Paste, and Undo operations. This example assumes that a TextBox control named Text1 has been created.

[Visual Basic]

Private Sub Menu_Copy(ByVal sender As System.Object, ByVal e As System.EventArgs)
   ' Ensure that text is selected in the text box.   
   If Text1.SelectionLength > 0 Then
       ' Copy the selected text to the Clipboard.
       Text1.Copy
   End If
End Sub

Private Sub Menu_Cut(ByVal sender As System.Object, ByVal e As System.EventArgs)
   ' Ensure that text is currently selected in the text box.   
   If Not Text1.SelectedText = "" Then
       ' Cut the currently selected text to in text box to Clipboard.
       Text1.Cut
   End If
End Sub

Private Sub Menu_Paste(ByVal sender As System.Object, ByVal e As System.EventArgs)
   ' Determine if there is any text in the Clipboard to paste into the text box.
   If Clipboard.GetDataObject.GetDataPresent(DataFormats.CF_STRINGCLASS) = True Then
       ' Determine if any text is selected in the text box.
       If Text1.SelectionLength > 0 Then
         ' Ask user if they want to paste over currently selected text.
         If MessageBox.Show("Do you want to paste over current selection?", "Cut Example", MessageBox.YesNo) = System.WinForms.DialogResult.No Then
' Move selection to the point after the current selection and paste.
Text1.SelectionStart = Text1.SelectionStart + Text1.SelectionLength
         End If
      End If
      ' Paste current text in Clipboard into text box.
      Text1.Paste
   End If
End Sub


Private Sub Menu_Undo(ByVal sender As System.Object, ByVal e As System.EventArgs)
   ' Determine if we can undo last operation in text box.   
   If Text1.CanUndo = True Then
      ' Undo the last operation.
      Text1.Undo
      ' Clear the undo buffer to prevent last action from being redone.
      Text1.ClearUndo
   End If
End Sub

See Also

TextBoxBase Class | TextBoxBase Members | System.WinForms Namespace | SelectionStart | SelectionLength