如何将多个Font.Styles设置为RTF框 [英] How to set multiple Font.Styles to a Rich Text Box

查看:81
本文介绍了如何将多个Font.Styles设置为RTF框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Rich Text Box,我希望将其设置为多个样式。这是唯一的方法吗?



I have a Rich Text Box with which I would like to set multiple styles to. Is this the only way?

frmMain.rtbStuff.Font = New Font(Me.Font, FontStyle.Bold + FontStyle.Italic)





例如,如果我想创建一个允许开发人员自定义样式的函数。





For instance, if I would like to create a function that will allow a developer to customize the styles as so.

Private Function ConfigureFont(Optional ByRef isUnderline As Boolean = False, Optional ByRef isBold As Boolean = False, Optional ByRef isItalized as Boolean = False) As Boolean

If isUnderline Then
Me.rtbStuff.Font.Styles = FontStyle.Underline ' Does NOT work. Read Only 
End If

Me.rtbStuff.Font.Bold = isBold ' Does NOT work. Read Only

Me.rtbStuff.Font.Italic = isItalized ' Does NOT work. Read Only.

End Function





这没有用,因为它只设置一种字体样式。 http://msdn.microsoft.com/en-us/library/yh8963yx.aspx [ ^ ]



有什么建议吗?



This does not help because it only sets one Font Style. http://msdn.microsoft.com/en-us/library/yh8963yx.aspx[^]

Any suggestions?

推荐答案

看一下 SelectionFont属性 [ ^ ] - 它为文本的特定部分设置特定的字体和样式。听起来像你想要的那样。
Have a look at the SelectionFont property[^] - it sets a specific font and style to a specific part of the text. Which sound like what you want.


你需要设置 SelectionFont 。它适用于所选文本或当前插入符号位置。查看链接以获取更多信息:

http://msdn.microsoft.com/en-us/library/system.windows.forms.richtextbox.selectionfont.aspx#Y57 [ ^ ]



祝你好运!
You would need to set the SelectionFont. It applies to the selected text or from the current caret position. Check the link for more info:
http://msdn.microsoft.com/en-us/library/system.windows.forms.richtextbox.selectionfont.aspx#Y57[^]

Good luck!


我认为这就是你要找的东西。这是我在我的一个程序中使用的函数。它根据工具条按钮的当前状态打开/关闭字体样式。





Private Sub Set_Font_Style()



Dim FStyle As FontStyle



'如果未选择Bold Style则...

如果不是tsbtnBold.Checked那么



'关闭大胆风格

FStyle = rtbItemDetails.SelectionFont.Style而不是FontStyle.Bold < br $>


rtbItemDetails.SelectionFont =新字体(rtbItemDetails.SelectionFont,FStyle)



Else'如果用户选择了Bold风格然后...



'开启大胆风格

FStyle = rtbItemDetails.SelectionFont.Style或FontStyle.Bold



rtbItemDetails.SelectionFont =新字体(rtbItemDetails.SelectionFont,FStyle)



结束如果



'如果我然后选择tallic Style ...

如果不是tsbtnItallics.Checked则



'关闭Itallic Style

FStyle = rtbItemDetails.SelectionFont.Style而非FontStyle.Italic



rtbItemDetails.SelectionFont =新字体(rtbItemDetails.SelectionFont,FStyle)



Else'如果用户选择Bold Style则...



'打开Itallic Style

FStyle = rtbItemDetails.SelectionFont.Style或FontStyle.Italic



rtbItemDetails.SelectionFont = New Font(rtbItemDetails.SelectionFont,FStyle)



结束如果



'如果未选择下划线样式则...

如果不是tsbtnUnderline.Checked则



'关闭下划线样式

FStyle = rtbItemDetails.SelectionFont.Style而不是Fo ntStyle.Underline



rtbItemDetails.SelectionFont =新字体(rtbItemDetails.SelectionFont,FStyle)



Else'如果用户选择了Underline Style,那么......



'打开下划线样式

FStyle = rtbItemDetails.SelectionFont.Style或FontStyle.Underline < br $>


rtbItemDetails.SelectionFont =新字体(rtbItemDetails.SelectionFont,FStyle)



结束如果



'如果没有选择Strikeout Style那么...

如果不是tsbtnStrkethrough.Checked那么



'关闭Strikethrough风格

FStyle = rtbItemDetails.SelectionFont.Style而非FontStyle.Strikeout



rtbItemDetails.SelectionFont = New Font (rtbItemDetails.SelectionFont,FStyle)



Else'如果用户选择了Strikeout Style,那么。 ..



'开启Strikeout风格

FStyle = rtbItemDetails.SelectionFont.Style或FontStyle.Strikeout



rtbItemDetails.SelectionFont =新字体(rtbItemDetails.SelectionFont,FStyle)



结束如果



'更新按钮状态

Button_Control()



'给予RichTextBox焦点。

rtbItemDetails .Focus()



End Sub
I think this is what you are looking for. This is a function I'm using in one of my programs. It turns the font styles on/off depending on the current state of the toolstrip buttons.


Private Sub Set_Font_Style()

Dim FStyle As FontStyle

'If Bold Style is not selected then...
If Not tsbtnBold.Checked Then

'Turn off Bold Style
FStyle = rtbItemDetails.SelectionFont.Style And Not FontStyle.Bold

rtbItemDetails.SelectionFont = New Font(rtbItemDetails.SelectionFont, FStyle)

Else 'If user selected Bold Style then...

'Turn on Bold Style
FStyle = rtbItemDetails.SelectionFont.Style Or FontStyle.Bold

rtbItemDetails.SelectionFont = New Font(rtbItemDetails.SelectionFont, FStyle)

End If

'If Itallic Style is not selected then...
If Not tsbtnItallics.Checked Then

'Turn off Itallic Style
FStyle = rtbItemDetails.SelectionFont.Style And Not FontStyle.Italic

rtbItemDetails.SelectionFont = New Font(rtbItemDetails.SelectionFont, FStyle)

Else 'If user selected Bold Style then...

'Turn on Itallic Style
FStyle = rtbItemDetails.SelectionFont.Style Or FontStyle.Italic

rtbItemDetails.SelectionFont = New Font(rtbItemDetails.SelectionFont, FStyle)

End If

'If Underline Style is not selected then...
If Not tsbtnUnderline.Checked Then

'Turn off Underline Style
FStyle = rtbItemDetails.SelectionFont.Style And Not FontStyle.Underline

rtbItemDetails.SelectionFont = New Font(rtbItemDetails.SelectionFont, FStyle)

Else 'If user selected Underline Style then...

'Turn on Underline Style
FStyle = rtbItemDetails.SelectionFont.Style Or FontStyle.Underline

rtbItemDetails.SelectionFont = New Font(rtbItemDetails.SelectionFont, FStyle)

End If

'If Strikeout Style is not selected then...
If Not tsbtnStrkethrough.Checked Then

'Turn off Strikethrough Style
FStyle = rtbItemDetails.SelectionFont.Style And Not FontStyle.Strikeout

rtbItemDetails.SelectionFont = New Font(rtbItemDetails.SelectionFont, FStyle)

Else 'If user selected Strikeout Style then...

'Turn on Strikeout Style
FStyle = rtbItemDetails.SelectionFont.Style Or FontStyle.Strikeout

rtbItemDetails.SelectionFont = New Font(rtbItemDetails.SelectionFont, FStyle)

End If

'Update Button Status
Button_Control()

'Give RichTextBox Focus.
rtbItemDetails.Focus()

End Sub


这篇关于如何将多个Font.Styles设置为RTF框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