3

I am creating a text editor in which when i increase the size of font by font dialog box then all font size of rich text box increase instead of only selected text.

How can I change any property of only selected text in text box?

public partial class Form1 : Form
{
    private void change()
    {
        if (click == true)
        {
            FontDialog fd = new FontDialog();

            fd.ShowColor = true;//Show color option in font dialog
            if (fd.ShowDialog() == DialogResult.OK)
            {

                //----------------------> How to affect only selected contents
                richtextbox.ForeColor = fd.Color;
                richtextbox.Font = fd.Font;

            }//end if
        }
    }//end method change
    public Form1()
    {
        InitializeComponent();
    }
    bool click = false;
    private void button1_Click(object sender, EventArgs e)
    {
        click = true;
        change();
    }
}
Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
mdadil2019
  • 807
  • 3
  • 12
  • 29
  • You can't have mixed styling in a standard textbox, you might want to look into a RichTextBox but depends on your needs. e.g. is you need to save the data, you will need to store as RTF to preserve formatting which might not work with other features in your system – musefan Mar 31 '16 at 11:21
  • there is same issue after using richtextbox. I want to increase the size of only selected text but when i increase it then the whole text of richtextbox increases in size. – mdadil2019 Mar 31 '16 at 11:37
  • Then you are probably using the wrong function... maybe you should put your code. I think it's something like `SelectionFont` you want in RichTextBox. **EDIT:** Actually not sure thats the right property, but [check this out](http://stackoverflow.com/questions/11874800/change-style-of-selected-text-in-richtextbox) – musefan Mar 31 '16 at 11:41

1 Answers1

0

Based on your code, the following should work for what you need:

richtextbox.SelectionColor = fd.Color;
richtextbox.SelectionFont = fd.Font;
musefan
  • 47,875
  • 21
  • 135
  • 185