RichTextBox format line by button click vb.net 2010

Hi Experts,

I'm trying writing to write a winform app with a RTB and button. The button follows the cursor/caret up or down depending on carriage return (which works great thanks to the mighty kaufmed from previous question... Previous Question

The purpose of the button is to format whatever text is on same line as button.
for example: if i want all the text on line 5 to change color to GREEN, then pressing this button will do so. and not continue on another line.

I found the code beow (from a google search) and inserted into my button click, which works great if I don't add Label1.Text in line 2 / 4... works with array (actual number).

1    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
2    RichTextBox1.SelectionStart = RichTextBox1.Text.IndexOf(RichTextBox1.Lines(Label1.Text))  'Sets the selection 3    length as the length of the line 3. At this point, we have now set the start and end positions of the selection.
4    RichTextBox1.SelectionLength = RichTextBox1.Lines(Label1.Text).Length
5    RichTextBox1.SelectionColor = Color.Green   'Sets the color of the selected text to Green.
6
7    End Sub

Open in new window


The ERROR I'm getting is
Message=Index was outside the bounds of the array.

because Label1 shows line numbers... I thought it would be easy to pass it through a label.
RobertoFreemanoAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Fernando SotoRetiredCommented:
Hi Roberto;

You should have gotten an error pointing to this part of the following line of code, RichTextBox1.Lines(Label1.Text), because the lines property is an array of String's and you are indexing that array with a string value from Label1.Text. You need an Integer value to do that indexing.

RichTextBox1.SelectionStart = RichTextBox1.Text.IndexOf(RichTextBox1.Lines(Label1.Text))
0
RobertoFreemanoAuthor Commented:
Hi Fernando,

I understand that you're saying... it's just connecting the dots now. I 'm trying to draw from lineCount, but it's encased in RichTextBox1_TextChanged....

RichTextBox1.SelectionStart = RichTextBox1.Text.IndexOf(RichTextBox1.Lines(lineCount)) ' would be better, but it doesn't work.
0
Fernando SotoRetiredCommented:
Try it like this Roberto and see if it gives you what you are looking for.

RichTextBox1.SelectionStart = RichTextBox1.Text.IndexOf(RichTextBox1.Lines.Count) 

Open in new window

0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

RobertoFreemanoAuthor Commented:
Thanks Fernando,

I get the following...
Message=
InvalidArgument=Value of '-1' is not valid for 'SelectionStart'.
0
Fernando SotoRetiredCommented:
With is part of the statement, RichTextBox1.SelectionStart, you should be trying to set the start of the Selected text in the RichTextBox. Looking at the right side of the equals sign of the statement, RichTextBox1.Text.IndexOf(RichTextBox1.Lines.Count), would suggest that you are trying to set the start character position from the beginning to the value of the number of lines in the text box which I do not think you what.

What do you wish to do?
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
RobertoFreemanoAuthor Commented:
ah yes, I think you've "Hit the nail on the head"... You're right... I guess what i want is to set the format anywhere along the line (instead of at the font).
I've been trying to get the BUTTON to highlight the line before actioning it.
Since your last comment, I've been experimenting with ysendkeys(end)

RichTextBox1.Focus()
        RichTextBox1.Focus()
        SendKeys.Send("{HOME}+{END}")
        RichTextBox1.SelectionAlignment = HorizontalAlignment.Center
        RichTextBox1.AppendText(Environment.NewLine)
        RichTextBox1.ScrollToCaret()
        RichTextBox1.SelectionAlignment = HorizontalAlignment.Left

Open in new window


This seems to work for me ;)
0
RobertoFreemanoAuthor Commented:
Thanks Fernando! Super Job ;)
0
Fernando SotoRetiredCommented:
Not a problem Roberto, glad I was able to help.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.