Bold Button

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Killer42
    Recognized Expert Expert
    • Oct 2006
    • 8429

    #16
    Originally posted by SammyB
    No, the Bold property, etc are all readonly. From help: "Because the Font object is immutable (meaning that you cannot adjust any of it's properties), you can only assign the Font property a new Font object. However, you can base the new font on the existing font."
    ????? That's crazy! :O
    What were M$ smoking when they came up with that one?

    Does that apply to the Font object in general, or a particular one (such as the one provided by SelectionFont)?

    Comment

    • Prose
      New Member
      • Mar 2007
      • 20

      #17
      Originally posted by Killer42
      ????? That's crazy! :O
      What were M$ smoking when they came up with that one?

      Does that apply to the Font object in general, or a particular one (such as the one provided by SelectionFont)?
      i think it's the font object in general.. could be wrong but im pretty sure it apply's to the font object in general

      Comment

      • Prose
        New Member
        • Mar 2007
        • 20

        #18
        Originally posted by Prose
        Okay, umm here is the code i have
        BoldButton:
        Code:
                If RichTextBox1.SelectionFont.Style <> FontStyle.Bold Then
                    RichTextBox1.SelectionFont = New Font(RichTextBox1.Font, FontStyle.Bold)
                Else
                    RichTextBox1.SelectionFont = New Font(RichTextBox1.Font, FontStyle.Regular)
                End If
        Italic Button:
        Code:
                If RichTextBox1.SelectionFont.Style <> FontStyle.Italic Then
                    RichTextBox1.SelectionFont = New Font(RichTextBox1.Font, FontStyle.Italic)
                Else
                    RichTextBox1.SelectionFont = New Font(RichTextBox1.Font, FontStyle.Regular)
                End If
        Underline Button:
        Code:
                If RichTextBox1.SelectionFont.Style <> FontStyle.Underline Then
                    RichTextBox1.SelectionFont = New Font(RichTextBox1.Font, FontStyle.Underline)
                Else
                    RichTextBox1.SelectionFont = New Font(RichTextBox1.Font, FontStyle.Regular)
                End If
        And each button Can either be Checked Or unChecked but wen i want to check the bold button and lets say the underline button for the same text if it is bold first naturally, wen i click the underline button it becomes unbolded but underlined.. Does anyone know how to check two of the buttons at once and have both effects applied to the same selected text?..

        im thinking ill need an "If statement" involving all 3 buttons.. but i havent tried that yet And im not sure if it will be possible to do it that way... Can anyone Help me?


        So No1 Has Any Ideas then?..

        Comment

        • SammyB
          Recognized Expert Contributor
          • Mar 2007
          • 807

          #19
          Originally posted by Killer42
          ????? That's crazy! :O
          What were M$ smoking when they came up with that one?
          I doubt that they allow smoking ;o), but that's sort of what I thought: it doesn't seem to match the rest of DotNet's framework, but since you can base the new font on the existing font and do in one line, its not so bad, but it takes a while to figure out the first time. --Sam

          Comment

          • SammyB
            Recognized Expert Contributor
            • Mar 2007
            • 807

            #20
            Originally posted by Prose
            So No1 Has Any Ideas then?..
            Look back at my last posting of code. Notice that I use SelectionFont as the base for the new font. You are using the control's default font. Don't forget to check the case when the selected text has more than one font style. It will be hard to model MS toggle buttons, but fun (for you)! -- Sam

            Comment

            • Prose
              New Member
              • Mar 2007
              • 20

              #21
              umm i changed them too selectionFont .. but still it wont work.. this is proving to be harder then i thought.. lol here is the code i have now..

              Code:
                  Private Sub tlbItalic_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tlbItalic.Click
              
                      If RichTextBox1.SelectionFont.Style <> FontStyle.Italic Then
                          RichTextBox1.SelectionFont = New Font(RichTextBox1.SelectionFont, FontStyle.Italic)
                      Else
                          RichTextBox1.SelectionFont = New Font(RichTextBox1.SelectionFont, FontStyle.Regular)
                      End If
              
                  End Sub
              
                  Private Sub tlbUnderline_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tlbUnderline.Click
              
                      If RichTextBox1.SelectionFont.Style <> FontStyle.Underline Then
                          RichTextBox1.SelectionFont = New Font(RichTextBox1.SelectionFont, FontStyle.Underline)
                      Else
                          RichTextBox1.SelectionFont = New Font(RichTextBox1.SelectionFont, FontStyle.Regular)
                      End If
              
                  End Sub
              
                  Private Sub tlbStrikeout_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tlbStrikeout.Click
              
                      If RichTextBox1.SelectionFont.Style <> FontStyle.Strikeout Then
                          RichTextBox1.SelectionFont = New Font(RichTextBox1.SelectionFont, FontStyle.Strikeout)
                      Else
                          RichTextBox1.SelectionFont = New Font(RichTextBox1.SelectionFont, FontStyle.Regular)
                      End If
              
                  End Sub
              but it still wont let me apply 2 styles to the one selection.. :(.. any other ideas :|

              Comment

              • Prose
                New Member
                • Mar 2007
                • 20

                #22
                Originally posted by Prose
                umm i changed them too selectionFont .. but still it wont work.. this is proving to be harder then i thought.. lol here is the code i have now..

                Code:
                    Private Sub tlbItalic_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tlbItalic.Click
                
                        If RichTextBox1.SelectionFont.Style <> FontStyle.Italic Then
                            RichTextBox1.SelectionFont = New Font(RichTextBox1.SelectionFont, FontStyle.Italic)
                        Else
                            RichTextBox1.SelectionFont = New Font(RichTextBox1.SelectionFont, FontStyle.Regular)
                        End If
                
                    End Sub
                
                    Private Sub tlbUnderline_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tlbUnderline.Click
                
                        If RichTextBox1.SelectionFont.Style <> FontStyle.Underline Then
                            RichTextBox1.SelectionFont = New Font(RichTextBox1.SelectionFont, FontStyle.Underline)
                        Else
                            RichTextBox1.SelectionFont = New Font(RichTextBox1.SelectionFont, FontStyle.Regular)
                        End If
                
                    End Sub
                
                    Private Sub tlbStrikeout_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tlbStrikeout.Click
                
                        If RichTextBox1.SelectionFont.Style <> FontStyle.Strikeout Then
                            RichTextBox1.SelectionFont = New Font(RichTextBox1.SelectionFont, FontStyle.Strikeout)
                        Else
                            RichTextBox1.SelectionFont = New Font(RichTextBox1.SelectionFont, FontStyle.Regular)
                        End If
                
                    End Sub
                but it still wont let me apply 2 styles to the one selection.. :(.. any other ideas :|
                i guess no1 knows then?... ahh well.. ill probably figure it out eventually..... .. i hope..... lol :)

                Comment

                • Killer42
                  Recognized Expert Expert
                  • Oct 2006
                  • 8429

                  #23
                  Originally posted by Prose
                  i guess no1 knows then?... ahh well.. ill probably figure it out eventually..... .. i hope..... lol :)
                  I don't know the answer, but don't be too impatient.

                  Remember that TSDN is peopled by volunteers, in various timezones all over the world, who put in their own time when they can spare it. It can sometimes take a while.

                  In the meantime, I'd suggest you try searching TSDN and the web as a whole for tutorials covering VB.Net, the use of fonts and the RichTextBox control. If you find anything good, post a link here and we won't have so much trouble next time someone needs this stuff.

                  Comment

                  • Prose
                    New Member
                    • Mar 2007
                    • 20

                    #24
                    Originally posted by Killer42
                    I don't know the answer, but don't be too impatient.

                    Remember that TSDN is peopled by volunteers, in various timezones all over the world, who put in their own time when they can spare it. It can sometimes take a while.

                    In the meantime, I'd suggest you try searching TSDN and the web as a whole for tutorials covering VB.Net, the use of fonts and the RichTextBox control. If you find anything good, post a link here and we won't have so much trouble next time someone needs this stuff.
                    i alrdy searched for alot of stuff.. but ill keep looking.. im thinkin it may involve script in the RTB aswell as script for the buttons aswell.. im not rly sure.. but it is alot harder then what i thought it would be.. :).. anywayz thanks for all ur suggestions.. i guess ill check back later if i figure sumffin out.. :)

                    Thanks again,

                    Prose.

                    Comment

                    • SammyB
                      Recognized Expert Contributor
                      • Mar 2007
                      • 807

                      #25
                      Originally posted by Prose
                      i alrdy searched for alot of stuff..
                      Well, I guess we both should have read the help. The Font object is even stranger than I had thought. The constructor that we are using, Font(prototype, style) does not use the prototype to initialize the style, so you need:
                      RichTextBox1.Se lectionFont = New Font(RichTextBo x1.SelectionFon t, RichTextBox1.Se lectionFont.Sty le Or FontStyle.Bold)
                      to bold the selection. But, at least the event code is now a single line.

                      Comment

                      • Killer42
                        Recognized Expert Expert
                        • Oct 2006
                        • 8429

                        #26
                        Originally posted by Prose
                        i alrdy searched for alot of stuff.. but ill keep looking.. im thinkin it may involve script in the RTB aswell as script for the buttons aswell.. im not rly sure.. but it is alot harder then what i thought it would be.. :).. anywayz thanks for all ur suggestions.. i guess ill check back later if i figure sumffin out.. :)
                        Keep in mind that the VB.Net font stuff is way over my head. But I just had an idea.

                        What if you just did the attributes in three steps. I mean, let's say you have a chunk of text selected in the RTB. You want to set it to both bold and italic. Can't you just set it bold, then set it italic, using the techniques already discussed here? Since the "old font" used as the basis of each would be the SelectionFont, one would imagine that setting one attribute shoudn't affect the others.

                        Or am I talking nonsense?

                        Comment

                        • SammyB
                          Recognized Expert Contributor
                          • Mar 2007
                          • 807

                          #27
                          Originally posted by Killer42
                          <snip> What if you just did the attributes in three steps. <snip>
                          Or am I talking nonsense?
                          No, I think that that is the best way to go: just use the ToolBar ItemClick event and cascade thru each of the buttons, but you will also need a SelectionChange event to keep the buttons in sync with the text. I started that way (after downloading VB Express), but got bogged down like Prose & only could get a single style, so I went back to the Button click event.

                          Now, I think I could go back to a single ItemClick event, but I couldn't get the Selection_Chang e event to work either, so I'm just going to bed. ;o)>>> Sam

                          Comment

                          • Prose
                            New Member
                            • Mar 2007
                            • 20

                            #28
                            Originally posted by SammyB
                            Well, I guess we both should have read the help. The Font object is even stranger than I had thought. The constructor that we are using, Font(prototype, style) does not use the prototype to initialize the style, so you need:
                            RichTextBox1.Se lectionFont = New Font(RichTextBo x1.SelectionFon t, RichTextBox1.Se lectionFont.Sty le Or FontStyle.Bold)
                            to bold the selection. But, at least the event code is now a single line.
                            Okay thanks for that SammyB ive got it all working now.. and with only 1 line of code for each button.. but like u said in ur last post im going to have to mess around with sum other stuff.. eg SelectionChange .. to make it like the MS word buttons :).. thanks for all you help everyone :) im very very grateful!!

                            Prose

                            P.S if this question ever comes up again.. just give them the link of this forum.. here is the code!
                            Code:
                                Private Sub tlbBold_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tlbBold.Click
                            
                                    RichTextBox1.SelectionFont = New Font(RichTextBox1.SelectionFont, RichTextBox1.SelectionFont.Style Or FontStyle.Bold)
                            
                                End Sub
                            
                                Private Sub tlbItalic_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tlbItalic.Click
                            
                                    RichTextBox1.SelectionFont = New Font(RichTextBox1.SelectionFont, RichTextBox1.SelectionFont.Style Or FontStyle.Italic)
                            
                                End Sub
                            
                                Private Sub tlbUnderline_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tlbUnderline.Click
                            
                                    RichTextBox1.SelectionFont = New Font(RichTextBox1.SelectionFont, RichTextBox1.SelectionFont.Style Or FontStyle.Underline)
                            
                                End Sub
                            
                                Private Sub tlbStrikeout_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tlbStrikeout.Click
                            
                                    RichTextBox1.SelectionFont = New Font(RichTextBox1.SelectionFont, RichTextBox1.SelectionFont.Style Or FontStyle.Strikeout)
                            
                                End Sub
                            that is for.. bold, italic, underline and strikeout! but remember it still may not be exactly what you want.. but that allows more the one fontstyle to be applied to the same text!!

                            Thanks Again,

                            Prose.

                            Comment

                            • Killer42
                              Recognized Expert Expert
                              • Oct 2006
                              • 8429

                              #29
                              Thanks for that, Prose. It will be very useful to have here for future searchers to find.

                              One question - does this work alright (that is, not crash) if the user has not selected any text before clicking?

                              Comment

                              • SammyB
                                Recognized Expert Contributor
                                • Mar 2007
                                • 807

                                #30
                                Originally posted by Killer42
                                One question - does this work alright (that is, not crash) if the user has not selected any text before clicking?
                                If nothing is selected then it looks like SelectionFont is the control's default font. In any case it is not null, so it does not crash. --Sam

                                Comment

                                Working...