VB.NET School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Page 1 of 1
  • You cannot start a new topic
  • Reply Reply

Creating a code parse recognition RichTextBox When Coding, it changes the whole colour Rate Topic: -----

#1 Loelin  Icon User is offline

  • New D.I.C Head
  • Pip

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 18-June 08


Dream Kudos: 0

Share |

Creating a code parse recognition RichTextBox

Posted 18 June 2008 - 05:43 PM

I am trying to create a script like program for a game and when I try to make the text color-coded(comments displayed green, and etc.) the text changed all to green.
	Private Sub rtfText_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles rtfText.TextChanged
		If rtfText.Text.IndexOf("//") Then
			rtfText.ForeColor = Color.Green
		Else
			rtfText.ForeColor = Color.Black
		End If
	End Sub

what should I do?

This post has been edited by Loelin: 18 June 2008 - 06:55 PM

Was This Post Helpful? 0
  • +
  • -


#2 Jayman  Icon User is offline

  • Student of Life
  • Icon

Reputation: 345
  • View blog
  • Posts: 9,235
  • Joined: 26-December 05


Dream Kudos: 500

Expert In: Everything

Re: Creating a code parse recognition RichTextBox

Posted 18 June 2008 - 07:34 PM

You first need to select the text in the richtextbox before you change the color. You can use the SelectedText property to select the text you want to change and then use the SelectionColor property to change it.

RichTextBox.SelectedText Property
Was This Post Helpful? 0
  • +
  • -

#3 Loelin  Icon User is offline

  • New D.I.C Head
  • Pip

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 18-June 08


Dream Kudos: 0

Re: Creating a code parse recognition RichTextBox

Posted 19 June 2008 - 07:07 AM

you don't get it, I want it colored automatically, not manually, like how vb express color-coded the code in the program :angry:
Was This Post Helpful? 0
  • +
  • -

#4 Jayman  Icon User is offline

  • Student of Life
  • Icon

Reputation: 345
  • View blog
  • Posts: 9,235
  • Joined: 26-December 05


Dream Kudos: 500

Expert In: Everything

Re: Creating a code parse recognition RichTextBox

Posted 19 June 2008 - 07:30 AM

Nothing is done automatically, the people that created the IDE had to deal with this very same issue.

You need to programmatically set the SelectedText and then change its color. They do this by looping through all the text and color coding the text.

So if you are looking for a single line of code that is going to do it, then you are out of luck. You need to create the code that will loop through all the text, seeking the text that needs to be changed.
Was This Post Helpful? 0
  • +
  • -

#5 Loelin  Icon User is offline

  • New D.I.C Head
  • Pip

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 18-June 08


Dream Kudos: 0

Re: Creating a code parse recognition RichTextBox

Posted 19 June 2008 - 07:37 AM

But how do you color it per line, i just want to know how, no matter how hard


EDIT: What about comments though, or things contained in things, like HTML

for example, this is how the game code works
//to create autolights

entfind mapmodel 23
newent lights 23 160 160 162






EDIT3: The code I made isn't working, it goes like this
If rtfText.Text <> "map" Then
			rtfText.SelectedText = "map"
			rtfText.SelectionColor = Color.Blue
		Else
			rtfText.SelectionColor = Color.Black
		End If


But what would happen is the editor first making a work then, whats ever after it turns blue

This post has been edited by Loelin: 19 June 2008 - 01:20 PM

Was This Post Helpful? 0
  • +
  • -

#6 Loelin  Icon User is offline

  • New D.I.C Head
  • Pip

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 18-June 08


Dream Kudos: 0

Re: Creating a code parse recognition RichTextBox

Posted 19 June 2008 - 06:33 PM

I have tried all of them but it just won't work, look above

This post has been edited by Loelin: 19 June 2008 - 06:33 PM

Was This Post Helpful? 0
  • +
  • -

#7 Loelin  Icon User is offline

  • New D.I.C Head
  • Pip

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 18-June 08


Dream Kudos: 0

Re: Creating a code parse recognition RichTextBox

Posted 26 June 2008 - 05:31 PM

sorry for triple posting...needed to bump...

Anyways, the Selected text doesn't help, it adds text... what else i should do?
Was This Post Helpful? 0
  • +
  • -

#11 Jayman  Icon User is offline

  • Student of Life
  • Icon

Reputation: 345
  • View blog
  • Posts: 9,235
  • Joined: 26-December 05


Dream Kudos: 500

Expert In: Everything

Re: Creating a code parse recognition RichTextBox

Posted 27 June 2008 - 08:50 AM

Allow me to provide you with a solution. Assuming you have some kind of identifier that allows you to determine which text you are going to change.

Here is one implementation. I am just loading some sample text into the control, then selecting it and changing the color. Don't forget to deselect the text before you making any other changes.
	Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
		Me.RichTextBox1.Text = "You first need to select the text in the richtextbox before you change the color. You can use the SelectedText property to select the text you want to change and then use the SelectionColor property to change it."
	End Sub

	Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

		Dim textToSelect As String = "richtextbox"
		Me.RichTextBox1.Select(Me.RichTextBox1.Text.IndexOf(textToSelect), textToSelect.Length)
		Me.RichTextBox1.SelectionColor = Color.Red
		Me.RichTextBox1.DeselectAll()

	End Sub

You can see in the screen shot that the word "richtextbox" got selected and changed to Red.

Hope this helps.

Attached thumbnail(s)

  • Attached Image

Was This Post Helpful? 1

Page 1 of 1
  • You cannot start a new topic
  • Reply Reply


Fast Reply

  

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users