简体   繁体   中英

How do I change the text in a RichTextBox size and removing the highlight on the text?

This is my code. When I open a text file it's changing the font size of the text but then coloring all the text making it highlight like I select all the text.

private void openToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            OpenFileDialog theDialog = new OpenFileDialog();
            theDialog.Title = "Open Text File";
            theDialog.Filter = "TXT files|*.txt";
            theDialog.InitialDirectory = @"C:\";
            if (theDialog.ShowDialog() == DialogResult.OK)
            {
                string filename = theDialog.FileName;
                richTextBox1.Text = File.ReadAllText(filename);
                this.richTextBox1.SelectionStart = 0;
                this.richTextBox1.SelectionLength = this.richTextBox1.Text.Length;
                this.richTextBox1.SelectionFont = new System.Drawing.Font("Maiandra GD", 30);
                string s = richTextBox1.Text;
                richTextBox1.Clear();
                richTextBox1.Text = s;
            }
        }

I tried to add this:

string s = richTextBox1.Text;
richTextBox1.Clear();
richTextBox1.Text = s;

It did the trick but the problem is that the text now back to it's original small size. I also tried before it to add this:

this.richTextBox.SelectionStart = 0;
this.richTextBox.SelectionLength = richTextBox.Text.Length;     
this.richTextBox.SelectionBackColor = Color.White;

But this didn't do it.

You are changing the Text property directly.: richTextBox1.Text = s; Never do this or richTextBox1.Text = File.ReadAllText(filename); if you don't want to mess up any formatting. See here for the rules!

Change this

        if (theDialog.ShowDialog() == DialogResult.OK)
        {
            string filename = theDialog.FileName;
            richTextBox1.Text = File.ReadAllText(filename);
            this.richTextBox1.SelectionStart = 0;
            this.richTextBox1.SelectionLength = this.richTextBox1.Text.Length;
            this.richTextBox1.SelectionFont = new System.Drawing.Font("Maiandra GD", 30);
            string s = richTextBox1.Text;
            richTextBox1.Clear();
            richTextBox1.Text = s;
        }

to this:

        if (theDialog.ShowDialog() == DialogResult.OK)
        {
            string filename = theDialog.FileName;
            string s = File.ReadAllText(filename);

            this.richTextBox1.SelectionStart = 0;  // or wherever you want to insert..
            this.richTextBox1.SelectionLength = 0;
            this.richTextBox1.SelectionFont = new System.Drawing.Font("Maiandra GD", 30);
            this.richTextBox1.SelectdText = s;
        }

I don't know about the 'highlighting' you mention but as you are inserting text outside of the selection it is probably reverting the text to the default font & attributes you had set before..

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM