Share via


RichTextBox.SelectionProtected 属性

获取或设置一个值,通过该值指示是否保护当前选定文本。

**命名空间:**System.Windows.Forms
**程序集:**System.Windows.Forms(在 system.windows.forms.dll 中)

语法

声明
Public Property SelectionProtected As Boolean
用法
Dim instance As RichTextBox
Dim value As Boolean

value = instance.SelectionProtected

instance.SelectionProtected = value
public bool SelectionProtected { get; set; }
public:
property bool SelectionProtected {
    bool get ();
    void set (bool value);
}
/** @property */
public boolean get_SelectionProtected ()

/** @property */
public void set_SelectionProtected (boolean value)
public function get SelectionProtected () : boolean

public function set SelectionProtected (value : boolean)

属性值

如果禁止更改当前选择的任何内容,则为 true;否则为 false。默认为 false

备注

如果当前未选定任何文本,则保护设置应用到插入点所在的段落和在此插入点后键入到控件中的所有文本。保护设置一直应用到此属性更改为其他值或插入点移动到控件内的其他段落为止。

如果控件内已选定文本,则选定文本和在选定文本之后输入的任何文本都将应用此属性值。可以使用此属性防止用户修改控件内的选定文本。

如果此属性设置为 true,则当用户试图更改当前选定文本时,将引发 Protected 事件。

提示

仅当控件内的整个选定文本包含受保护的内容时,此属性才返回 true

示例

下面的代码示例演示如何使用 SelectionProtected 属性指定 RichTextBox 内受保护的文本。此示例要求名为 richTextBox1RichTextBox 控件已添加到窗体中,且 RichTextBox 控件中已添加包含单词“RichTextBox”的文本。

Private Sub ProtectMySelectedText()
   ' Determine if the selected text in the control contains the word "RichTextBox".
   If richTextBox1.SelectedText <> "RichTextBox" Then
      ' Search for the word RichTextBox in the control.
      If richTextBox1.Find("RichTextBox", RichTextBoxFinds.WholeWord) = -1 Then
         'Alert the user that the word was not foun and return.
         MessageBox.Show("The text ""RichTextBox"" was not found!")
         Return
      End If
   End If
   ' Protect the selected text in the control from being altered.
   richTextBox1.SelectionProtected = True
End Sub
private void ProtectMySelectedText()
{
   // Determine if the selected text in the control contains the word "RichTextBox".
   if(richTextBox1.SelectedText != "RichTextBox")
   {
      // Search for the word RichTextBox in the control.
      if(richTextBox1.Find("RichTextBox",RichTextBoxFinds.WholeWord)== -1)
      {
         //Alert the user that the word was not foun and return.
         MessageBox.Show("The text \"RichTextBox\" was not found!");
         return;
      }
   }
   // Protect the selected text in the control from being altered.
   richTextBox1.SelectionProtected = true;
}
private:
   void ProtectMySelectedText()
   {
      // Determine if the selected text in the control contains the word "RichTextBox".
      if (  !richTextBox1->SelectedText->Equals( "RichTextBox" ) )
      {
         // Search for the word RichTextBox in the control.
         if ( richTextBox1->Find( "RichTextBox", RichTextBoxFinds::WholeWord ) == -1 )
         {
            //Alert the user that the word was not foun and return.
            MessageBox::Show( "The text \"RichTextBox\" was not found!" );
            return;
         }
      }

      // Protect the selected text in the control from being altered.
      richTextBox1->SelectionProtected = true;
   }
private void ProtectMySelectedText()
{
    // Determine if the selected text in the control contains the word 
    //"RichTextBox".
    if (richTextBox1.get_SelectedText().Equals("RichTextBox")) {
        // Search for the word RichTextBox in the control.
        if (richTextBox1.Find("RichTextBox",
            RichTextBoxFinds.WholeWord) == -1) {
            //Alert the user that the word was not foun and return.
            MessageBox.Show("The text \"RichTextBox\" was not found!");
            return;
        }
    }
    // Protect the selected text in the control from being altered.
    richTextBox1.set_SelectionProtected(true);
} //ProtectMySelectedText

平台

Windows 98、Windows 2000 SP4、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Mobile for Smartphone、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

请参见

参考

RichTextBox 类
RichTextBox 成员
System.Windows.Forms 命名空间
Protected