RichTextBox.SelectionFont 属性

获取或设置当前选定文本或插入点的字体。

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

语法

声明
Public Property SelectionFont As Font
用法
Dim instance As RichTextBox
Dim value As Font

value = instance.SelectionFont

instance.SelectionFont = value
public Font SelectionFont { get; set; }
public:
property Font^ SelectionFont {
    Font^ get ();
    void set (Font^ value);
}
/** @property */
public Font get_SelectionFont ()

/** @property */
public void set_SelectionFont (Font value)
public function get SelectionFont () : Font

public function set SelectionFont (value : Font)

属性值

表示应用到当前选定文本或在插入点后输入的文本的字体的 Font

备注

如果当前选定文本具有多种指定字体,则此属性为 空引用(在 Visual Basic 中为 Nothing)。如果当前未选定任何文本,则此属性中指定的字体应用到当前插入点和在此插入点后键入到控件中的所有文本。字体设置一直应用到此属性更改为其他字体或插入点移动到控件内的其他区域为止。

如果控件内已选定文本,则选定文本和在选定文本之后输入的任何文本都将应用此属性值。可以使用此属性更改 RichTextBox 中的文本字体样式。可以使控件中的文本变成粗体、斜体和带下划线。还可以更改文本的大小和应用到文本的字体。

若要更改控件中的文本颜色,请使用 SelectionColor 属性。

示例

下面的代码示例更改 RichTextBox 控件内的选中文本或在插入点后输入的文本的当前字体粗体样式设置。此示例要求 Form 中的方法内包含此代码。此示例还要求已将名为 richTextBox1RichTextBox 添加到 Form 中。

Private Sub ToggleBold()
   If Not richTextBox1.SelectionFont Is Nothing Then
      Dim currentFont As System.Drawing.Font = richTextBox1.SelectionFont
      Dim newFontStyle As System.Drawing.FontStyle

      If richTextBox1.SelectionFont.Bold = True Then
         newFontStyle = FontStyle.Regular
      Else
         newFontStyle = FontStyle.Bold
      End If

      richTextBox1.SelectionFont = New Font( _
         currentFont.FontFamily, _
         currentFont.Size, _
         newFontStyle _
      )
   End If
End sub
private void ToggleBold()
{
   if (richTextBox1.SelectionFont != null)
   {
      System.Drawing.Font currentFont = richTextBox1.SelectionFont;
      System.Drawing.FontStyle newFontStyle;

      if (richTextBox1.SelectionFont.Bold == true)
      {
         newFontStyle = FontStyle.Regular;
      }
      else
      {
         newFontStyle = FontStyle.Bold;
      }

      richTextBox1.SelectionFont = new Font(
         currentFont.FontFamily, 
         currentFont.Size, 
         newFontStyle
      );
   }
}
void ToggleBold()
{
   if ( richTextBox1->SelectionFont != nullptr )
   {
      System::Drawing::Font^ currentFont = richTextBox1->SelectionFont;
      System::Drawing::FontStyle newFontStyle;
      if ( richTextBox1->SelectionFont->Bold == true )
      {
         newFontStyle = FontStyle::Regular;
      }
      else
      {
         newFontStyle = FontStyle::Bold;
      }
      richTextBox1->SelectionFont = gcnew System::Drawing::Font( currentFont->FontFamily,currentFont->Size,newFontStyle );
   }
}
private void ToggleBold()
{
    if (richTextBox1.get_SelectionFont() != null) {
        System.Drawing.Font currentFont = richTextBox1.get_SelectionFont();
        System.Drawing.FontStyle newFontStyle;

        if (richTextBox1.get_SelectionFont().get_Bold() == true) {
            newFontStyle = FontStyle.Regular;
        }
        else {
            newFontStyle = FontStyle.Bold;
        }

        richTextBox1.set_SelectionFont(
            new Font(currentFont.get_FontFamily(), 
            currentFont.get_Size(), newFontStyle));
    }
} //ToggleBold

平台

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 命名空间
RichTextBox.SelectionColor 属性
Select