is there a richtextbox which behaves likes textarea control in html

kanak

Member
Joined
Jan 9, 2005
Messages
17
Programming Experience
Beginner
hi all
i m tryin to make a messenger application using vb.net
the problem i m facing is ... i have two rich text boxes
one i will type the messgae and another one in which i will display the message along with the user name who send the message...
i want to make the user name bold ... so is there any rich text box in vb.net which accepts html tags
or how to do it ...
can i inherit the text area control from asp.net ... i
how to do it ... i want the window to be like the yahoo chat window ... with user who is sending the message as bold...
anyone help
 

dedassasin

New member
Joined
Feb 6, 2005
Messages
3
Programming Experience
5-10
here ya go

myrichtextbox.select(myrichtextbox.text.indexof("username"),Len("username"))
myrichtextbox.selectionfont = New Font(myrichtextbox.selectionfont, fontstyle.bold)
myrichtextbox.select(myrichtextbox.textlength,0)


that will make the username bold you can automate it too jsut by making teh username a variable and putting it in place of "username"

keep programming
 

kanak

Member
Joined
Jan 9, 2005
Messages
17
Programming Experience
Beginner
I got that before but there were some problems coming

1.when i concatenate the next message ..the whole message ios becoming bold...
2. the name will be occuring again and again ... and it will making only the first occurance bold...
3. if the same name is typed in the messgae ... itr will also become bold...

i hope u might have got the point...

there is another problem i am facing .. i think i shud put it in another thread
..its also a richtextbox problem
 

dedassasin

New member
Joined
Feb 6, 2005
Messages
3
Programming Experience
5-10
Here ya go

here are a couple functions to help you.

