Search Tools Links Login

Great stuff for people creating text editors!

Read Below

Original Author: RyanConard


In this example, you will learn a very simple way to add a Color and Font dialog control to your word/text application. You will also learn how to add a live "Character Counter" to your application as well

Part 1: Adding Color

Ok, first lets cover the required objects. You will need a Common Dialog Control and a Rich Text Box control, which can both be found under the Components section of VB5.0 or VB6.0...

So, once you have those added, lets set the names. First, click on the Common Dialog Control you just added. Its name should be auto set to "CommonDialog1" but, were gonna rename it to "CD1" to shorten it and make it easier. Next, click on the Rich Text Box Control you just added. Its name is usually set to "RichTextBox1" but, were gonna rename it to "RTB1." Please note, that you can change these to what ever you want later, but for now just use what I gave you.

Ok, now we have our required controls and we named them to what we want. So, lets add the code! You now need to create a button. Name the button "cmdColor" and set it's caption to "Edit Back Color." Once you have that done, double click the new button so you can edit its code.

Inside the code, you should see:

Private Sub cmdColor_Click()

End Sub

Now, time for the good stuff! In between the code you just read above, put this:

Dim SelectedColor As Long

CD1.Flags = cdlCCRGBInit


SelectedColor = CD1.Color

RTB1.BackColor = SelectedColor

Thats it! The code is very basic. Basically what it does, is when you click cmdColor, the Common Dialog will open the Color Dialog Table. Once you choose a color from that table and click Ok, it will save the color you chose as our variable "SelectedColor." After it saves the color, it then calls the color into the RichTextBox and sets it as its background! Sounds complicated, but its not! And dont worry about editing the Font Color, we'll get to that in a minute!

Part 2: Editing Font

This part gets a little more tricky and has a few more lines of code, but its still very simple to understand.

Now, create a new button right next to your old one and name it "cmdFont." After, set its caption to "Edit Font." After you got that, double click the button so you can edit the code. You should now see something like this:

Private Sub cmdFont_Click()

End Sub

Now, within that code you need to put this:

Dim TextColor As Long

Dim Bold As Boolean

Dim Italic As Boolean

Dim Underline As Boolean

Dim StrikeThru As Boolean

Dim Font As String

Dim Size As Integer

CD1.Flags = cdlCFEffects Or cdlCFBoth


TextColor = CD1.Color

Bold = CD1.FontBold

Italic = CD1.FontItalic

Underline = CD1.FontUnderline

StrikeThru = CD1.FontStrikeThru

Font = CD1.FontName

Size = CD1.FontSize

RTB1.SelFontName = Font

RTB1.SelFontSize = Size

RTB1.SelColor = TextColor

If Bold = True Then

RTB1.SelBold = Bold

If Italic = True Then

RTB1.SelItalic = Italic

If Underline = True Then

RTB1.SelUnderline = Underline

If StrikeThru = True Then

RTB1.SelStrikeThru = StrikeThru

End If

End If

End If

End If

Wow, that was alot of typing for me! But, thats it! Once you have that code inside the button, you can edit every aspect of the font in a RichTextBox.

Part 3: Real-Time Character Counter

This is very cool. This will count every character you type and tell you how much you've typed.

All you need to do is create a Label. You can put it anywhere you, but name it "lblCount" and clear its caption. Once you have done that you need to double click the Rich Text Box so you can edit the code. Once you do that, you should see:

Private Sub RTB1_Change()

End Sub

Now, within that code you need to put this:

Dim Text As String

Dim Count As Integer

Text = RTB1.Text

Count = Len(Text)

lblCount.Caption = Count

Thats it! You now have a Character Counter!

Well, I gotta go! Hope you learned something from this, enjoy! Please Vote!

About this post

Posted: 2003-06-01
By: ArchiveBot
Viewed: 107 times


Visual Basic 6


No attachments for this post

Loading Comments ...


No comments have been added for this post.

You must be logged in to make a comment.