13 Replies - 11832 Views - Last Post: 08 October 2012 - 09:10 PM Rate Topic: -----

#1 The lost  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 30-March 12

How to change font color in RichTextBox

Posted 30 March 2012 - 10:45 PM

Hi all,

how change font color for some word in Richtextbox
example : when write "The lost" in Rich font color change to blue ...
and other word don't change ...
other example : same "DIM","AS" and "integer" in VB.NET ,,
how i can do it?

Other problem :

how to save the word from richtextbox to text file ?

Note : I'm using VB.NET 2008



Is This A Good Question/Topic? 0
  • +

Replies To: How to change font color in RichTextBox

#2 nK0de  Icon User is offline

  • Catch me As Exception
  • member icon

Reputation: 205
  • View blog
  • Posts: 823
  • Joined: 21-December 11

Re: How to change font color in RichTextBox

Posted 30 March 2012 - 10:50 PM

I suppose you're talking about something like syntax highlighting, right? Check out this tutorial. Its written in C# but you can get an idea and its not that difficult to convert C# code to VB.NET if you want to.
Was This Post Helpful? 0
  • +
  • -

#3 The lost  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 30-March 12

Re: How to change font color in RichTextBox

Posted 30 March 2012 - 11:08 PM

yep ,
but
how I can change it to vb.net ?

thanks nKOde
Was This Post Helpful? 0
  • +
  • -

#4 nK0de  Icon User is offline

  • Catch me As Exception
  • member icon

Reputation: 205
  • View blog
  • Posts: 823
  • Joined: 21-December 11

Re: How to change font color in RichTextBox

Posted 30 March 2012 - 11:22 PM

It has like 30 lines of code or less. Hardly anything to convert. The only parts that differ from VB.NET code are,

Declaring the RegEx pattern Public keyWords As New Regex(<regex pattern>)

Declaring variables. Dim selPos As Integer

If it seems too much of a work, you can just use a converter like this and convert the code right away. Which is no fun and offers you nothing to learn.
Was This Post Helpful? 0
  • +
  • -

#5 The lost  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 30-March 12

Re: How to change font color in RichTextBox

Posted 30 March 2012 - 11:45 PM

can u convert the code to vb.net ,bcz I didn't do it :(

plz help me
Was This Post Helpful? 0
  • +
  • -

#6 nK0de  Icon User is offline

  • Catch me As Exception
  • member icon

Reputation: 205
  • View blog
  • Posts: 823
  • Joined: 21-December 11

Re: How to change font color in RichTextBox

Posted 31 March 2012 - 12:27 AM

I'm not gonna do that. Its against the site's policies. If you read the FAQ, you'll see its our #1 rule.

I mean c'mon, I just gave you a link to a converter. What's so hard for you to just copy the code from that tutorial and paste it in the converter?
Was This Post Helpful? 0
  • +
  • -

#7 nK0de  Icon User is offline

  • Catch me As Exception
  • member icon

Reputation: 205
  • View blog
  • Posts: 823
  • Joined: 21-December 11

Re: How to change font color in RichTextBox

Posted 31 March 2012 - 12:35 AM

oh I forgot to mention earlier. You need to convert the code to VB.NET and put the VB.NET keywords for the RegEx pattern.
Was This Post Helpful? 0
  • +
  • -

#8 The lost  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 30-March 12

Re: How to change font color in RichTextBox

Posted 31 March 2012 - 12:58 AM

Imports System.IO
Imports System.Text.RegularExpressions '{<regex pattern>}



Public Class Form1
    Dim keyWordMatch As New Object
    Public keyWords As Regex = New Regex("abstract|as|base|bool|break|byte|case|catch|char|checked|class|const|continue|decimal|default|delegate|do|double|else|enum|event|explicit|extern|false|finally|fixed|float|for|foreach|goto|if|implicit|in|int|interface|internal|is|lock|long|namespace|new|null|object|operator|out|override|params|private|protected|public|readonly|ref|return|sbyte|sealed|short|sizeof|stackalloc|static| string|struct|switch|this|throw|true|try|typeof|uint|ulong|unchecked|unsafe|ushort|using|virtual|volatile|void|while|") '(Regex pattern)




    'public Regex keyWords = new Regex"abstract|as|base|bool|break|byte|case|catch|char|checked|class|const|continue|decimal|default|delegate|do|double|else|enum|event|explicit|extern|false|finally|fixed|float|for|" +
    '"foreach|goto|if|implicit|in|int|interface|internal|is|lock|long|namespace|new|null|object|operator|out|override|params|private|protected|public|readonly|ref|return|sbyte|sealed|short|sizeof|stackalloc|static|" + "string|struct|switch|this|throw|true|try|typeof|uint|ulong|unchecked|unsafe|ushort|using|virtual|volatile|void|while|");


  

    Private Sub RichTextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RichTextBox1.TextChanged

