RichTextBox.SelectionFont 属性
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
获取或设置当前文本选择或插入点的字体。
public:
property System::Drawing::Font ^ SelectionFont { System::Drawing::Font ^ get(); void set(System::Drawing::Font ^ value); };
[System.ComponentModel.Browsable(false)]
public System.Drawing.Font SelectionFont { get; set; }
[System.ComponentModel.Browsable(false)]
public System.Drawing.Font? SelectionFont { get; set; }
[<System.ComponentModel.Browsable(false)>]
member this.SelectionFont : System.Drawing.Font with get, set
Public Property SelectionFont As Font
属性值
一个 Font 表示要应用于当前文本选择的字体或插入点后输入的文本的字体。
- 属性
示例
下面的代码示例更改控件内插入点 RichTextBox 后输入的文本选择或文本的当前字体粗体样式设置。 此示例要求代码包含在方法中 Form。 该示例还要求已向
void ToggleBold()
{
if ( richTextBox1->SelectionFont != nullptr )
{
System::Drawing::Font^ currentFont = richTextBox1->SelectionFont;
System::Drawing::FontStyle newFontStyle;
if (richTextBox1->SelectionFont->Bold)
{
newFontStyle = FontStyle::Regular;
}
else
{
newFontStyle = FontStyle::Bold;
}
richTextBox1->SelectionFont = gcnew System::Drawing::Font( currentFont->FontFamily,currentFont->Size,newFontStyle );
}
}
private void ToggleBold()
{
if (richTextBox1.SelectionFont != null)
{
System.Drawing.Font currentFont = richTextBox1.SelectionFont;
System.Drawing.FontStyle newFontStyle;
if (richTextBox1.SelectionFont.Bold)
{
newFontStyle = FontStyle.Regular;
}
else
{
newFontStyle = FontStyle.Bold;
}
richTextBox1.SelectionFont = new Font(
currentFont.FontFamily,
currentFont.Size,
newFontStyle
);
}
}
Private Sub ToggleBold()
If richTextBox1.SelectionFont IsNot 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
注解
如果当前文本选择指定了多个字体,则此属性为 null。 如果未选择任何文本,则此属性中指定的字体将应用于当前插入点和插入点之后键入控件的所有文本。 字体设置将应用于属性更改为其他字体,或者直到插入点移动到控件中的另一节。
如果在控件中选择文本,则所选文本和文本选择后输入的任何文本都将应用此属性的值。 可以使用此属性更改文本的 RichTextBox字体样式。 可以将控件中的文本设为粗体、斜体和下划线。 还可以更改文本的大小和应用于文本的字体。
若要更改控件中文本的颜色,请使用 SelectionColor 该属性。