Solved

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

Posted on 2004-10-14
14
369 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…

867 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now