views:

611

answers:

3

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

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.

brian
A: 

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;
    }
}
Alex LE
This works for the selected font but what about the font after the marker e.g."Hey There" (change the font here so everything followin this will be the new chosen font)
rt
A: 
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

Asad Butt
brian
This is what I was looking for thank you
rt
Asad Butt
Not sure if the else is necessary, or going to do what you think it is.
brian
brian
if(fontDialog1.ShowDialog() == DialogResult.OK) == false, why check !String.IsNullOrEmpty(richtextbox.Text)
Asad Butt
brian
Asad Butt