• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 413
  • Last Modified:

formatmating only a section of A STRING (FontBold) in a textbox

I have text that is added to a textbox and I want to BOLD only a certain section of the string.
Example:
(1,000) .... One Thousand

I just want the 1,000  font bolded

the code is as followes:

Text3.text = Text3.text & "(" & Label3.Caption & ")" & Text2.text VbCrLf

The Label3.Caption is the Number (1,000) the Text2.text is that number convert to a text form and as the user enters more numbersto be converted it adds them to to the next line of the textbox.

Should I use a Listbox instead of a text box?

I would like to make the Number (Label3.Caption) Font Bold so that it stands out.

Thanks.....
0
ucla11
Asked:
ucla11
  • 5
  • 4
  • 4
  • +1
1 Solution
 
Michael_DCommented:
You cannot use for it neither textbox nor label.
Use RichTextBox instead.


Regards,

Michael
0
 
David LeeCommented:
That's not entirely correct.  You can bold the entire label or textbox, just not a portion of either.  Here's how:

    Label3.FontBold = True
    Text1.FontBold = True
0
 
Pi7Commented:
ucla11, do you mean the text will be displayed in a textbox? Yes you can use the FontBold property of a label to make the caption in the label bold(as long as it appears in the label).However I don't think that is what you want to do.It seems you want to display the final string in a textbox, which, unfortunately does not allow you to format text placed in it.
So if you want to display the text in a textbox, then it is best to use the RichTextBox

