How can I code this?

I am writing a text editor. I would like to have a combobox with all the fonts, and I would like the font to change in the rich text box to the one the user selected. How can I code this

Matt


Answer this question

How can I code this?

  • rubenswise

    What have you already tried What problems are you getting

  • phb5000

    Error 10 Operator '&' cannot be applied to operands of type 'System.Drawing.FontStyle' and 'bool' C:\Documents and Settings\HP_Owner\My Documents\mRibbon\Sample\Form1.cs 85 17 Sample

    Thats what I get. Something seems to be wrong with this line:

    if (style & FontStyle.Bold == FontStyle.Bold)

    Matt


  • SThorpe

    Try this:



    private void BoldButton_Click(object sender, EventArgs e)
    {
        FontStyle style = this.richTextBox1.Font.Style;

        if (style  & FontStyle.Bold == FontStyle.Bold)
        {
            style &= ~FontStyle.Bold;
        }
        else
        {
            style |= FontStyle.Bold;
        }

        this.richTextBox1.Font = new Font(
            this.richTextBox1.Font.Name,
            this.richTextBox1.Font.Size,
            style);
    }

     


    changing FontStyle.Bold with the style you want.

  • pilot2242

    Make three buttons and change the following code appropriately for each button

    private void BoldButton_Click(object sender, EventArgs e)
     {

         this.richTextBox1.Font = new Font(this.richTextBox1.Font.Name, this.richTextBox1.Font.Size,FontStyle.Bold);

     }

     


  • javaGurL


    Now how can I do the same with font sizes

    Matt

  • Drew P

    Something like this:



    public class MyForm : Form
    {

     private readonly int[] FontSizes = new int[] { 8, 9, 10, 11, 12, 14, 16, 18, 20, 22, 24 };
     
     public void SomeFunction()
     {

     
      for (int i = 0; i < FontSizes.Length; i++)
      {
           comboBox2.Items.Add(FontSizes[ i ]);
      }


     }
     
     private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
     {


         this.richTextBox1.Font = new Font(FontFamily.Families[comboBox1.SelectedIndex], FontSize[comboBox2.SelectedIndex]);

     }
    }

     

    Add more font sizes to the FontSizes array as you like.

    Chris


  • Janus_42

    Now how can I do the same with font sizes

    Matt

    Ali Raza Shaikh wrote:
    Add the following code to the combobox index change event.

    private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
    this.richTextBox1.Font = new Font(FontFamily.Families[this.comboBox1.SelectedIndex],this.richTextBox1.Font.Size);
    }

  • StephanLotter

    Add the following code to the combobox index change event.

    private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
    this.richTextBox1.Font = new Font(FontFamily.Families[this.comboBox1.SelectedIndex],this.richTextBox1.Font.Size);
    }

  • B-me

    Use the following code to get all the Font Names in the combo box

    for (int i = 0; i < FontFamily.Families.Length; i++)
    {
    this.comboBox1.Items.Add(FontFamily.Families[ i ].Name.ToString());
    }


  • mculo

    Got it. Now one last q: How do I code the Bold, Italic and Underlined buttons

    Matt

  • gholam ali

    Thanks but when I add bold italic underlined etc...... it only allows one style at a time. What if I want both Italic and Bold at the same time

    Matt

  • Eindhoven

    The easiest solution to this is to use the build in FontDialog that comes with VS2005.

    Drag a FontDialog from your toolbar (Under the Common Dialogs grouping) onto your form. And add an event handler to a button or menu option on your form that contains the following code...

    if (fontDialog1.ShowDialog() == DialogResult.OK)
    {
    yourRichTextBox.Font = fontPicker.Font;
    }



  • Gene B. 3

    That code you guys mentioned changes the font. Now, how can I create a combobox, populate it with font sizes and when a user clicks a font size, it changes the font size in the richtextbox

    Matt

  • Maxild

    Plz Help!

    Matt

  • How can I code this?