locked
How to get a highlighted text into the clipboard? RRS feed

  • 問題

  • I use VB .NET 2005.  I want to put a highlighted text into the clipboard.  I use the below code:

     

    me.computer.clipboard.settext(me.activechildform.activecontrol.text)

     

    But the whole text in the control is selected.  Can anyone give me some insight?
    2008年5月20日 上午 03:28

解答

  • If you want to select a part of the text in a TextBox control, you can make use of these porperties:

    textBox1.SelectionStart
    textBox1.SelectionLength
    textBox1.SelectedText

    If you find that your text is not highlighted, it might be your textbox is not in focus. Try this:

    textBox1.Focus();

    I am not clear what Jacky is refering to but I find this is working even on TextBox. Tested with VS 2005 C#.

    FYI: http://msdn.microsoft.com/en-us/library/hk09zy8f.aspx

    2008年5月21日 上午 03:17
  • Hi Jack,

     

    You are correct... SelectedText, SelectionStart and SelectionLength is Inherited from TextBoxBase, just get lost...

     

    And Martin, I think the example is exact what you are looking for:

    http://msdn.microsoft.com/en-us/library/system.windows.forms.textboxbase.selectedtext.aspx

    2008年5月21日 上午 11:49

所有回覆

  • If you are using "TexBox", you cannot select the highlighted text.

     

    You can make it work in RichTextBox. There has RichTextBox.Selectedtext, RichTextBox.SelectionIndex, RichTextBox.SelectionLength

     

    Enjoy it~

    2008年5月20日 上午 04:10
  • If you want to select a part of the text in a TextBox control, you can make use of these porperties:

    textBox1.SelectionStart
    textBox1.SelectionLength
    textBox1.SelectedText

    If you find that your text is not highlighted, it might be your textbox is not in focus. Try this:

    textBox1.Focus();

    I am not clear what Jacky is refering to but I find this is working even on TextBox. Tested with VS 2005 C#.

    FYI: http://msdn.microsoft.com/en-us/library/hk09zy8f.aspx

    2008年5月21日 上午 03:17
  • Hi Jack,

     

    You are correct... SelectedText, SelectionStart and SelectionLength is Inherited from TextBoxBase, just get lost...

     

    And Martin, I think the example is exact what you are looking for:

    http://msdn.microsoft.com/en-us/library/system.windows.forms.textboxbase.selectedtext.aspx

    2008年5月21日 上午 11:49