14 Replies - 1399 Views - Last Post: 13 February 2011 - 06:21 AM Rate Topic: -----

#1 FahedDotnet   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 58
  • Joined: 25-February 10

Coloring Codes In vb.net

Posted 11 February 2011 - 10:48 AM

hi everyone

i am searching for code that color the codes in richtextbox in vb.net


i tried that code

        Dim startingPoint As Integer = -1
        Dim startingPoint1 As Integer = -1
        Do
            startingPoint = RichTextBox1.Find("dim", startingPoint + 1, RichTextBoxFinds.WholeWord)
            startingPoint1 = RichTextBox1.Find("as", startingPoint + 1, RichTextBoxFinds.None)

            If (startingPoint >= 0) Then
                RichTextBox1.Selectionstart = startingPoint
                RichTextBox1.SelectionLength = "dim".Length
                RichTextBox1.SelectionColor = Color.Blue
            End If

            If (startingPoint1 >= 0) Then
                RichTextBox1.Selectionstart = startingPoint1
                RichTextBox1.SelectionLength = 11
                RichTextBox1.SelectionColor = Color.Blue
            End If

        Loop Until startingPoint < 0


it works very good but i wont to color the text that inside the " " to
brown color so i need code to color text in richtextbox

Is This A Good Question/Topic? 0
  • +

Replies To: Coloring Codes In vb.net

#2 FahedDotnet   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 58
  • Joined: 25-February 10

Re: Coloring Codes In vb.net

Posted 11 February 2011 - 11:43 AM

I NEED HELP
Was This Post Helpful? 0
  • +
  • -

#3 Shadar   User is offline

  • D.I.C Head
  • member icon

Reputation: 46
  • View blog
  • Posts: 157
  • Joined: 26-May 09

Re: Coloring Codes In vb.net

Posted 11 February 2011 - 12:20 PM

To do this you have to find the " individually. Find the first one...that is your starting point. Then find the second one.... position of second minus the position of the first one is the length of what you want to select. Then just change the color.

        Dim intStart As Integer
        Dim intLength As Integer
        Dim intEnd As Integer
        intStart = RichTextBox1.Find(Chr(34))
        intEnd = RichTextBox1.Find(Chr(34), intStart + 1)
        intLength = intEnd - intStart
        RichTextBox1.Select(intStart, intLength)
        RichTextBox1.SelectionColor = Color.Brown


This post has been edited by Shadar: 11 February 2011 - 12:21 PM

Was This Post Helpful? 1
  • +
  • -

#4 FahedDotnet   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 58
  • Joined: 25-February 10

Re: Coloring Codes In vb.net

Posted 11 February 2011 - 12:23 PM

THANK U SOO MUCH

BUT CAN U GIVE ME EASIER CODE FOR WORDS DIM AS STRING
Was This Post Helpful? 0
  • +
  • -

#5 Shadar   User is offline

  • D.I.C Head
  • member icon

Reputation: 46
  • View blog
  • Posts: 157
  • Joined: 26-May 09

Re: Coloring Codes In vb.net

Posted 11 February 2011 - 12:30 PM

View PostFahedDotnet, on 11 February 2011 - 01:23 PM, said:

BUT CAN U GIVE ME EASIER CODE FOR WORDS DIM AS STRING


I'm not sure what you mean.

DIM AS STRING

is a declaration of a variable as a string type. I don't know of an easier way to do it.
Was This Post Helpful? 0
  • +
  • -

#6 FahedDotnet   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 58
  • Joined: 25-February 10

Re: Coloring Codes In vb.net

Posted 11 February 2011 - 12:37 PM

I Mean to color it in EASIER code coloring words
Was This Post Helpful? 0
  • +
  • -

#7 Shadar   User is offline

  • D.I.C Head
  • member icon

Reputation: 46
  • View blog
  • Posts: 157
  • Joined: 26-May 09

Re: Coloring Codes In vb.net

Posted 11 February 2011 - 12:54 PM

I don't know of an easier way to do it but maybe I can explain this better:

Dim intStart As Integer                           'Declare variable to find position of first "
Dim intLength As Integer                          'Declare variable to hold length of Selection
Dim intEnd As Integer                             'Declare variable to find position of second "
intStart = RichTextBox1.Find(Chr(34))             'Assign start position of first "
intEnd = RichTextBox1.Find(Chr(34), intStart + 1) 'Assign start position of second "
intLength = intEnd - intStart                     'calculate length of selection
RichTextBox1.Select(intStart, intLength)          'select text
RichTextBox1.SelectionColor = Color.Brown         'change color to brwon


Was This Post Helpful? 0
  • +
  • -

#8 AdamSpeight2008   User is offline

  • MrCupOfT
  • member icon

Reputation: 2298
  • View blog
  • Posts: 9,535
  • Joined: 29-May 08

Re: Coloring Codes In vb.net

Posted 11 February 2011 - 12:55 PM

The OP (FahedDotnet) wants to create a syntax highlighter I think.
To make a really good you to parse the text, against the grammar of the language you are highlighting.

This post has been edited by AdamSpeight2008: 11 February 2011 - 12:58 PM

Was This Post Helpful? 0
  • +
  • -

#9 FahedDotnet   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 58
  • Joined: 25-February 10

Re: Coloring Codes In vb.net

Posted 11 February 2011 - 01:00 PM

View PostAdamSpeight2008, on 11 February 2011 - 12:55 PM, said:

The OP (FahedDotnet) wants to create a syntax highlighter I think.
To make a really good you to parse the text, against the grammar of the language you are highlighting.


