使用字体对话框更改字体 C#

发布于 2024-08-22 17:32:33 字数 395 浏览 3 评论 0原文

谁能告诉我如何使用字体对话框更改字体。我正在尝试获取它,以便选定的文本发生变化,或者如果没有选择文本,则仅更改标记后的字体(而不是整个文本框)。

这是我到目前为止所拥有的。谢谢

 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

鸵鸟症 2024-08-29 17:32:33
private void menuFont_Click(object sender, EventArgs e)
{
  if (fontDialog1.ShowDialog() == DialogResult.OK & !String.IsNullOrEmpty(richtextbox.Text))
  {
      richtextbox.SelectionFont = fontDialog1.Font;
  }
  else
  {
     //  richtextbox.SelectionFont = ?
  }
} 

编辑:

如果fontDialog1.ShowDialog() == DialogResult.OKfalse,则可以使用&&根据 user210118 建议,仅此条件就满足 else 子句的使用

private void menuFont_Click(object sender, EventArgs e)
{
  if (fontDialog1.ShowDialog() == DialogResult.OK & !String.IsNullOrEmpty(richtextbox.Text))
  {
      richtextbox.SelectionFont = fontDialog1.Font;
  }
  else
  {
     //  richtextbox.SelectionFont = ?
  }
} 

EDIT:

you may use && if fontDialog1.ShowDialog() == DialogResult.OKis false and this condition alone satisfies the use for else clause, as per user210118 recommendation

同展鸳鸯锦 2024-08-29 17:32:33

不是有 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.

酒绊 2024-08-29 17:32:33

使用 SelectionFont 属性RichTextBox,该示例将根据您的需要工作:

private void menuFont_Click(object sender, EventArgs e)
{
    if (fontDialog1.ShowDialog() == DialogResult.OK) {
        richtextbox.SelectionFont = fontDialog1.Font;
    }
}

Use the SelectionFont property of the RichTextBox, the example will work as you need:

private void menuFont_Click(object sender, EventArgs e)
{
    if (fontDialog1.ShowDialog() == DialogResult.OK) {
        richtextbox.SelectionFont = fontDialog1.Font;
    }
}
南烟 2024-08-29 17:32:33

要使输入的以下文本为不同的字体,而不仅仅是选定的文本,您需要向 RTB 添加运行块,然后写入其中。我为 RTB 实现了一个工具栏,其功能如下:

    public static void SetFontSize(RichTextBox target, double value)
    {
        // Make sure we have a richtextbox.
        if (target == null)
            return;

        // Make sure we have a selection. Should have one even if there is no text selected.
        if (target.Selection != null)
        {
            // Check whether there is text selected or just sitting at cursor
            if (target.Selection.IsEmpty)
            {
                // Check to see if we are at the start of the textbox and nothing has been added yet
                if (target.Selection.Start.Paragraph == null)
                {
                    // Add a new paragraph object to the richtextbox with the fontsize
                    Paragraph p = new Paragraph();
                    p.FontSize = value;
                    target.Document.Blocks.Add(p);
                }
                else
                {
                    // Get current position of cursor
                    TextPointer curCaret = target.CaretPosition;
                    // Get the current block object that the cursor is in
                    Block curBlock = target.Document.Blocks.Where
                        (x => x.ContentStart.CompareTo(curCaret) == -1 && x.ContentEnd.CompareTo(curCaret) == 1).FirstOrDefault();
                    if (curBlock != null)
                    {
                        Paragraph curParagraph = curBlock as Paragraph;
                        // Create a new run object with the fontsize, and add it to the current block
                        Run newRun = new Run();
                        newRun.FontSize = value;
                        curParagraph.Inlines.Add(newRun);
                        // Reset the cursor into the new block. 
                        // If we don't do this, the font size will default again when you start typing.
                        target.CaretPosition = newRun.ElementStart;
                    }
                }
            }
            else // There is selected text, so change the fontsize of the selection
            {
                TextRange selectionTextRange = new TextRange(target.Selection.Start, target.Selection.End);
                selectionTextRange.ApplyPropertyValue(TextElement.FontSizeProperty, value);
            }
        }
        // Reset the focus onto the richtextbox after selecting the font in a toolbar etc
        target.Focus();
    }

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:

    public static void SetFontSize(RichTextBox target, double value)
    {
        // Make sure we have a richtextbox.
        if (target == null)
            return;

        // Make sure we have a selection. Should have one even if there is no text selected.
        if (target.Selection != null)
        {
            // Check whether there is text selected or just sitting at cursor
            if (target.Selection.IsEmpty)
            {
                // Check to see if we are at the start of the textbox and nothing has been added yet
                if (target.Selection.Start.Paragraph == null)
                {
                    // Add a new paragraph object to the richtextbox with the fontsize
                    Paragraph p = new Paragraph();
                    p.FontSize = value;
                    target.Document.Blocks.Add(p);
                }
                else
                {
                    // Get current position of cursor
                    TextPointer curCaret = target.CaretPosition;
                    // Get the current block object that the cursor is in
                    Block curBlock = target.Document.Blocks.Where
                        (x => x.ContentStart.CompareTo(curCaret) == -1 && x.ContentEnd.CompareTo(curCaret) == 1).FirstOrDefault();
                    if (curBlock != null)
                    {
                        Paragraph curParagraph = curBlock as Paragraph;
                        // Create a new run object with the fontsize, and add it to the current block
                        Run newRun = new Run();
                        newRun.FontSize = value;
                        curParagraph.Inlines.Add(newRun);
                        // Reset the cursor into the new block. 
                        // If we don't do this, the font size will default again when you start typing.
                        target.CaretPosition = newRun.ElementStart;
                    }
                }
            }
            else // There is selected text, so change the fontsize of the selection
            {
                TextRange selectionTextRange = new TextRange(target.Selection.Start, target.Selection.End);
                selectionTextRange.ApplyPropertyValue(TextElement.FontSizeProperty, value);
            }
        }
        // Reset the focus onto the richtextbox after selecting the font in a toolbar etc
        target.Focus();
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文