Rich Text Box

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • gsgurdeep
    New Member
    • Apr 2007
    • 94

    #1

    Rich Text Box

    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.
  • truezplaya
    New Member
    • Jul 2007
    • 115

    #2
    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 checkButtonsCli cked
    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

    Comment

    • truezplaya
      New Member
      • Jul 2007
      • 115

      #3
      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

      Comment

      • phvfl
        Recognized Expert New Member
        • Aug 2007
        • 173

        #4
        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.Under line.

        A very thrown together implementation would be:

        Code:
        Private Sub AddStyle(ByVal style As FontStyle)
            Dim newStyle As FontStyle = testRich.SelectionFont.Style Or style
            Dim ifont As New Font(testRich.Font, newStyle)
            testRich.SelectionFont = ifont
          End Sub
        
          Private Sub RemoveStyle(ByVal style As FontStyle)
            Dim newStyle As FontStyle = testRich.SelectionFont.Style Xor style
            Dim ifont As New Font(testRich.Font, newStyle)
            testRich.SelectionFont = ifont
          End Sub
        
          Private Function HasStyle(ByVal style As FontStyle) As Boolean
            Return (testRich.SelectionFont.Style And style) = style
          End Function
        This assumes that the RichTextBox control is called testRich. Calling these procedures is done by:
        Code:
        ' HasStyle(FontStyle.Bold) = False
        AddStyle(FontStyle.Bold)
        
        ' HasStyle(FontStyle.Bold) = True
        RemoveStyle(FontStyle.Bold)
        
        ' 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.

        Comment

        • gsgurdeep
          New Member
          • Apr 2007
          • 94

          #5
          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

          rich_text.Selec tionFont = New Font(rich_text. SelectionFont, IIf(rich_text.S electionFont.Un derline, rich_text.Selec tionFont.Style - System.Drawing. FontStyle.Under line, Me.at5_descript ion.SelectionFo nt.Style + System.Drawing. FontStyle.Under line))

          Underline & italic events are same as this code

          Comment

          Working...