hi , no i don't wont to create syntax highlighter i am making a code maker

what op means??
Was This Post Helpful? 0
  • +
  • -

#10 Shadar   User is offline

  • D.I.C Head
  • member icon

Reputation: 46
  • View blog
  • Posts: 157
  • Joined: 26-May 09

Re: Coloring Codes In vb.net

Posted 11 February 2011 - 01:02 PM

View PostAdamSpeight2008, on 11 February 2011 - 01:55 PM, said:

The OP (FahedDotnet) wants to create a syntax highlighter I think.
To make a really good you to parse the text, against the grammar of the language you are highlighting.


If that's what he's looking for, it sounds like he needs a highlighting class based on the grammer. I'm happy to help but not about to write an entire class for it.
Was This Post Helpful? 0
  • +
  • -

#11 Shadar   User is offline

  • D.I.C Head
  • member icon

Reputation: 46
  • View blog
  • Posts: 157
  • Joined: 26-May 09

Re: Coloring Codes In vb.net

Posted 11 February 2011 - 01:44 PM

Fahed,

This is what you were looking for:
I thought you wanted to highlight text in between two quotation marks. You just wanted the word DIM AND the quotation marks around it. My mistake. I was making it too complicated.

Hope this helps.
    Dim intStart As Integer
        Dim intLength As Integer
        intStart = RichTextBox1.Find("DIM") - 1
        intLength = "DIM".Length + 2
        RichTextBox1.Select(intStart, intLength)
        RichTextBox1.SelectionColor = Color.Blue



View PostFahedDotnet, on 11 February 2011 - 02:00 PM, said:

View PostAdamSpeight2008, on 11 February 2011 - 12:55 PM, said:

The OP (FahedDotnet) wants to create a syntax highlighter I think.
To make a really good you to parse the text, against the grammar of the language you are highlighting.

what op means??

OP = Original Post

This post has been edited by Shadar: 11 February 2011 - 01:54 PM

Was This Post Helpful? 0
  • +
  • -

#12 AdamSpeight2008   User is offline

  • MrCupOfT
  • member icon

Reputation: 2298
  • View blog
  • Posts: 9,535
  • Joined: 29-May 08

Re: Coloring Codes In vb.net

Posted 11 February 2011 - 02:00 PM

Original Poster
Was This Post Helpful? 0
  • +
  • -

#13 FahedDotnet   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 58
  • Joined: 25-February 10

Re: Coloring Codes In vb.net

Posted 12 February 2011 - 01:45 PM

Hi Everyone
i have a problame whan i search for a word and i wont to color it it colored when i press the button on the first time but in the seconde time all richtextbox fore color change to the previous color
Was This Post Helpful? 0
  • +
  • -

#14 FahedDotnet   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 58
  • Joined: 25-February 10

Re: Coloring Codes In vb.net

Posted 13 February 2011 - 02:36 AM

i meant in the first click the word dim color is blue

Dim msg As String = ""

msg &= "bank for programes" & ControlChars.CrLf
msg &= "its the bank" & ControlChars.CrLf

MsgBox(msg,524288 or 4 or 64,"bankking")

in the second time

Dim msg As String = ""

msg &= "bank for programes" & ControlChars.CrLf
msg &= "its the bank" & ControlChars.CrLf

MsgBox(msg,524288 or 4 or 64,"bankking")


that what happens with me
Was This Post Helpful? 0
  • +
  • -

#15 Luc001   User is offline

  • D.I.C Addict

Reputation: 85
  • View blog
  • Posts: 617
  • Joined: 04-May 09

Re: Coloring Codes In vb.net

Posted 13 February 2011 - 06:21 AM

Hi,

You can try this:

Private Sub RichTextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RichTextBox1.TextChanged
        Dim cl As Long
        cl = RichTextBox1.Selectionstart
        Dim StartPos As Long
        Dim EndPos As Long
        StartPos = 1 : EndPos = 1
        RichTextBox1.Selectionstart = 1
        RichTextBox1.SelectionLength = Len(RichTextBox1.Text)
        RichTextBox1.SelectionColor = Color.Black
        Me.Text = RichTextBox1.TextLength
        'RichTextBox1.Text
        Do

            StartPos = InStr(EndPos, RichTextBox1.Text, "Dim", CompareMethod.Binary)
            If StartPos = 0 Then Exit Do
            EndPos = InStr(StartPos, RichTextBox1.Text, "m", CompareMethod.Binary)
            If EndPos = 0 Then Exit Do

            EndPos = EndPos + 1
            RichTextBox1.Selectionstart = StartPos - 1
            RichTextBox1.SelectionLength = EndPos - StartPos
            RichTextBox1.SelectionColor = Color.Blue


        Loop
        StartPos = 1 : EndPos = 1

        Do

            StartPos = InStr(EndPos, RichTextBox1.Text, "Dimm", CompareMethod.Binary)
            If StartPos = 0 Then Exit Do
            EndPos = InStr(StartPos, RichTextBox1.Text, "m", CompareMethod.Binary)
            If EndPos = 0 Then Exit Do

            EndPos = EndPos + 1
            RichTextBox1.Selectionstart = StartPos - 1
            RichTextBox1.SelectionLength = EndPos - StartPos
            RichTextBox1.SelectionColor = Color.Black


        Loop


        RichTextBox1.SelectionLength = 0
        RichTextBox1.Selectionstart = cl
        RichTextBox1.SelectionColor = Color.Black

    End Sub


I needed to change my code, because when you added Dimm then a part of the word was colored.

This post has been edited by Luc001: 13 February 2011 - 07:25 AM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1