Connecting Tech Pros Worldwide Forums | Help | Site Map

Rich Text Box

gsgurdeep's Avatar
Member
 
Join Date: Apr 2007
Location: Delhi (India )
Posts: 94
#1: Dec 23 '08
I am using rich text box control in my vb.net windows application to format some user define text formatting.

I placed 3 buttons for bold, italic, & underline to change text formatting.

These buttons perform very well separately. But when want to use more than one option at same text, latest formatting option overwrites previous formatting.

Give Me Any Suggestion Please Help Me.

Member
 
Join Date: Jul 2007
Posts: 87
#2: Dec 23 '08

re: Rich Text Box


Could you use a global bool value to show if the button is clicked, Then do some checks so
after you have defined these you would set these true and false in the button click events
dim b1 as bool = false // bold
dim b2 as bool = false etc etc /italics


private sub checkButtonsClicked
if b1 = true and b2 = true then
//your text or text box properties italics bold set to true
end sub

you would have to cover all the combinations but that the quickest idea that came to me. Sorry i'm not amazing at programming
Member
 
Join Date: Jul 2007
Posts: 87
#3: Dec 23 '08

re: Rich Text Box


very simple way
set some global bool variables
on buttonclick set these to either false or true
do some checks so if b1 = true and b2 = true then set properties for bold and italics
as i said very simple but possibly not the best way
Expert
 
Join Date: Aug 2007
Location: UK
Posts: 153
#4: Dec 24 '08

re: Rich Text Box


A flexible way of this is to use the FontStyle as a flag i.e. if the font should be bold and underlined the style is FontStyle.Bold And FontStyle.Underline.

A very thrown together implementation would be:

Expand|Select|Wrap|Line Numbers
  1. Private Sub AddStyle(ByVal style As FontStyle)
  2.     Dim newStyle As FontStyle = testRich.SelectionFont.Style Or style
  3.     Dim ifont As New Font(testRich.Font, newStyle)
  4.     testRich.SelectionFont = ifont
  5.   End Sub
  6.  
  7.   Private Sub RemoveStyle(ByVal style As FontStyle)
  8.     Dim newStyle As FontStyle = testRich.SelectionFont.Style Xor style
  9.     Dim ifont As New Font(testRich.Font, newStyle)
  10.     testRich.SelectionFont = ifont
  11.   End Sub
  12.  
  13.   Private Function HasStyle(ByVal style As FontStyle) As Boolean
  14.     Return (testRich.SelectionFont.Style And style) = style
  15.   End Function
This assumes that the RichTextBox control is called testRich. Calling these procedures is done by:
Expand|Select|Wrap|Line Numbers
  1. ' HasStyle(FontStyle.Bold) = False
  2. AddStyle(FontStyle.Bold)
  3.  
  4. ' HasStyle(FontStyle.Bold) = True
  5. RemoveStyle(FontStyle.Bold)
  6.  
  7. ' HasStyle(FontStyle.Bold) = False
The And, Or & XOr (Exclusive Or) are all bitwise operator. A brief tutorial can be seen here.

Using this method means that you can use any of the FontStyle values.
gsgurdeep's Avatar
Member
 
Join Date: Apr 2007
Location: Delhi (India )
Posts: 94
#5: Jan 7 '09

re: Rich Text Box


Thanks for your interest.
But these answers are not helping me.

but finally i have found the solution of my problem.
I used following code for bold event

Quote:
rich_text.SelectionFont = New Font(rich_text.SelectionFont, IIf(rich_text.SelectionFont.Underline, rich_text.SelectionFont.Style - System.Drawing.FontStyle.Underline, Me.at5_description.SelectionFont.Style + System.Drawing.FontStyle.Underline))

Underline & italic events are same as this code
Reply