|
-
Aug 12th, 2011, 06:58 AM
#1
Thread Starter
Addicted Member
[RESOLVED] "Conditional" formatting
Hi again
In excel, Im aware of conditional formatting..
just wondering if i can code my rich text box to accept the following
If there is text between this character "[" and this character "]" color it red and make it font size 9. all other text should remain black and font size 10.
((**EXAMPLE TEXT
[Notes: Proof read this before printing]
**))
the above should then appear in the textbox as red, and size 9
thanks for your help
M
-
Aug 12th, 2011, 08:34 AM
#2
Re: "Conditional" formatting
Select the text and then set the color. Richtextbox has methods for both.
-
Aug 12th, 2011, 08:43 AM
#3
Thread Starter
Addicted Member
Re: "Conditional" formatting
Hi! Thanks for your reply.
I'm wondering if it's possible to assign code to a button which will automatically select the text I want based on conditions that i set, eg that it starts with a "[" bracket and finishes with a "]" bracket. I'm looking to assign that code to a button.
that way, it reduces the amount of formatting the user has to do when the application is running.
Thanks for your help, I appreciate it.
-
Aug 12th, 2011, 08:45 AM
#4
Re: "Conditional" formatting
 Originally Posted by MacShand
Hi! Thanks for your reply.
I'm wondering if it's possible to assign code to a button which will automatically select the text I want based on conditions that i set, eg that it starts with a "[" bracket and finishes with a "]" bracket. I'm looking to assign that code to a button.
that way, it reduces the amount of formatting the user has to do when the application is running.
Thanks for your help, I appreciate it. 
Sure, why not.
RichTextBox
-
Aug 12th, 2011, 09:03 AM
#5
Thread Starter
Addicted Member
Re: "Conditional" formatting
Great! Can you advise on how to code it?
Sorry to hassle :S...really new to programming and struggling with piecing everything together to make it work.
Cheers
-
Aug 12th, 2011, 11:15 AM
#6
Re: "Conditional" formatting
 Originally Posted by MacShand
Great! Can you advise on how to code it?
Sorry to hassle :S...really new to programming and struggling with piecing everything together to make it work.
Cheers
This should get you started:
Code:
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
RichTextBox1.Text = "123[ABC]67890" 'test data
Dim openPos As Integer = RichTextBox1.Text.IndexOf("[")
Dim closePos As Integer = RichTextBox1.Text.IndexOf("]")
RichTextBox1.Select() 'set focus
RichTextBox1.Select(openPos + 1, closePos - openPos - 1) 'select the text
RichTextBox1.SelectionColor = Color.Red 'set color
End Sub
Using the link I posted earlier you should be able to figure out what each line does.
-
Aug 12th, 2011, 12:10 PM
#7
Thread Starter
Addicted Member
Re: "Conditional" formatting
Awesome! Thank you so much for your help! That works perfectly!
Great!
-
Aug 14th, 2011, 10:28 AM
#8
Thread Starter
Addicted Member
Re: [RESOLVED] "Conditional" formatting
Hi again...is it possible to modify the code so that if there is more than one occurrence of the "[" bracket, then the formatting will apply to all occurrences in the textbox?
Thanks
-
Aug 14th, 2011, 11:25 AM
#9
Re: [RESOLVED] "Conditional" formatting
 Originally Posted by MacShand
Hi again...is it possible to modify the code so that if there is more than one occurrence of the "[" bracket, then the formatting will apply to all occurrences in the textbox?
Thanks
I'll bet that IndexOf has another parameter that is a starting location.
-
Aug 15th, 2011, 12:30 PM
#10
Thread Starter
Addicted Member
Re: [RESOLVED] "Conditional" formatting
Hi...looking up the "indexof" on microsoft, this definition:
"Reports the index of the first occurrence of the specified Unicode character in this string."
So, I need to modify it so it searches for all occurrence of the character...can you advise on how to do this?
Many thanks,
Calum
-
Aug 15th, 2011, 12:36 PM
#11
Re: [RESOLVED] "Conditional" formatting
 Originally Posted by MacShand
Hi...looking up the "indexof" on microsoft, this definition:
"Reports the index of the first occurrence of the specified Unicode character in this string."
So, I need to modify it so it searches for all occurrence of the character...can you advise on how to do this?
Many thanks,
Calum
Hmmm. Did you read this page
http://msdn.microsoft.com/en-us/libr...g.indexof.aspx ?
One of the overloads (6th one) has your answer.
-
Aug 15th, 2011, 12:51 PM
#12
Thread Starter
Addicted Member
Re: [RESOLVED] "Conditional" formatting
I can only apologise for my absolute ignorance...but I cannot string everything together without errors appearing everywhere :S Would you be able to advise even more? :S
sorry again...your help is greatly appreciated...
-
Aug 15th, 2011, 01:13 PM
#13
Re: [RESOLVED] "Conditional" formatting
 Originally Posted by MacShand
I can only apologise for my absolute ignorance...but I cannot string everything together without errors appearing everywhere :S Would you be able to advise even more? :S
sorry again...your help is greatly appreciated...
Using code tags post what you have so far.
-
Aug 15th, 2011, 01:25 PM
#14
Thread Starter
Addicted Member
Re: [RESOLVED] "Conditional" formatting
Code:
Imports System
Imports System.Windows.Forms
Imports Microsoft.VisualBasic
Public Class Dialog1
Private Sub OK_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK_Button.Click
Form1.tbUpperBody.Text = Form1.tbUpperBody.Text & "[Notes" & vbNewLine & _
& Me.tbNotes.Text & "]"
Public Function IndexOf (“[“] _
value As Char, _
startIndex As Integer, (0) _
count As Integer (100) _
) As Integer
Dim openPos As Integer = Form1.tbUpperBody.Text.IndexOf("[")
Dim closePos As Integer = Form1.tbUpperBody.Text.IndexOf("]")
Form1.tbUpperBody.Select() 'set focus
Form1.tbUpperBody.Select(openPos + 1, closePos – openPos - 1) 'select the text
Form1.tbUpperBody.SelectionColor = Color.Red 'set color
Form1.tbUpperBody.SelectionFont = New Font("Arial", 9)
End Sub
I'm clearly missing something quite substantial :S just not sure how to write everything out...
Thanks for your patience
-
Aug 15th, 2011, 03:31 PM
#15
Re: [RESOLVED] "Conditional" formatting
Something like this should get you started.
Code:
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
RichTextBox1.Text = "123[ABC]6789[0]123[ABC]67890[" 'test data
highlightRTB(RichTextBox1)
End Sub
Private Sub highlightRTB(rtb As RichTextBox)
rtb.Select() 'set focus
Dim openPos As Integer = rtb.Text.IndexOf("[")
Dim closePos As Integer = rtb.Text.IndexOf("]")
Do While openPos >= 0 AndAlso closePos >= 0 AndAlso openPos < closePos
rtb.Select(openPos + 1, closePos - openPos - 1) 'select the text
rtb.SelectionColor = Color.Red 'set color
openPos = rtb.Text.IndexOf("[", closePos + 1)
closePos = rtb.Text.IndexOf("]", closePos + 1)
Loop
End Sub
This wasn't tested much, so the cases of ] before [ may not work, as well as others.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|