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

Easy 500 Points for string function of the label.caption on vb 6 form

Hello Experts!
Just wondering if anyone knows a quick & easy way to turn bold ON & OFF on a vb 6 form Label.caption
Example

If instr ( "*", label1.caption) then turn bold on until vb sees another "*"
 
The asterisk is used to autoformat ms word (when it sees * and then another * anything between the two *s is bold.)

I tried to find this on the internet - but couldn't.

Not too familiar with Visual Basic String functions and don't know if this can actually be done.

Any ideas. Offering easy (I think) 500 pts for the one who nails it.  

0
trudymaresch
Asked:
trudymaresch
  • 7
  • 5
1 Solution
 
fds_fatboyCommented:
I love the ones that say easy 500 points They are invariably nearly impossible ;-)

There is no way to mix fonts in a standard VB label control. You might be able to do it if you Subclass it and OwnerDraw it.


You can easily turn the whole label text to bold:

label1.Font.Bold = True

But not part of the text.

The only easy solution I can think of is by using a RichTextBox control instead of a label.

 
0
 
fds_fatboyCommented:
Or of course use a picturebox instead of a label.

Heres an example:

1. Create a new VB Standard exe app,
2. Add a picturebox (Picture1), a textbox (Text1) and a Command button (Command1 to the form)
3. Drop the code below into the form:
4. Run. Enter the captions in the textbox and click the button to display them.


'Heres the code
'__________________________________________________________
Option Explicit

Private Sub SetCaption(pic As PictureBox, ByVal Caption As String)
    Dim pos As Long
    pic.Cls
    pic.Font.Bold = False
    pos = InStr(Caption, "*")
   
    Do Until pos = 0
        pic.Print Left$(Caption, pos); '   If you want the *s not to appear change to :     pic.Print Left$(Caption, pos - 1);
        Caption = Mid$(Caption, pos + 1)
        pic.Font.Bold = Not pic.Font.Bold
        pos = InStr(Caption, "*")
    Loop
   
    pic.Print Caption
End Sub

Private Sub Command1_Click()
    SetCaption Picture1, Text1.Text
End Sub
'__________________________________________________________
0
 
trudymareschAuthor Commented:
So very True about the "Easy 500!"  Wasn't sure if you could turn bold ON & Off inside label or text and one never knows what the gurus have going on?

FDS FATBOY! - Thanks I'm giving this a try to see if it works.  Let you know soon!
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
trudymareschAuthor Commented:
Thanks! This works great wonder how it will work using picture1 with data in a form though giving it a whirl!
0
 
trudymareschAuthor Commented:
Worked great on a datafield also! Thanks so very much again fds_fatbody!
0
 
trudymareschAuthor Commented:
Oh my typing is terrible - many apologies fds_fatboy.. (I'm the one with the fatbody btw that's probably why I typed it).  I see that the caption doesn't wrap.
0
 
fds_fatboyCommented:
Something I forgot, Your picturebox should have its AutoRedraw property set to true

I'll look in to wrapping it. I know I can do it but I'm a bit busy with work at the moment.

First things first. Here is a slightly different method with similar functionality to above (does not display asterisks):

Private Sub SetCaption(pic As PictureBox, ByVal Caption As String)
    Dim count As Long
    Dim captionParts() As String
    pic.Cls
    pic.Font.Bold = False
   
    If Caption <> vbNullString Then
        captionParts = Split(Caption, "*")
       
        For count = LBound(captionParts) To UBound(captionParts)
            pic.Print captionParts(count);
            pic.Font.Bold = Not pic.Font.Bold
        Next
    End If
End Sub
0
 
trudymareschAuthor Commented:
Wow - thats great! Thanks so much for this. ! I'll have a look at that.

I'm trying to do a wrap based on width of the picture box and a flat amount of characters to fit across. But that and working it to break on a space " " ... I'm not doing to well.  I figured that 67 characters is good number to display across each row width.  But I don't know why I cannot get it to properly divide into total number of characters in the caption. It somehow cannot determine the exact number of characters in the caption?? Maybe I should round down to 60?

take the string
divide by 60 = total num of lines to

for i = 1 to (string / 60)
  print first 60 char
next

I dunno? Any ideas how to wrap better than this ?
0
 
fds_fatboyCommented:
Sorry - I had a very busy day in work and then I got home and the children nedded putting to bed and then my wife asked me to look at some tricky SQL and then it was 1:15am. Anyway, no it's ten to two, but I have a new SetCaption routine with added functionality:

1. It now wraps to the size of the label (picturebox)
2. The optional parameter BoldToggle allows you to use any string inside the caption to toggle bold. - the default is currently *.
3. The Caption is stored in the Pictureboxe's Tag property.

Here's the code:

Private Sub SetCaption(pic As PictureBox, Caption As String, Optional BoldToggle As String = "*")
    Dim captionWords() As String
    Dim wordCount As Long
    Dim parts() As String
    Dim partCount As Long
    Dim XPos As Single
    Dim boldFlag As Boolean
    pic.Tag = Caption
    pic.Cls

    If Caption = vbNullString Then
        Exit Sub
    End If

    captionWords = Split(Caption, " ")
    pic.Font.Bold = False

    For wordCount = LBound(captionWords) To UBound(captionWords)
        If Len(captionWords(wordCount)) > 0 Then
            parts = Split(captionWords(wordCount), BoldToggle)
            If pic.CurrentX > 0 Then
                boldFlag = pic.Font.Bold
                'Measure word
                XPos = pic.CurrentX
                For partCount = LBound(parts) To UBound(parts)
                    XPos = XPos + pic.TextWidth(parts(partCount))
                    If XPos > pic.ScaleWidth Then
                        pic.Print
                        Exit For
                    End If
                    pic.Font.Bold = Not pic.Font.Bold
                Next
                pic.Font.Bold = boldFlag
            End If
               
            For partCount = LBound(parts) To UBound(parts) - 1
                pic.Print parts(partCount);
                pic.Font.Bold = Not pic.Font.Bold
            Next
            pic.Print parts(UBound(parts)) & " ";
        Else
            pic.Print " ";
        End If
    Next
   
End Sub

0
 
fds_fatboyCommented:
Also, if you want resizable labels (possibly changing shape as your form resizes) that still wrap, use the above code in conjunction with this snippet:

'For each picturebox label, replace "Picture1" with the picturebox name
Private Sub Picture1_Resize()
    SetCaption Picture1, Picture1.Tag, "*"
End Sub
0
 
trudymareschAuthor Commented:
WOW!!! That's what I call having my cake and eating it!
0
 
trudymareschAuthor Commented:
EXCELLENT EXCELLENT EXCELLENT - THIS IS BEAUTIFUL! THANKS SO MUCH!!!!!
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

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