1:      '	private void richTextBox1_TextChanged(object sender, EventArgs e)
2:      '{
3:      '    //Highlight every found word from keyWords.
4:
5:      '  //Get the last cursor position in the richTextBox1.
6:
7:      Dim selPos As Integer = RichTextBox1.Selectionstart
8:
9:      '   //For each match from the regex, highlight the word.
10:     'for each (Match keyWordMatch in keyWords.Matches(richTextBox1.Text))
11:     '   {
        For Each Match In keyWords.Matches(RichTextBox1.Text)
            '   Dim keyWordMatch As New Object
12:
[color="#0000FF"]13:         RichTextBox1.Select(keyWordMatch.Index, keyWordMatch.Length)[/color]
14:         RichTextBox1.SelectionColor = Color.Blue
15:         RichTextBox1.Selectionstart = selPos
16:         RichTextBox1.SelectionColor = Color.Black
17:     Next
18:



    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click

10:     'Write data to Temp TXT file
        Dim myWriter As StreamWriter
        Dim myStream As FileStream

12:     myStream = New FileStream(("Token.txt"), FileMode.Create)
13:     myWriter = New StreamWriter(myStream)
14:
15:     Dim myItem As Object
        Dim space As String() = Split(New Char() {" "})
        ' For Each space In RichTextBox1.Text

        '    Next
16:     For Each myItem In RichTextBox1.Text
17:         myWriter.Write(myItem.ToString & Environment.NewLine)
            '  space.Split(New Char() {" "})

            ' space.Write(Environment.NewLine)
18:     Next
19:     myWriter.Close()
        MsgBox("Done")
    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        '   private void timer1_Tick(object sender, EventArgs e)
2:      '  {
3:      '   //Highlight every found word from keyWords.
4:
5:      '   //Get the last cursor position in the richTextBox1.
6:
7:      Dim selPos As Integer = RichTextBox1.Selectionstart
8:
9:      '  //For each match from the regex, highlight the word.
10:     '   for each (Match keyWordMatch in keyWords.Matches(richTextBox1.Text))
11:     '{
        For Each Match In keyWords.Matches(RichTextBox1.Text)
12:         '    Dim keyWordMatch As New Object
[color="#0000FF"]13:         RichTextBox1.Select(keyWordMatch.Index, keyWordMatch.Length)[/color]
14:         RichTextBox1.SelectionColor = Color.Blue
15:         RichTextBox1.Selectionstart = selPos
16:         RichTextBox1.SelectionColor = Color.Black
17:         ' }
        Next

18:     '}
    End Sub
    
End Class




This is my code after convert it
No syntax error , but
when i click run show this message "Public member 'Index' on type 'Object' not found." on line 13

sorry , this is my code

01	Imports System.IO
02	Imports System.Text.RegularExp<b></b>ressions '{<regex pattern>}
03	 
04	 
05	 
06	Public Class Form1
07	    Dim keyWordMatch As New Object
08	    Public keyWords As Regex = New Regex("abstract|as|base|bool|break|byte|case|catch|char|checked|class|const|continue|decimal|default|delegate|do|double|else|enum|event|explicit|extern|false|finally|fixed|float|for|foreach|goto|if|implicit|in|int|interface|internal|is|lock|long|namespace|new|null|object|operator|out|override|params|private|protected|public|readonly|ref|return|sbyte|sealed|short|sizeof|stackalloc|static| string|struct|switch|this|throw|true|try|typeof|uint|ulong|unchecked|unsafe|ushort|using|virtual|volatile|void|while|") '(Regex pattern)
09	 
10	 
11	 
12	 
13	    'public Regex keyWords = new Regex"abstract|as|base|bool|break|byte|case|catch|char|checked|class|const|continue|decimal|default|delegate|do|double|else|enum|event|explicit|extern|false|finally|fixed|float|for|" +
14	    '"foreach|goto|if|implicit|in|int|interface|internal|is|lock|long|namespace|new|null|object|operator|out|override|params|private|protected|public|readonly|ref|return|sbyte|sealed|short|sizeof|stackalloc|static|" + "string|struct|switch|this|throw|true|try|typeof|uint|ulong|unchecked|unsafe|ushort|using|virtual|volatile|void|while|");
15	 
16	 
17	   
18	 
19	    Private Sub RichTextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RichTextBox1.TextChanged
20	 
21	1:      '   private void richTextBox1_TextChanged(object sender, EventArgs e)
22	2:      '{
23	3:      '    //Highlight every found word from keyWords.
24	4:
25	5:      '  //Get the last cursor position in the richTextBox1.
26	6:
27	7:      Dim selPos As Integer = RichTextBox1.Selectionstart
28	8:
29	9:      '   //For each match from the regex, highlight the word.
30	10:     'for each (Match keyWordMatch in keyWords.Matches(richTextBox1.Text))
31	11:     '   {
32	        For Each Match In keyWords.Matches(RichTextBox1.Text)
33	            '   Dim keyWordMatch As New Object
34	12:
35	13:         RichTextBox1.Select(keyWordMatch.Index, keyWordMatch.Length)
36	14:         RichTextBox1.SelectionColor = Color.Blue
37	15:         RichTextBox1.Selectionstart = selPos
38	16:         RichTextBox1.SelectionColor = Color.Black
39	17:     Next
40	18:
41	 
42	 
43	 
44	    End Sub
45	 
46	    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
47	 
48	10:     'Write data to Temp TXT file
49	        Dim myWriter As StreamWriter
50	        Dim myStream As FileStream
51	 
52	12:     myStream = New FileStream(("Token.txt"), FileMode.Create)
53	13:     myWriter = New StreamWriter(myStream)
54	14:
55	15:     Dim myItem As Object
56	        Dim space As String() = Split(New Char() {" "})
57	        ' For Each space In RichTextBox1.Text
58	 
59	        '    Next
60	16:     For Each myItem In RichTextBox1.Text
61	17:         myWriter.Write(myItem.ToString & Environment.NewLine)
62	            '  space.Split(New Char() {" "})
63	 
64	            ' space.Write(Environment.NewLine)
65	18:     Next
66	19:     myWriter.Close()
67	        MsgBox("Done")
68	    End Sub
69	 
70	    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
71	        '   private void timer1_Tick(object sender, EventArgs e)
72	2:      '  {
73	3:      '   //Highlight every found word from keyWords.
74	4:
75	5:      '   //Get the last cursor position in the richTextBox1.
76	6:
77	7:      Dim selPos As Integer = RichTextBox1.Selectionstart
78	8:
79	9:      '  //For each match from the regex, highlight the word.
80	10:     '   for each (Match keyWordMatch in keyWords.Matches(richTextBox1.Text))
81	11:     '{
82	        For Each Match In keyWords.Matches(RichTextBox1.Text)
83	12:         '    Dim keyWordMatch As New Object
84	13:         RichTextBox1.Select(keyWordMatch.Index, keyWordMatch.Length)
85	14:         RichTextBox1.SelectionColor = Color.Blue
86	15:         RichTextBox1.Selectionstart = selPos
87	16:         RichTextBox1.SelectionColor = Color.Black
88	17:         ' }
89	        Next
90	 
91	18:     '}
92	    End Sub
93	     
94	End Class

