Changing font using a font dialog c#

17,171
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

Share:
17,171
rt.
Author by

rt.

Updated on June 04, 2022

Comments

  • rt.
    rt. almost 2 years

    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;
                }
        }}