选择 属性 无效

Selection property not working

string holder = richTextBox1.Selection.Text;

.Selection 产生错误:

'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?).

A RichtTextBox 没有 Selection 属性,因此出现错误消息。

它确实有一个 SelectionStart and SelectionLength 属性,您可以使用它来自己获取选择文本:

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

或者直接使用SelectedText

string selectedText = rtb.SelectedText;