Was This Post Helpful? 0
  • +
  • -

#9 nK0de  Icon User is offline

  • Catch me As Exception
  • member icon

Reputation: 205
  • View blog
  • Posts: 823
  • Joined: 21-December 11

Re: How to change font color in RichTextBox

Posted 31 March 2012 - 01:17 AM

In that tutorial, there's complete version of the application attached at the end. Download it and have a look at the code. You don't need to add anything more than that.
And make sure you've added a timer to your application and configured its properties as mentioned in the tutorial.
btw just outta curiosity, what are all those numbers on the left side in the code you've posted. Remove all that.

And I say again, if you want this application to highlight only VB.NET code, you need to put VB.NET keywords for the RegEx pattern.
Was This Post Helpful? 0
  • +
  • -

#10 The lost  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 30-March 12

Re: How to change font color in RichTextBox

Posted 31 March 2012 - 02:11 AM

OK sir, I will do that ,

now ..

When I write in Richtextbox , I want save that onto text file

example :

Thank you sir for your time <<<< this is the statement in Richtextbox

i want saved it in txt file

Thank
you
sir
for
your
time


How I do that ?

:)
Was This Post Helpful? 0
  • +
  • -

#11 nK0de  Icon User is offline

  • Catch me As Exception
  • member icon

Reputation: 205
  • View blog
  • Posts: 823
  • Joined: 21-December 11

Re: How to change font color in RichTextBox

Posted 31 March 2012 - 02:56 AM

You need to use the Split function in Vb.NET to break down a string. Take a look at the first example here. It shows how to do that and explains better than I can.

To write data to a text file, you need to use the StreamWriter. If you're not familiar with it, here's a tutorial.

Go through them and come up with something and we'll go from there.
Was This Post Helpful? 1
  • +
  • -

#12 The lost  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 30-March 12

Re: How to change font color in RichTextBox

Posted 31 March 2012 - 01:37 PM

nKOde

Thank u so much (F)
Was This Post Helpful? 0
  • +
  • -

#13 flamerazer  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 08-October 12

Re: How to change font color in RichTextBox

Posted 08 October 2012 - 08:51 PM

View PostThe lost, on 31 March 2012 - 02:11 AM, said:

OK sir, I will do that ,

now ..

When I write in Richtextbox , I want save that onto text file

example :

Thank you sir for your time <<<< this is the statement in Richtextbox

i want saved it in txt file

Thank
you
sir
for
your
time


How I do that ?

:)

Was This Post Helpful? 0
  • +
  • -

#14 flamerazer  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 08-October 12

Re: How to change font color in RichTextBox

Posted 08 October 2012 - 09:10 PM

you can RichTextBox1.Text = "Thank" & vbnewline & "you" & vbnewline & "sir" & vbnewline & "for" & vbnewline & "your" & vbnewline & "time"
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1