Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

i got a richtextbox were text is entered truw TCP/IP (like messenger)

when the text looks like this:

 

Ontani says:

Hi

Friend says:

Hey, How your doing

Ontani says:

good, and you...?

 

it has to color my name (ontani) like in the example

 

how can i do this?

 

this is the send code:

 

toSend= userName & " says:" & vbCrLf & txtToSend.Text
txtRecieved.Text &= toSend & vbCrLf
wsTCP.SendData(toSend)
txtToSend.Text = ""

 

someone an idea?

 

Greetz

  • Leaders
Posted
You will have to use the SelectionStart and the SelectionLength of the RichTextBox to select your name, and then use SelectedColor to change the color of your name. You will probably have to use .IndexOf to find your name for SelectionStart, or maybe .Find to find it. :)

Iceplug, USN

One of my coworkers thinks that I believe that drawing bullets is the most efficient way of drawing bullets. Whatever!!! :-(

Posted

yeah i tried

 

txtRecieved.SelectionStart = txtRecieved.Find("Ontani")

txtRecieved.SelectionLength = 7

txtRecieved.SelectionColor = Color.MarineBlye

 

that works for the first time

 

then it doesn't anymore.

 

i don't know how to make a loop to select every "Ontani" in the textbox

 

Greetz

Posted
i got a richtextbox were text is entered truw TCP/IP (like messenger)

when the text looks like this:

 

Ontani says:

Hi

Friend says:

Hey, How your doing

Ontani says:

good, and you...?

 

it has to color my name (ontani) like in the example

 

how can i do this?

 

this is the send code:

 

toSend= userName & " says:" & vbCrLf & txtToSend.Text
txtRecieved.Text &= toSend & vbCrLf
wsTCP.SendData(toSend)
txtToSend.Text = ""

 

someone an idea?

 

Greetz

 

 

Check this link, looks close to what you are looking for.

 

http://www.codeproject.com/cs/miscctrl/csexrichtextbox.asp

"Nobody knows what I do until I stop doing it."
Posted (edited)

Sorry, I am still trying to wake up. I am not a C# expert, but I know there are a few sites which might be able to turn this into VB code. There is not a lot there, let me see what I can find.

 

*correction*

 

Ok, there is a lot more int he source code that you would have to download, and not being a C# person, I am not sure I can convert it anytime soon, sorry about that.

Edited by Rick_Fla
"Nobody knows what I do until I stop doing it."
Posted

Play around with this

 

Me.rtbTest.SelectionColor = Color.Blue
Me.rtbTest.SelectedText = "Ontani "
Me.rtbTest.SelectionColor = Color.Black
Me.rtbTest.SelectedText = "Says: Cool"

"Nobody knows what I do until I stop doing it."
  • Leaders
Posted

Try using IndexOf instead of Find. :)

 

Really all you do is assign TB.Text.IndexOf("Ontani") to the selectionstart instead.

That finds the first Ontani in the textbox.

Everything else that you have after it is fine.

 

Now, to loop it, you have to use:

X = TB.Text.IndexOf("Ontani", TB.SelectionStart + 1)

'This will find the next Ontani from wherever the selection is.

 

in a loop of some sort. Preferably a Do Loop that loops until X = -1.

And of course, this check will come at the end.

 

Now, if you want some pseudocode... this is how you might set up the "IndexOf Loop".

X = indexof ontani

Do Until X = -1

selectionlength

selcolor

X = indexof (next) ontani

Loop

:)

Iceplug, USN

One of my coworkers thinks that I believe that drawing bullets is the most efficient way of drawing bullets. Whatever!!! :-(

  • Leaders
Posted

Sure, but the cursor will have to stay at the end of the textbox.

If you move the cursor to the end of the textbox, you can simply do:

 

RTB.SelectedText = "Ontani"

RTB.SelectedColor = Color.Red

 

(or something like that)

:)

Iceplug, USN

One of my coworkers thinks that I believe that drawing bullets is the most efficient way of drawing bullets. Whatever!!! :-(

Posted
Sure, but the cursor will have to stay at the end of the textbox.

If you move the cursor to the end of the textbox, you can simply do:

 

RTB.SelectedText = "Ontani"

RTB.SelectedColor = Color.Red

 

(or something like that)

:)

 

 

it was the other way around ;)

 

RTB.SelectedColor = Color.Red

RTB.SelectedText = "Ontani"

 

Thanx

Posted

Damned can't get it done!

 

I want to add the speaker to the richtextbox without any Style

 

so just plain text.

and the text to be added next can have a font/size/color.

 

something like :

 

Ontani says:

Hey

 

i used the FontDialog for choosing the font. (what works)

but the adding @ the insertion of the text to the richtextbox something goes wrong.

 

   Private Const WM_VSCROLL As Int32 = &H115
   Private Const SB_BOTTOM As Int32 = 7
   Private Declare Auto Function SendMessage Lib "user32.dll" (ByVal hwnd As IntPtr, ByVal wMsg As Int32, ByVal wParam As Int32,  _
ByVal lParam As Int32) As Int32
   Dim User as String = "Ontani"

