13 Replies - 4354 Views - Last Post: 08 August 2012 - 05:45 PM Rate Topic: -----

#1 Tailean  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 56
  • Joined: 21-March 12

Changing color of a single line of text in a Rich Text Box.

Posted 08 August 2012 - 11:55 AM

Okay, I have been working a few hours trying to figure out how to do this. My Google search turned up with this:
richTextBox1.SelectionColor = Color.Red;

For some reason 'unknown to me' that line of code does nothing in my program. My understanding is that would change 1 line of text to the color red and leave it red. That is what I need. Perhaps I am missing something?
Here is my code:
public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            welcomeMessage();
        }

        private void welcomeMessage()
        {
            
            richTextBox1.SelectionColor = Color.Red;
            richTextBox1.Text = "                                                                                        Welcome to My Silly Little Mud!"; 
        }
        private void textBox1_KeyDown(object sender, KeyEventArgs e)
        {
           
            if (e.KeyCode == Keys.Return)
            {
                richTextBox1.AppendText(textBox1.Text);
                richTextBox1.AppendText(Environment.NewLine);
                textBox1.Clear();
            }
        }


Is This A Good Question/Topic? 0
  • +

Replies To: Changing color of a single line of text in a Rich Text Box.

#2 MrShoes  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 312
  • View blog
  • Posts: 488
  • Joined: 13-June 12

Re: Changing color of a single line of text in a Rich Text Box.

Posted 08 August 2012 - 12:12 PM

Didn't you have a similar problem before? richTextBox1.SelectionColor will only change the colour of selected text. You will need to select the text first. Use richTextBox1.SelectAll(); first to select it.
Was This Post Helpful? 1
  • +
  • -

#3 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3450
  • View blog
  • Posts: 10,646
  • Joined: 05-May 12

Re: Changing color of a single line of text in a Rich Text Box.

Posted 08 August 2012 - 12:29 PM

BTW, MrShoes' example of using SelectAll() is just to demonstrate that you need to set the selection. Obviously, you'll need something more fine grained to just select the line you are interested in, rather than selecting all the text. You'll probably use Select() method, or the Selectionstart and SelectionLength properties.
Was This Post Helpful? 0
  • +
  • -

#4 Tailean  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 56
  • Joined: 21-March 12

Re: Changing color of a single line of text in a Rich Text Box.

Posted 08 August 2012 - 01:58 PM

Okay so...... I added the code but it still isn't working ..this is what I have now:
 private void welcomeMessage()
        {
            richTextBox1.SelectAll();
            richTextBox1.SelectionColor = Color.Red;
            richTextBox1.Text = "                                                                                        Welcome to HERO'S ONLINE!"; 
        }

Was This Post Helpful? 0
  • +
  • -

#5 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2239
  • View blog
  • Posts: 9,409
  • Joined: 29-May 08

Re: Changing color of a single line of text in a Rich Text Box.

Posted 08 August 2012 - 02:16 PM

Are you a Cut & Paste programmer?

Programming isn't magical, its about customizing software plumbing components.

Do you read and research the freely available documentation? MSDN RichTextBox Class
Have you experimented with the code? Trying stuff out and see what happens.

Cos I've seen no evidence of that.

This post has been edited by AdamSpeight2008: 08 August 2012 - 02:22 PM

Was This Post Helpful? 0
  • +
  • -

#6 Tailean  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 56
  • Joined: 21-March 12

Re: Changing color of a single line of text in a Rich Text Box.

Posted 08 August 2012 - 02:26 PM

No sir..but I am new at C#..I don't know the syntax of what I need to do. I have no reason to believe that my logic is wrong..I simply don't know the Syntax. I did as suggested above..So I don't follow you.

as you can see below, I did as Mr.Shoes suggested:
You will need to select the text first. Use richTextBox1.SelectAll(); first to select it. 

Was This Post Helpful? 0
  • +
  • -

#7 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3450
  • View blog
  • Posts: 10,646
  • Joined: 05-May 12

Re: Changing color of a single line of text in a Rich Text Box.

Posted 08 August 2012 - 03:09 PM

It's very hard to select all the text when you haven't entered the text yet. Try setting text value first to enter the text. Then select all, and then finally set the color.
Was This Post Helpful? 0
  • +
  • -

#8 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2239
  • View blog
  • Posts: 9,409
  • Joined: 29-May 08

Re: Changing color of a single line of text in a Rich Text Box.

Posted 08 August 2012 - 03:33 PM

Tailean:- What about the suggestion in Skydiver's post?
Was This Post Helpful? 0
  • +
  • -

#9 Tailean  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 56
  • Joined: 21-March 12

Re: Changing color of a single line of text in a Rich Text Box.

Posted 08 August 2012 - 04:21 PM

ok thanks for the Help...that has done a GREAT deal to help my understanding. One last remaining question. After doing some reasearch as Adam suggested I noticed there is also a richTextBox1.SelectionFont I can use. However I can't figure our the syntax onb how to use it..I am trying this:
richTextBox1.SelectionFont = Size.22;


That is giving me an error....where am I going wrong?
Was This Post Helpful? 0
  • +
  • -

#10 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3450
  • View blog
  • Posts: 10,646
  • Joined: 05-May 12

Re: Changing color of a single line of text in a Rich Text Box.

Posted 08 August 2012 - 04:27 PM

The SelectionFont property take a Font object. You can read more about it here: http://msdn.microsof...x.selectionfont
Was This Post Helpful? 0
  • +
  • -

#11 Tailean  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 56
  • Joined: 21-March 12

Re: Changing color of a single line of text in a Rich Text Box.

Posted 08 August 2012 - 05:03 PM

hmm... I have been looking at that but I don't an object....objects are created with the new keyword..if I am correct and I don't see an object to use.

My bad ....overlooked it..there is a new keyword listed after all. I will take a look at this...

This post has been edited by Tailean: 08 August 2012 - 05:04 PM

Was This Post Helpful? 0
  • +
  • -

#12 Tailean  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 56
  • Joined: 21-March 12

Re: Changing color of a single line of text in a Rich Text Box.

Posted 08 August 2012 - 05:13 PM

There is no example on how to change Font size. Can I do that with selectionfont?
Was This Post Helpful? 0
  • +
  • -

#13 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3450
  • View blog
  • Posts: 10,646
  • Joined: 05-May 12

Re: Changing color of a single line of text in a Rich Text Box.

Posted 08 August 2012 - 05:22 PM

If you look at the sample in MSDN, they changed the font style. You could just as easily change the code so that it changes the font size instead of the font style.
Was This Post Helpful? 0
  • +
  • -

#14 Tailean  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 56
  • Joined: 21-March 12

Re: Changing color of a single line of text in a Rich Text Box.

Posted 08 August 2012 - 05:45 PM

okay, thanks again...I figured it out.just wasn't sure what went where in the statement.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1