Results 1 to 13 of 13

Thread: [RESOLVED] VB6 Writing text to a picture box

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2006
    Posts
    403

    Resolved [RESOLVED] VB6 Writing text to a picture box

    In my programme I am generating various text and writing it (by the PRINT instruction) to a picture box on my form.

    If each piece of text is less wide than my picture box, this works just fine. However if the text is longer than the picture box is wide, it does not continue on the next line but is lost off the right hand side of the picture box, and hence is not visible to the user.

    How do I arrange that a piece of text longer than the width of my picture box will in effect 'line feed' when it gets to the right hand edge and continue on the next line please?

    Am a newcomer to VB. I expect the answer is very straightforward - but I can't find a Property of a picture box which allows this to happen. In my current project I got round the problem by having loads of Print instructions (one for each line of text) and made sure that none was longer than the PB width. However should I ever re-size the picture box smaller, this will not continue to work.

    Solutions will be gratefully received.

    C.A.Moore

  2. #2
    Frenzied Member Andrew G's Avatar
    Join Date
    Nov 2005
    Location
    Sydney
    Posts
    1,587

    Re: VB6 Writing text to a picture box

    Hope this works

    VB Code:
    1. Private Sub Command1_Click()
    2.     PrintText "This is my really really really really long text that i want to print"
    3. End Sub
    4.  
    5. Sub PrintText(StrText As String)
    6. If Picture1.TextWidth(StrText) > Picture1.ScaleWidth Then
    7.     x = InStr(1, StrReverse(StrText), " ")
    8.     While Not x = 0
    9.         If Picture1.TextWidth(Left(StrText, Len(StrText) - x)) < Picture1.ScaleWidth Then
    10.             StrText = Left(StrText, Len(StrText) - x) & vbCrLf & Right(StrText, x)
    11.             x = 0
    12.         Else
    13.             x = InStr(x + 1, StrReverse(StrText), " ")
    14.         End If
    15.     Wend
    16. End If
    17.  
    18. Picture1.Print StrText
    19. End Sub

  3. #3
    Hyperactive Member
    Join Date
    Aug 2003
    Location
    Wigan, UK
    Posts
    291

    Re: VB6 Writing text to a picture box

    THere is also the DrawText Api.
    VB Code:
    1. Option Explicit
    2. Private Type RECT
    3.         Left As Long
    4.         Top As Long
    5.         Right As Long
    6.         Bottom As Long
    7. End Type
    8.  
    9. Private Declare Function DrawText Lib "user32" Alias "DrawTextA" (ByVal hDC As Long, ByVal lpStr As String, ByVal nCount As Long, lpRect As RECT, ByVal wFormat As Long) As Long
    10. Private Const DT_WORDBREAK = &H10
    11.  
    12.  
    13. 'usage
    14. Dim rct As RECT
    15.  
    16. Picture1.ScaleMode = vbPixels
    17.  
    18. With rct
    19.     .Left = 0
    20.     .Right = Picture1.ScaleWidth
    21.     .Top = 0
    22.     .Bottom = Picture1.ScaleHeight
    23. End With
    24.  
    25. DrawText Picture1.hDC, Text1.Text, -1, rct, DT_WORDBREAK

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2006
    Posts
    403

    Re: VB6 Writing text to a picture box

    Andrew G and Thingimijig

    Many thanks for your rapid replies. I will try both out later this weekend and post a note of success, or otherwise, at this string.

    I am pleased that I was not simply missing something totally obvious and stupid.

    C.A.Moore Wales,UK

  5. #5
    Addicted Member
    Join Date
    Jun 2006
    Location
    Egypt
    Posts
    162

    Re: VB6 Writing text to a picture box

    With the second method you can also use this const to center your text :
    VB Code:
    1. Const DT_CENTER = &H1
    Mohammed Sayed - Egypt - Anesthetist



    =

    =

  6. #6
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: VB6 Writing text to a picture box

    There is a much easier way to do this.
    I would have preferred putting a label control inside the picturebox with its background transparent and wordwrap to true.
    Something like this should do.

    VB Code:
    1. Private Sub Form_Load()
    2.     Label1.Move 0, 0, Picture1.Width, Picture1.Height
    3.     Label1.BackStyle = vbTransparent
    4.     Label1.WordWrap = True
    5. End Sub
    6.  
    7. Private Sub Command1_Click()
    8.     Label1.Caption = "This is my really really really really long text that i want to print"
    9. End Sub


    Pradeep
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2006
    Posts
    403

    Re: VB6 Writing text to a picture box

    PRADEEP1210

    Thank you for this suggestion. It almost works as well as I could wish. If I use a Label rather than a picture box, with wordwrap set to TRUE most things I want to achieve are OK.

    Remaining problems with this however are these :-

    1. If the text string my program is writing is longer than the Label is wide, it does not wrapround, but gets lost 'off the right hand side' (and confuses subsequent writings to the Label).

    2. How may I get the Label routine to, in effect, print a blank line to make its layout better? If I use "Label1 = label1 + "XYZXYZXYZ" statements, this adds to the existing text but on a continuous line basis. I was trying to use a picture box and the print.pictureX method because this allows for each new text section to start on a new line. If I want a line space, I can simply print "". under this routine.

    Your label method is the better answer to the problem, if only the above two snags can be resolved.

    C.A. Moore Wales,UK

  8. #8
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: VB6 Writing text to a picture box

    Quote Originally Posted by camoore
    PRADEEP1210

    Thank you for this suggestion. It almost works as well as I could wish. If I use a Label rather than a picture box, with wordwrap set to TRUE most things I want to achieve are OK.

    Remaining problems with this however are these :-

    1. If the text string my program is writing is longer than the Label is wide, it does not wrapround, but gets lost 'off the right hand side' (and confuses subsequent writings to the Label).

    2. How may I get the Label routine to, in effect, print a blank line to make its layout better? If I use "Label1 = label1 + "XYZXYZXYZ" statements, this adds to the existing text but on a continuous line basis. I was trying to use a picture box and the print.pictureX method because this allows for each new text section to start on a new line. If I want a line space, I can simply print "". under this routine.

    Your label method is the better answer to the problem, if only the above two snags can be resolved.

    C.A. Moore Wales,UK
    1. This should only do only if there is no linebreak or space in the word and it exceeds the label width. It's a rare possibility in real situation in case you are dealing with words.
    VB Code:
    1. e.g.
    2. label1.caption = "wwwwwwwwwweeeeerrtyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy"

    2. Use vbcrlf to concatenate the strings
    VB Code:
    1. Label1 = Label1 & vbCrLf & "XYZXYZXYZ"

    Pradeep
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2006
    Posts
    403

    Re: VB6 Writing text to a picture box

    Thank you predeep1210.
    Problem is all but resolved. Using a label with wordwrap set to True I can write code such as
    Label1 = Label1 + "XXX YYY ZZZ" to add to its contents on same line.

    If I write
    Label1 = Label1 + vbCrLf + "XXX YYY ZZZ" the new contents (XXX YYY ZZZ) start on the next line.

    If I write Label1 = Label1 + vbCrLf + vbCrLf + "XXX YYY ZZZ" the new contents start after a neat linespace gap.

    This virtually solves my problem, thank you. However there remains the situation in which a continuous word or character set is longer than the width of the label box. If this is the case it does not seem to wrap around. However I agree this is unlikely, and must be accepted as a limitation of the solution - unless you have any further thoughts.

    I appreciate your help.

    C.A. Moore Wales,UK
    Last edited by camoore; Oct 29th, 2006 at 02:54 PM.

  10. #10

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2006
    Posts
    403

    Re: VB6 Writing text to a picture box

    I will mark this thread as RESOLVED. VMT. for all replies. As a beginner I have favoured what seems the simplest solution to the current problem.

    Regards, Forum

  11. #11
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: [RESOLVED] VB6 Writing text to a picture box

    you can check the width of each string against the width of the label, but then the decision of where to break the word is still a problem, of course you could also make the label wider if need be (up to whatever limitation the form or screen impose on you)
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  12. #12

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2006
    Posts
    403

    Re: [RESOLVED] VB6 Writing text to a picture box

    Thank you again westconn1 for your helpful suggestions.

    It will not be too great a limitation to ensure that Labels are wider than the longest string which a program will write to them, but it would habe been great to find a simple routine (because I am a beginner to all this) which would simply break up a long string, insert a couple of hyphens and continue it on the next line of the label.

    However, thanks to the replies to my thread I now know (almost!) how to do this by comparing the label size with the string length. I think that there may be a problem in that still because the width occupied by text depends on whether it is upper or lower case whereas the string length returns only the number of characters, not the space they will require.


    My immediate problem was resolved thanks to Pradeep1210's suggestion and I do not anticipate needing to go more deeply into the matter for the time being.


    One other simple stupid problem I have I will post as a separate new thread.

    With thanks,


    C.A.Moore Wales, UK

  13. #13
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: [RESOLVED] VB6 Writing text to a picture box

    you can get the actual width of the string as it is to be printed by using textwidth(string)
    textwidth will return a number of pixels (or something depending on the scalemode), which will be the same measure as the width for the label
    the textwidth is based on the font,style and size of the form, but the font of the form can be changed to be the same as the label, though by default they will be the same unless you change them
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

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