...

       RTB.Text &= User & " says: " & vbCrLf
       SendMessage(RTB.Handle, WM_VSCROLL, SB_BOTTOM, 0)
       RTB.SelectionFont = FontDialog1.Font
       RTB.SelectedText = TextBox1.Text & vbCrLf

 

the sendmessage takes sure the carret (cursor) goes to the end of the richtextbox. (well it should)

 

the text in the textbox is added in front of everything.

Can someone help me with this problem

 

Greetz.

  • Leaders
Posted

I'm just throwing an idea out there...

 

You can retrieve and set the raw rich text from a rich text box. You could construct your own raw rich text if you learn a little about rtf format and assign it to the RichTextBox's Rtf property. For example, to insert your name into the RichTextBox you could maintain a raw rtf string that includes "{\colortbl ;\red0\green0\blue255;}" somewhere in the rtf (not sure exactly where, but this adds blue to the color table), and then insert this:

 

"\cf1\b" & Name & ": \cf0\b0" & Message

 

into the rtf in order to insert the message with the name in blue bold and message in black regular. Assign this to the richtextbox's Rtf property and... voila! Formatted rich text. Just insert your new messages in the rtf string like this, and assigning it to the richtextbox's rtf property.

[sIGPIC]e[/sIGPIC]
  • Leaders
Posted
So, RTB.SelectionStart = RTB.TextLength doesn't move the cursor to the end?

Iceplug, USN

One of my coworkers thinks that I believe that drawing bullets is the most efficient way of drawing bullets. Whatever!!! :-(

  • 4 weeks later...
Posted

I am trying to do something similar. The only difference that I can see is that I need the whole line colored. For some reason the first one gets set correctly... the remaining lines seem to be set to the right color and THEN get changed to the same color as the first line... the last line added in my example code below seems to be the correct color. I am guessing that it has something to do with the selection part but I have struggled with this for hours and can't seem to get it. Any help is appreciated.

 

  string[] strTest = new string[] {"Line one", "Line two", "Line three", "Line four" , "Line five"};
 Color[] clrTest = new Color[] {Color.Red, Color.Blue, Color.Yellow, Color.Green, Color.Purple};
 
 int i = 0;
 
 foreach (string s in strTest)
 {
  rtb1.Text += s+Environment.NewLine;
  rtb1.SelectionStart = rtb1.Text.Length - (s.Length+1);
  rtb1.SelectionLength = s.Length;
  rtb1.SelectionColor = clrTest[i];
  rtb1.SelectionStart = rtb1.Text.Length;
  i++;
 }

"Programmers are tools for converting caffeine into code."

 

Madcow Inventions -- Software for the Sanity Challenged.

Posted

Ok... after posting the above example it finally sunk in what Iceplug was saying... and the correcton Otani made. I got it to work with this code:

 

 

string[] strTest = new string[]{"Line one", "Line two", "Line three", "Line four", "Line five"};
Color[] clrTest = new Color[]{Color.Red, Color.Blue, Color.Yellow, Color.Green, Color.Purple};
int i = 0;
foreach (string s in strTest) {
rtb1.SelectionColor = clrTest(i);
rtb1.SelectedText = s + Environment.NewLine;
i++;
}[size=2][color=#006400][/color][/size]

 

 

Or for the sake of completeness here is the VB example:

 

Dim strTest As String() = New String() {"Line one", "Line two", "Line three", "Line four", "Line five"}
Dim clrTest As Color() = New Color() {Color.Red, Color.Blue, Color.Yellow, Color.Green, Color.Purple}
Dim i As Integer = 0
For Each s As String In strTest
rtb1.SelectionColor = clrTest(i)
rtb1.SelectedText = s + Environment.NewLine
i += 1
Next[size=2][color=#0000ff][/color][/size]

"Programmers are tools for converting caffeine into code."

 

Madcow Inventions -- Software for the Sanity Challenged.

  • 2 months later...
Posted

***?

 

Ok... after posting the above example it finally sunk in what Iceplug was saying... and the correcton Otani made. I got it to work with this code:

 

Or for the sake of completeness here is the VB example:

 

Dim strTest As String() = New String() {"Line one", "Line two", "Line three", "Line four", "Line five"}
Dim clrTest As Color() = New Color() {Color.Red, Color.Blue, Color.Yellow, Color.Green, Color.Purple}
Dim i As Integer = 0
For Each s As String In strTest
rtb1.SelectionColor = clrTest(i)
rtb1.SelectedText = s + Environment.NewLine
i += 1
Next[size=2][color=#0000ff][/color][/size]

 

I was after an answer for this program, and I tried ur code, and it kinda worked. it made the specified text the correct color - but when more text was added to the text bot - it reset the colored text to its original colour.

 

i have NFI why.

 

any help would be appreciated

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...