RichTextBox Control

Snippets, Projects and Assistance

Moderators: Moderator, Global Moderator

Post Reply
Josh
Hero Member
Hero Member
Posts: 4627
Joined: Fri Jun 04, 2004 2:54 pm

RichTextBox Control

Post by Josh »

I figured that if I was having as much trouble with the [color=\"blue\"]RichTextBox Control[/color], maybe someone else on the internet is.. So here is my explanation of it all since I finally get it thanks to [b][url=\"http://forums.killagraphix.com/index.php?showuser=41\"]FlyingDoggyJake[/url][/b].


<center>[color=\"Blue\"][size=7]RichTextBox Control[/size][/color]</center>

[b]What is the [color=\"Blue\"]RichTextBox Control[/color]?[/b]
[i]The [color=\"blue\"]RichTextBox control[/color] is a control which can read .rtf files and display them properly, unlike the basic TextBox control. You can also add color and images to this type of textbox also, unlike the basic TextBox control.[/i]

Now that we have that covered, let\'s jump right in. ~ First, let\'s actually get the [color=\"Blue\"]RichTextBox Control[/color] on our toolbox list of components.

[color=\"purple\"]1.) Right-click on the toolbox and select \"[b]Components[/b]\"
2.) Scroll down near the bottom/middle and find \"[b]Microsoft Rich TextBox Control 6.0 (SP4)[/b]\"
3.) Check it and then click \"[b]Apply[/b]\" and then \"[b]OK[/b]\"
4.) If everything went \'ok\' you should now have a [/color][color=\"Blue\"]RichTextBox Control[/color][color=\"Purple\"] ([img]http://img95.exs.cx/img95/6712/RTB.png[/img]) on your toolbox <img src=\'http://www.killanet.net/forum3/public/s ... /smile.gif\' class=\'bbc_emoticon\' alt=\':)\' />[/color]


Now go ahead and add the [color=\"Blue\"]RichTextBox Control[/color] to your form with a pretty good size to it and add a textbox underneath that with a command button somewhere and set the command button\'s default property to \"True\". You should end up with [i]something[/i] like this:

[img]http://img44.exs.cx/img44/3566/RTB1.png[/img]

Now lets add some code to this project. In the [b][color=\"red\"]Private Sub Command1_Click()[/color][/b] event, add this code:

<div class=\'codetop\'>Visual Basic Code</div><div class=\'codemain\'><font face=courier><font color=navy>Public Sub</font> <font color=black>Command1_Click()</color>
<font color=navy>If</font> (Len(Text1.Text) > 1) <font color=navy>Then</font>
<font color=green>\'Adding The Nickname</font>
RichTextBox1.SelStart = Len(RichTextBox1.Text)
RichTextBox1.SelBold = <font color=navy>True</font>
RichTextBox1.SelColor = QBColor(1)
RichTextBox1.SelFontName = \"Arial\"
RichTextBox1.SelFontSize = \"10\"
RichTextBox1.SelText = \"Josh: \"

<font color=green>\'Adding Text1.Text After The Nickname</font>
RichTextBox1.SelStart = Len(RichTextBox1.Text)
RichTextBox1.SelBold = <font color=navy>False</font>
RichTextBox1.SelColor = QBColor(2)
RichTextBox1.SelFontName = \"Arial\"
RichTextBox1.SelFontSize = \"10\"
RichTextBox1.SelText = Text1.Text & vbCrLf

<font color=green>\'Clear the TextBox</font>
Text1.Text = \"\"
<font color=navy>End If</font>
<font color=navy>End Sub</font></font></font></div>

Okay.. Now that we have all of this added, let me explain everything step-by-step.

[b][color=\"green\"]If (Len(Text1.Text) > 1) Then[/color][/b]
This means, \"If the length of the text in Text1 is greater than ONE, then do the following code

[b][color=\"green\"]RichTextBox1.SelStart = Len(RichTextBox1.Text)[/b][/color]
This is saying that we want to start our colored text at the end of everything that is in the [color=\"blue\"]RichTextBox Control\'s[/color] contents.. This is why we bring in [color=\"green\"]Len(RichTextBox1.Text)[/color] because it will return where to start, which is at the end of it all.

[b][color=\"green\"]RichTextBox1.SelBold = True[/b][/color]
This tells VB to make the text bold we are about to enter in the [color=\"blue\"]RichTextBox[/color]

[b][color=\"green\"]RichTextBox1.SelColor = QBColor(1)[/b][/color]
This tells VB to make the color navy blue, or QBColor(1).. (QBColor(1) = Navy Blue)

[b][color=\"green\"]RichTextBox1.SelFontName = \"Arial\"
RichTextBox1.SelFontSize = \"10\"[/b][/color]
This tells VB to make our font Arial size 10

[b][color=\"green\"]RichTextBox1.SelText = \"Josh: \"[/b][/color]
And finally, this is the text we are adding to our [color=\"blue\"]RichTextBox[/color], which is in fact \"Josh:\"

I think you can figure out the rest of the things that I\'ve included at the end..

[img]http://img78.exs.cx/img78/4075/RTB2.png[/img]

That is basically the workings of [color=\"blue\"]RichTextBox Control[/color].. Pretty simple eh? Sometimes I wonder why I was having such a hard time with it <img src=\'http://www.killanet.net/forum3/public/s ... >/blum.gif\' class=\'bbc_emoticon\' alt=\':P\' />

If you have any questions, please feel free to PM me, or leave a post here <img src=\'http://www.killanet.net/forum3/public/s ... /smile.gif\' class=\'bbc_emoticon\' alt=\':)\' /> <img src=\'http://www.killanet.net/forum3/public/s ... >/good.gif\' class=\'bbc_emoticon\' alt=\'(Y)\' />

~Josh <img src=\'http://www.killanet.net/forum3/public/s ... sp_ike.gif\' class=\'bbc_emoticon\' alt=\'(spike)\' />
Last edited by Josh on Sun Oct 17, 2004 5:52 pm, edited 1 time in total.
[align=center]

Image

“If you stop learning, you stop living.” ~Tami Quiring

“It's the rare man who understands the value of a single perfect rose.”

[/align]
Jim
Administrator
Administrator
Posts: 1324
Joined: Thu Jun 17, 2004 12:37 am

RichTextBox Control

Post by Jim »

nice tutorial Josh`. Now I can stop using the webbrowser for everything <img src=\'http://www.killanet.net/forum3/public/s ... >/blum.gif\' class=\'bbc_emoticon\' alt=\':P\' />
Love is all a matter of timing.

It's no good meeting the right person too soon or too late.

If I'd live in another time or place...

...my story might have had a very different ending.
Josh
Hero Member
Hero Member
Posts: 4627
Joined: Fri Jun 04, 2004 2:54 pm

RichTextBox Control

Post by Josh »

lol, well as I told Rift the other night, I wish I could combine the two into one control because I really wish I had the webbrowser control and not the RTB and sometimes I wish I had the RTB and not the webbrowser..
[align=center]

Image

“If you stop learning, you stop living.” ~Tami Quiring

“It's the rare man who understands the value of a single perfect rose.”

[/align]
Post Reply

Return to “Visual Basic”