To add this control to your tool box,
click the Project menu
click Components...
In the window that (appears scroll down)  and check "Microsoft Rich Textbox Control(6.0)" (or whichever version you have
Open your toolbox, click on this control and draw it on your form
Name the control

Add this code
Dim StartIndex As Integer
With rt
     StartIndex = Len(.Text)  
     .TextRTF = .Text & Label3.Caption & Text2.Text & vbcrlf
     .SelStart = StartIndex
     .SelLength = Len (Label3.Caption)
     .SelStart = Len(.TextRTF)
End With
rt.SelLength = 2
rt.SelBold = True
rt.SelStart = Len(rt.TextRTF)

0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
Pi7Commented:
oh sorry I added unnecessary code. This is the right code

Dim StartIndex As Integer
With rt
     StartIndex = Len(.Text)  
     .TextRTF = .Text & Label3.Caption & Text2.Text & vbcrlf
     .SelStart = StartIndex
     .SelLength = Len (Label3.Caption)
     .SelStart = Len(.TextRTF)
End With
0
 
ucla11Author Commented:
I tryed your code and I am a little confused on how use it. I just want the Number to be bold

example:
Here is what is added to the RichTextBox if the the user types in 1,000

(1,000)...One Thousand  

I want only the number  1,000  to be Bold

Here is the code when they click on enter to do the conversion and add to the RichTextBox...

Private Sub Command1_Click()

    NumInput = Val(Text1.Text)
    If NumInput > 920000000000000# Then
   intReply = MsgBox("Number values of 920 Trillion or less, please" & vbCrLf & "     (920,000,000,000,000 - 15 Digits)", vbCritical + vbOkay, "Too large of a number !")
    Text1.Text = ""
    Text1.SetFocus
    Exit Sub
    End If
         Anwser = NumToString(NumInput)
           
            Text2.Text = Anwser
            Label1.Caption = ""
            Dim StartIndex As Integer

            RichTextBox1 = RichTextBox1.Text & "(" & Label3.Caption & ")..." & Text2.Text & vbCrLf & vbCrLf
            Text1.Text = ""
            Text1.SetFocus
             
            Label3.Caption = "Enter a new number"

The user can't see text1 as they type with textchange it appears in the Label.  did this beacause at the time I had problems format the text on the fy as the user typed it in but since hasve solved this problem but have not revised this programs code

Thanks....
  End Sub
0
 
ucla11Author Commented:
forget the Dim StartIndex As Integer
also just the text version is show in Text2  The (1,000)...One Thousand   is added to the RichTextBox
this way they print the contents of the RichTextBox   This program is to help my kids with  6th grade math

0
 
Michael_DCommented:
OK, here the solution

Private Sub Command1_Click()

    NumInput = Val(Text1.Text)
    If NumInput > 920000000000000# Then
   intReply = MsgBox("Number values of 920 Trillion or less, please" & vbCrLf & "     (920,000,000,000,000 - 15 Digits)", vbCritical + vbOkay, "Too large of a number !")
    Text1.Text = ""
    Text1.SetFocus
    Exit Sub
    End If
         Anwser = NumToString(NumInput)
           
            Text2.Text = Anwser
            Label1.Caption = ""

            RichTextBox1.SelStart = Len(RichTextBox1.Text)
            RichTextBox1.SelLength = 0
            RichTextBox1.SelText = "("
           
            RichTextBox1.SelStart = Len(RichTextBox1.Text)
            RichTextBox1.SelLength = 0
            RichTextBox1.SelBold = True
            RichTextBox1.SelText = NumInput
           
            RichTextBox1.SelStart = Len(RichTextBox1.Text)
            RichTextBox1.SelLength = 0
            RichTextBox1.SelBold = False
            RichTextBox1.SelText = ")..." & Anwser & vbCrLf

            Text1.Text = ""
            Text1.SetFocus
             
            Label3.Caption = "Enter a new number"
End Sub


Hope it helps,
Best regards,

Michael
0
 
Pi7Commented:
ucla11, I've been trying to figure out exactly what you're looking for. I thought you wanted the contents of label3 and Text2 to be displayed, with the contents of label3 in bold format

Just let me know what you want displayed in the rich textbox and which of them you want formatted(Please include the code for NumToString() function

The code I posted will display the contents of label3 and Textbox2 in the rich textbox with the contents of label3 in bold format

regards,
Pi7
0
 
ucla11Author Commented:
I post the NumToString function....All I need is the the Number (example: 1,000)  to be bold.  I have it VB 6.0
0
 
ucla11Author Commented:
I need it VB.net.......
0
 
Michael_DCommented:
ucla11: Did you try my code?
It works for me.

Regards,

Michael
0
 
Pi7Commented:
never knew you were using VB.Net. I use VB6. Fortunately I found the link to a site that'll help you
Check:  http://www.startvbdotnet.com/controls/rtb.htm

try modifying the code:

  Anwser = NumToString(NumInput)
           
            Text2.Text = Anwser
            Label1.Caption = ""
            Dim StartIndex As Integer

            RichTextBox1 = RichTextBox1.Text & "(" & Label3.Caption & ")..." & Text2.Text & vbCrLf & vbCrLf
            Text1.Text = ""
            Text1.SetFocus
             
            Label3.Caption = "Enter a new number"

 to:

  Anwser = NumToString(NumInput)
           
            Text2.Text = Anwser
            Label1.Caption = ""
           

            RichTextBox1 = RichTextBox1.Text & "(" & Label3.Caption & ")..." & Text2.Text & vbCrLf & vbCrLf
            RichTextBox1.SelectionStart =RichTextBox1.Find( Label3.Caption)
            Dim bfont As New Font(RichTextBox1.Font, FontStyle.Bold)
            RichTextBox1.SelectionFont = bfont

            Text1.Text = ""
            Text1.SetFocus
             
            Label3.Caption = "Enter a new number"


regards,
Pi7

0
 
Michael_DCommented:
Pi7:

Did you test your code? It will make bold only the last entry and remove all formatting when you perform this :
RichTextBox1 = RichTextBox1.Text & "(" & Label3 ....
                          ^
                           .Text property contain plain text only.




ucla11 :
Here the VB.Net code that do exactly what you want
       
         Anwser = NumToString(NumInput)
           
        Text2.Text = Anwser
        Label1.Caption = ""

        Dim pos As Integer
        pos = RichTextBox1.TextLength
        RichTextBox1.AppendText("(" & NumInput & ")..." & Anwser & vbCrLf)

        RichTextBox1.Select(pos + 1, NumInput.Length)
        RichTextBox1.SelectionFont = New Font(RichTextBox1.Font, FontStyle.Bold)



0
 
ucla11Author Commented:
I will try the code when I get Home......Thanks
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

  • 5
  • 4
  • 4
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now