【问题标题】:Changing font using a font dialog c#使用字体对话框c#更改字体
【发布时间】:2011-01-19 05:51:17
【问题描述】:

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

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

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

【问题讨论】:

    标签: c# fonts richtextbox


    【解决方案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,则可以使用&&,并且根据user210118的建议,仅此条件就可以满足else子句的使用

    【讨论】:

    • 在条件句中可能需要 && 而不是 &。
    • "&",表示如果第一个条件为假,则不需要检查第二个!
    • 不确定 else 是否是必要的,还是按照你的想法去做。
    • && 运算符 条件与运算符 (&&) 对其布尔操作数执行逻辑与,但仅在必要时评估其第二个操作数。
    • 没错,这就是你使用 && 的原因。我认为你有 & 和 && 的定义。 (msdn.microsoft.com/en-us/library/2a723cdk%28VS.71%29.aspx)
    【解决方案2】:

    难道没有 SelFont、SelX 将字体属性应用于所选文本吗?现在想来,也许是SelectedX,但应用是一样的。

    【讨论】:

      【解决方案3】:

      使用 RichTextBox 的SelectionFont 属性,该示例将按您的需要运行:

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

      【讨论】:

      • 这适用于选定的字体,但标记后的字体呢? “嘿那里”(在此处更改字体,以便后面的所有内容都是新选择的字体)
      【解决方案4】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-10-14
        • 1970-01-01
        • 2013-05-30
        • 2012-11-10
        • 1970-01-01
        相关资源
        最近更新 更多