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

3

有人能告诉我如何使用字体对话框更改字体吗?我想让选定的文本更改,或者如果没有选择文本,则仅更改标记后的字体(而不是整个文本框)。

这是我目前所拥有的。谢谢。

 private void menuFont_Click(object sender, EventArgs e)
    {
        if (fontDialog1.ShowDialog() == DialogResult.OK)
        {
            if (richtextbox.SelectedText != "")
            {
                richtextbox.Font = fontDialog1.Font;
            }
    }}
4个回答

1
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,并且这个条件单独满足else子句的使用,根据{{link1:user210118}}建议。


1
在条件语句中,可能需要使用&&而不是&。 - brian
"&",表示如果第一个条件为假,则无需检查第二个! - Asad
不确定 else 是否必要,或者它是否会做你认为的事情。 - brian
&& 运算符条件-与运算符(&&)对其布尔操作数执行逻辑AND运算,但仅在必要时评估其第二个操作数。 - brian
2
没错,这就是为什么要使用&&。我认为你把&和&&的定义搞反了。(http://msdn.microsoft.com/en-us/library/2a723cdk%28VS.71%29.aspx) - brian
显示剩余2条评论

0
使用 RichTextBox 的 SelectionFont 属性,示例将按照您的需求工作:
private void menuFont_Click(object sender, EventArgs e)
{
    if (fontDialog1.ShowDialog() == DialogResult.OK) {
        richtextbox.SelectionFont = fontDialog1.Font;
    }
}

这适用于所选字体,但标记后的字体怎么办,例如:“嗨,你在干嘛”(更改此处的字体,因此其后的所有内容都将成为新选择的字体) - rt.

0
为了使输入的以下文本具有不同的字体而不仅仅是所选文本,您需要向 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();
    }

0

难道没有SelFont、SelX可以应用于所选文本的字体属性吗?现在我想起来了,也许是SelectedX,但应用是相同的。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接