Welcome to Dream.In.Code
Getting VB Help is Easy!

Join 135,981 VB Programmers for FREE! Get instant access to thousands of VB experts, tutorials, code snippets, and more! There are 2,284 people online right now. Registration is fast and FREE... Join Now!




Rich Text Box formatting

 
Reply to this topicStart new topic

Rich Text Box formatting, Getting similtaneous bold, italics and underline

ecokev
29 Aug, 2007 - 07:10 PM
Post #1

New D.I.C Head
*

Joined: 16 Aug, 2007
Posts: 1


My Contributions
Hi there folks,

I'm somewhat of a major beginner in Visual Basic. I'm using VB 2005 Express and am having some basic trouble with RTB formatting.

I have geared it up so that a user can format text in a Rich Text Box with either bold, italics or underline. However when one option is selected it overrides the previous command, so for instance I can't get it to have Bold and Italics at the same time.

Here is my code:

CODE
If Not RichTextBox1.SelectionFont Is Nothing Then

            Dim currentFont As System.Drawing.Font = RichTextBox1.SelectionFont

            Dim newFontStyle As System.Drawing.FontStyle

            If RichTextBox1.SelectionFont.Bold = True Then

                newFontStyle = FontStyle.Regular


            Else

                newFontStyle = FontStyle.Bold

            End If



            RichTextBox1.SelectionFont = New Font(currentFont.FontFamily, currentFont.Size, newFontStyle)

        End If


Is there any obvious problem with my coding? How can I fix the problem?

Many thanks,

Kev
User is offlineProfile CardPM
+Quote Post

Martyr2
RE: Rich Text Box Formatting
29 Aug, 2007 - 07:19 PM
Post #2

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,179



Thanked: 208 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
Well your if statement is saying if the selection font of your rich text box is bolded, set it to regular and otherwise set it to bold. To combine styles you are going to "OR" them together like so...

CODE

// Gives you regular
Dim font_style As FontStyle = FontStyle.Regular

// Gives you the regular plus the bold
font_style = font_style Or FontStyle.Bold

// Gives you regular plus bold plus italicized
font_style = font_style Or FontStyle.Italic

// Set the selection to be regular + bold + italics
RichTextBox1.SelectionFont = New Font(currentFont.FontFamily, currentFont.Size, font_style)


Lookup the Fontstyle enumeration for more information on combining font styles together.

smile.gif
User is online!Profile CardPM
+Quote Post

PsychoCoder
RE: Rich Text Box Formatting
29 Aug, 2007 - 08:22 PM
Post #3

using DIC.Core;
Group Icon

Joined: 26 Jul, 2007
Posts: 8,983



Thanked: 125 times
Dream Kudos: 8525
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net

My Contributions
You could always create your own RichTextBox control (which Ive done), but that may be a little too advanced for a beginner. Hmmmm....I think I might write a tutorial on it <crazy_thinking> I wish I would stop thinking of things while posting to someone's question</crazy_thinking> smile.gif
User is offlineProfile CardPM
+Quote Post

m2s87
RE: Rich Text Box Formatting
31 Aug, 2007 - 09:26 AM
Post #4

D.I.C Regular
Group Icon

Joined: 28 Nov, 2006
Posts: 390



Thanked: 1 times
Dream Kudos: 1225
My Contributions
Or if you don not mind reading and learning a hole lot about using controls, you could start at: http://vb-helper.com/index_controls.html#richtextbox

Writing a tutorial that would educate newbies is a good idea ^^
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/1/08 10:35AM

Live VB Help!

VB Tutorials

Reference Sheets

VB Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month