Solved

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

Posted on 2004-10-14
14
371 Views
Last Modified: 2006-11-17
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
Comment
Question by:ucla11
  • 5
  • 4
  • 4
  • +1
14 Comments
 
LVL 13

Expert Comment

by:Michael_D
ID: 12313320
You cannot use for it neither textbox nor label.
Use RichTextBox instead.


Regards,

Michael
0
 
LVL 76

Expert Comment

by:David Lee
ID: 12314649
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
 
LVL 5

Expert Comment

by:Pi7
ID: 12317930
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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
LVL 5

Expert Comment

by:Pi7
ID: 12317948
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
 

Author Comment

by:ucla11
ID: 12321995
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
 

Author Comment

by:ucla11
ID: 12322049
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
 
LVL 13

Expert Comment

by:Michael_D
ID: 12324452
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
 
LVL 5

Expert Comment

by:Pi7
ID: 12337544
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
 

Author Comment

by:ucla11
ID: 12342363
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
 

Author Comment

by:ucla11
ID: 12342379
I need it VB.net.......
0
 
LVL 13

Expert Comment

by:Michael_D
ID: 12343733
ucla11: Did you try my code?
It works for me.

Regards,

Michael
0
 
LVL 5

Expert Comment

by:Pi7
ID: 12345713
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
 
LVL 13

Accepted Solution

by:
Michael_D earned 300 total points
ID: 12346221
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
 

Author Comment

by:ucla11
ID: 12352430
I will try the code when I get Home......Thanks
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Adding to a VBA? 6 69
How to Add / Edit Windows Menu 4 59
How to make an ADE file by code? 11 83
Help me. 3 54
I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

785 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question