We help IT Professionals succeed at work.

How to copy selected text

pratikshahse
pratikshahse asked
on
i am trying to do cut and paste using ctrl ^x  and ctrl^v from my form. i want to copy selected text but . here is the code that i use.

 if (this.ActiveMdiChild.ActiveControl is BMWFSAM.Cafe.Desktop.Controls.TextBox)
            {
                Clipboard.Clear();
               
                Clipboard.SetDataObject(this.ActiveMdiChild.ActiveControl.Text.ToString());
                this.ActiveMdiChild.ActiveControl.Text = "";  
            }

this allows me to copy the entire text from the textbox but I want to do selectedbox . I cannot find .SelectedText property over here . is there anything else that i can use to cut selected text.
Comment
Watch Question

Top Expert 2006

Commented:
What is BMWFSAM.Cafe.Desktop.Controls.TextBox?  Does it have SelectionStart or SelectionLength methods?  Could you use SendKeys.Send("^X")?

Author

Commented:
Its just a regular textbox. and no it does not have selectionstart or selectionlength
Top Expert 2006
Commented:
If your control is just a regular textbox (i.e. it inherits from System.Windows.Forms.TextBox), then the following should work.
  if (this.ActiveMdiChild.ActiveControl is BMWFSAM.Cafe.Desktop.Controls.TextBox)
  {
    BMWFSAM.Cafe.Desktop.Controls.TextBox textBox = (BMWFSAM.Cafe.Desktop.Controls.TextBox)this.ActiveMdiChild.ActiveControl;
 
    Clipboard.Clear();
    Clipboard.SetDataObject(textBox.SelectedText);
    textBox.SelectedText = "";
  }

Open in new window

Explore More ContentExplore courses, solutions, and other research materials related to this topic.