Results 1 to 15 of 15

Thread: [RESOLVED] "Conditional" formatting

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Aug 2011
    Posts
    198

    Resolved [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

  2. #2
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: "Conditional" formatting

    Select the text and then set the color. Richtextbox has methods for both.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Aug 2011
    Posts
    198

    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.

  4. #4
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: "Conditional" formatting

    Quote Originally Posted by MacShand View Post
    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
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Aug 2011
    Posts
    198

    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

  6. #6
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: "Conditional" formatting

    Quote Originally Posted by MacShand View Post
    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.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Aug 2011
    Posts
    198

    Re: "Conditional" formatting

    Awesome! Thank you so much for your help! That works perfectly!

    Great!

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Aug 2011
    Posts
    198

    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

  9. #9
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: [RESOLVED] "Conditional" formatting

    Quote Originally Posted by MacShand View Post
    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.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  10. #10

    Thread Starter
    Addicted Member
    Join Date
    Aug 2011
    Posts
    198

    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

  11. #11
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: [RESOLVED] "Conditional" formatting

    Quote Originally Posted by MacShand View Post
    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.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  12. #12

    Thread Starter
    Addicted Member
    Join Date
    Aug 2011
    Posts
    198

    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...

  13. #13
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: [RESOLVED] "Conditional" formatting

    Quote Originally Posted by MacShand View Post
    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.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  14. #14

    Thread Starter
    Addicted Member
    Join Date
    Aug 2011
    Posts
    198

    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

  15. #15
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    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.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

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
  •  



Click Here to Expand Forum to Full Width