CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8

Thread: Bolding text

  1. #1
    Join Date
    Jul 2002
    Posts
    44

    Bolding text

    I want to bold text, I can do it with a button easily, but i need for it to do it automatically.
    For example I need
    txtquote1.Text = txtquote1.Text & vbNewLine & "Lettershop" & vbNewLine
    "Lettershop" to be bold
    How do i put that in code?

  2. #2
    Join Date
    Jun 2001
    Location
    MO, USA
    Posts
    2,868
    Textboxes can only display one font at at time. You need to use a richtextbox to display multiple fonts, then use SelText properties to change the text characteristics.

  3. #3
    Join Date
    Jul 2002
    Posts
    44
    yeah, I am using a richtextbox, but the txtquote1.selbold=true does nothing

  4. #4
    Join Date
    Jun 2001
    Location
    MO, USA
    Posts
    2,868
    Private Sub Command1_Click()
    'make fox bold
    RichTextBox1.SelStart = RichTextBox1.Find("fox")
    RichTextBox1.SelLength = 3
    RichTextBox1.SelBold = True
    End Sub

    Private Sub Form_Load()
    RichTextBox1.Text = "The quick brown fox jumped over the lazy brown dog."
    End Sub
    Last edited by DSJ; July 19th, 2002 at 02:56 PM.

  5. #5
    Join Date
    Jul 2002
    Posts
    44
    Is there a way to bold text that you are outputting
    lets say,

    richtextbox1 = "Test line here"
    But i want that whole sting bold... can i do that.
    I want only that string bold, even if that same string was in the textbox 100 times.
    is it possible?

  6. #6
    Join Date
    Apr 2000
    Location
    South Carolina,USA
    Posts
    2,210
    SelBold must be used in conjunction with SelStart and SelLen methods. The example below will make the word "There" bold
    Code:
    Private Sub Command1_Click()
        Dim x As Long
        RichTextBox1.Text = "Hello"             ' Print some text
        RichTextBox1.Text = RichTextBox1.Text & " There" ' add some more
        x = InStr(1, RichTextBox1.Text, "There")         ' Locate text to colorize
        RichTextBox1.SelStart = x - 1                   ' set starting point
        RichTextBox1.SelLength = 5                      ' Set length of "There"
        'RichTextBox1.SelColor = vbRed                   ' Colorize it
        RichTextBox1.SelBold = True                     ' Bold it
    End Sub

  7. #7
    Join Date
    Sep 2001
    Location
    IL, USA
    Posts
    1,090
    Originally posted by thecaptainjs
    Is there a way to bold text that you are outputting
    lets say,

    richtextbox1 = "Test line here"
    But i want that whole sting bold... can i do that.
    I want only that string bold, even if that same string was in the textbox 100 times.
    is it possible?
    Code:
    'The following code will change the color and boldness of the selected string while entering text in the RichBox.
    Dim start As Long, BoldString As String
    Private Sub Form_Load()
       start = 1
       BoldString = "Test line here"
    End Sub
    
    Private Sub RichTextBox1_Change()
       Dim pos As Long
       With RichTextBox1
       pos = InStr(start, .Text, BoldString)
       If pos > 0 Then
          start = pos + Len(BoldString)
          .SelStart = pos - 1
          .SelLength = Len(BoldString)
          .SelBold = True
          .SelColor = vbRed
          .SelStart = pos + .SelLength
          .SelBold = False
          .SelColor = vbBlack
       End If
       End With
    End Sub

  8. #8
    Join Date
    Sep 2001
    Location
    IL, USA
    Posts
    1,090
    Originally posted by thecaptainjs
    Is there a way to bold text that you are outputting
    lets say,

    richtextbox1 = "Test line here"
    But i want that whole sting bold... can i do that.
    I want only that string bold, even if that same string was in the textbox 100 times.
    is it possible?
    Code:
    'The following code find all occurrences of a string in a RichBox and make them Bold with a Blue color.
    Private Sub Command1_Click()
       Dim BoldString As String, pos As Long
       BoldString = "Test line here"
       With RichTextBox1
       pos = InStr(1, .Text, BoldString)
       Do While pos > 0
          .SelStart = pos - 1
          .SelLength = Len(BoldString)
          .SelBold = True
          .SelColor = vbBlue
          pos = InStr(pos + Len(BoldString), .Text, BoldString)
       Loop
       End With
    End Sub

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured