使用字体对话框更改字体 C#
谁能告诉我如何使用字体对话框更改字体。我正在尝试获取它,以便选定的文本发生变化,或者如果没有选择文本,则仅更改标记后的字体(而不是整个文本框)。
这是我到目前为止所拥有的。谢谢
private void menuFont_Click(object sender, EventArgs e)
{
if (fontDialog1.ShowDialog() == DialogResult.OK)
{
if (richtextbox.SelectedText != "")
{
richtextbox.Font = fontDialog1.Font;
}
}}
Can anyone tell me how I can change the font using a font dialog. I'm trying to get it so either the selected text changes or if no text is selected only the font after the marker gets changed (not the whole textbox).
This is what I have so far. Thanx
private void menuFont_Click(object sender, EventArgs e)
{
if (fontDialog1.ShowDialog() == DialogResult.OK)
{
if (richtextbox.SelectedText != "")
{
richtextbox.Font = fontDialog1.Font;
}
}}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
编辑:
如果
fontDialog1.ShowDialog() == DialogResult.OK
为false
,则可以使用&&
根据user210118
建议,仅此条件就满足 else 子句的使用EDIT:
you may use
&&
iffontDialog1.ShowDialog() == DialogResult.OK
isfalse
and this condition alone satisfies the use for else clause, as peruser210118
recommendation不是有 SelFont、SelX 将字体属性应用于所选文本吗?现在想来,也许是SelectedX,但应用程序是一样的。
Isn't there SelFont, SelX that apply font properties to the selected text? Now that I think about it, maybe it's SelectedX, but the application is the same.
使用 SelectionFont 属性RichTextBox,该示例将根据您的需要工作:
Use the SelectionFont property of the RichTextBox, the example will work as you need:
要使输入的以下文本为不同的字体,而不仅仅是选定的文本,您需要向 RTB 添加运行块,然后写入其中。我为 RTB 实现了一个工具栏,其功能如下:
To get the following text entered to be a different font, and not just the selected text, you need to add a run block to the RTB and then write into it. I implemented a toolbar for a RTB that does something like this: