Selection property not working

2019-09-09 14:05发布

string holder = richTextBox1.Selection.Text;

The .Selection produces an error :

'System.Windows.Forms.RichTextBox' does not contain a definition for 'Selection' and no extension method 'Selection' accepting a first argument of type 'System.Windows.Forms.RichTextBox' could be found (are you missing a using directive or an assembly reference?).

1条回答
闹够了就滚
2楼-- · 2019-09-09 14:47

A RichtTextBox doesn't have a Selection property, hence the error message.

It does have a SelectionStart and SelectionLength property, which you can use to get the selection text yourself:

string selectedText = rtb.Text.Substring(rtb.SelectionStart, rtb.Length);

Or simply use SelectedText

string selectedText = rtb.SelectedText;
查看更多
登录 后发表回答