VB.NET:
 [size=2][color=#0000ff]
Public [/color][/size][size=2][color=#0000ff]Sub[/color][/size][size=2] AddFontStyle([/size][size=2][color=#0000ff]ByVal[/color][/size][size=2] rtb [/size][size=2][color=#0000ff]As[/color][/size][size=2] IMRichTextBox.IMRichTextBox, [/size][size=2][color=#0000ff]ByVal[/color][/size][size=2] style [/size][size=2][color=#0000ff]As[/color][/size][size=2] System.Drawing.FontStyle)
 
[/size][size=2][color=#008000]' if the selection length is > 0, work char by char.
 
[/color][/size][size=2][color=#008000]' This is necessary because the selected text may have different
 
[/color][/size][size=2][color=#008000]' styles in different substrings, and we want to preserve all
 
[/color][/size][size=2][color=#008000]' the original styles of any char, and just add the specified one
 
[/color][/size][size=2][color=#0000ff]If[/color][/size][size=2] rtb.SelectionLength > 0 [/size][size=2][color=#0000ff]Then
 
[/color][/size][size=2][color=#0000ff]Dim[/color][/size][size=2] selStart [/size][size=2][color=#0000ff]As[/color][/size][size=2][color=#0000ff]Integer[/color][/size][size=2] = rtb.SelectionStart
 
[/size][size=2][color=#0000ff]Dim[/color][/size][size=2] selLength [/size][size=2][color=#0000ff]As[/color][/size][size=2][color=#0000ff]Integer[/color][/size][size=2] = rtb.SelectionLength
 
[/size][size=2][color=#0000ff]Dim[/color][/size][size=2] currFont [/size][size=2][color=#0000ff]As[/color][/size][size=2] System.Drawing.Font
 
[/size][size=2][color=#0000ff]Dim[/color][/size][size=2] currStyle [/size][size=2][color=#0000ff]As[/color][/size][size=2] System.Drawing.FontStyle
 
[/size][size=2][color=#0000ff]Dim[/color][/size][size=2] i [/size][size=2][color=#0000ff]As[/color][/size][size=2][color=#0000ff]Integer
 
[/color][/size][size=2][color=#0000ff]For[/color][/size][size=2] i = 0 [/size][size=2][color=#0000ff]To[/color][/size][size=2] selLength - 1
 
[/size][size=2][color=#008000]' select 1 char
 
[/color][/size][size=2]rtb.Select(selStart + i, 1)
 
[/size][size=2][color=#008000]' get the font of the selected char
 
[/color][/size][size=2]currFont = rtb.SelectionFont
 
[/size][size=2][color=#008000]' get the current style of this char, and add the 
 
[/color][/size][size=2][color=#008000]' specified style
 
[/color][/size][size=2]currStyle = currFont.Style
 
currStyle = currStyle [/size][size=2][color=#0000ff]Or[/color][/size][size=2] style
 
[/size][size=2][color=#008000]' create and assign to the char a new font 
 
[/color][/size][size=2][color=#008000]' with the new style this may throw an exception, 
 
[/color][/size][size=2][color=#008000]' is the current font family does not support
 
[/color][/size][size=2][color=#008000]' the specified style, so handle this possible situation
 
[/color][/size][size=2][color=#0000ff]Try
 
[/color][/size][size=2]rtb.SelectionFont = [/size][size=2][color=#0000ff]New[/color][/size][size=2] Font(currFont.FontFamily, currFont.Size, _
 
currStyle)
 
[/size][size=2][color=#0000ff]Catch[/color][/size][size=2] ex [/size][size=2][color=#0000ff]As[/color][/size][size=2] Exception
 
[/size][size=2][color=#0000ff]End[/color][/size][size=2][color=#0000ff]Try
 
[/color][/size][size=2][color=#0000ff]Next
 
[/color][/size][size=2][color=#008000]' reselect the original selection
 
[/color][/size][size=2]rtb.Select(selStart, selLength)
 
[/size][size=2][color=#0000ff]Else
 
[/color][/size][size=2]rtb.SelectionFont = [/size][size=2][color=#0000ff]New[/color][/size][size=2] Font(rtb.SelectionFont, _
 
rtb.SelectionFont.Style [/size][size=2][color=#0000ff]Or[/color][/size][size=2] style)
 
[/size][size=2][color=#0000ff]End[/color][/size][size=2][color=#0000ff]If
 
[/color][/size][size=2][color=#0000ff]End [/color][/size][size=2][color=#0000ff]Sub
 
[/color][/size][size=2][color=#0000ff]Public [/color][/size][size=2][color=#0000ff]Sub[/color][/size][size=2] RemoveFontStyle([/size][size=2][color=#0000ff]ByVal[/color][/size][size=2] rtb [/size][size=2][color=#0000ff]As[/color][/size][size=2] IMRichTextBox.IMRichTextBox, [/size][size=2][color=#0000ff]ByVal[/color][/size][size=2] style [/size][size=2][color=#0000ff]As[/color][/size][size=2] System.Drawing.FontStyle)
 
[/size][size=2][color=#008000]' if the selection length is > 0, work char by char.
 
[/color][/size][size=2][color=#008000]' This is necessary because the selected text may have different
 
[/color][/size][size=2][color=#008000]' styles in different substrings, and we want to preserve all
 
[/color][/size][size=2][color=#008000]' the original styles of any char, except for removing
 
[/color][/size][size=2][color=#008000]' the specified one
 
[/color][/size][size=2][color=#0000ff]If[/color][/size][size=2] rtb.SelectionLength > 0 [/size][size=2][color=#0000ff]Then
 
[/color][/size][size=2][color=#0000ff]Dim[/color][/size][size=2] selStart [/size][size=2][color=#0000ff]As[/color][/size][size=2][color=#0000ff]Integer[/color][/size][size=2] = rtb.SelectionStart
 
[/size][size=2][color=#0000ff]Dim[/color][/size][size=2] selLength [/size][size=2][color=#0000ff]As[/color][/size][size=2][color=#0000ff]Integer[/color][/size][size=2] = rtb.SelectionLength
 
[/size][size=2][color=#0000ff]Dim[/color][/size][size=2] currFont [/size][size=2][color=#0000ff]As[/color][/size][size=2] System.Drawing.Font
 
[/size][size=2][color=#0000ff]Dim[/color][/size][size=2] currStyle [/size][size=2][color=#0000ff]As[/color][/size][size=2] System.Drawing.FontStyle
 
[/size][size=2][color=#0000ff]Dim[/color][/size][size=2] i [/size][size=2][color=#0000ff]As[/color][/size][size=2][color=#0000ff]Integer
 
[/color][/size][size=2][color=#0000ff]For[/color][/size][size=2] i = 0 [/size][size=2][color=#0000ff]To[/color][/size][size=2] selLength - 1
 
[/size][size=2][color=#008000]' select 1 char
 
[/color][/size][size=2]rtb.Select(selStart + i, 1)
 
[/size][size=2][color=#008000]' get the font of the selected char
 
[/color][/size][size=2]currFont = rtb.SelectionFont
 
[/size][size=2][color=#008000]' get the current style of this char, and 
 
[/color][/size][size=2][color=#008000]' remove the specified style
 
[/color][/size][size=2]currStyle = currFont.Style
 
currStyle = currStyle [/size][size=2][color=#0000ff]And[/color][/size][size=2][color=#0000ff]Not[/color][/size][size=2] style
 
[/size][size=2][color=#008000]' create and assign to the char a new font
 
[/color][/size][size=2][color=#008000]' with the new style
 
[/color][/size][size=2]rtb.SelectionFont = [/size][size=2][color=#0000ff]New[/color][/size][size=2] Font(currFont.FontFamily, currFont.Size, _
 
currStyle)
 
[/size][size=2][color=#0000ff]Next
 
[/color][/size][size=2][color=#008000]' reselect the original selection
 
[/color][/size][size=2]rtb.Select(selStart, selLength)
 
[/size][size=2][color=#0000ff]Else
 
[/color][/size][size=2]rtb.SelectionFont = [/size][size=2][color=#0000ff]New[/color][/size][size=2] Font(rtb.SelectionFont, _
 
rtb.SelectionFont.Style [/size][size=2][color=#0000ff]And[/color][/size][size=2][color=#0000ff]Not[/color][/size][size=2] style)
 
[/size][size=2][color=#0000ff]End[/color][/size][size=2][color=#0000ff]If
 
[/color][/size][size=2][color=#0000ff]End [/color][/size][size=2][color=#0000ff]Sub
 
[/color][/size]

you would use it like this

VB.NET:
' Too Add Bold
AddFontStyle(myrichtextbox, fontstyle.bold)
 
' Too Remove Bold
RemoveFontStyle(myrichtextbox, fontstyle.bold)

That will also fix the problem of adding multiple styles together and fix your bold problem so to make your username bold

VB.NET:
myrichtextbox.Select(myrichtextbox.Text.IndexOf("Username"),Len("Username"))
AddFontStyle(myrichtextbox, fontstyle.bold)
myrichtextbox.Select((myrichtextbox.SelectionStart + Len("Username")),0)
RemoveFontStyle(myrichtextbox, fontstyle.bold)

That should clear up your problem. No bugs int aht code since i used it.

Keep coding, you'll figure it out
 
Last edited:

harshshkl

New member
Joined
Jul 28, 2006
Messages
1
Programming Experience
Beginner
how can i add textarea like the one that is used for posting threads

hello plz help me to add a text area that looks similar to this one that we use to post threads and topics.
 
Top Bottom