-
Oct 28th, 2006, 07:02 AM
#1
Thread Starter
Hyperactive Member
[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
-
Oct 28th, 2006, 08:39 AM
#2
Re: VB6 Writing text to a picture box
Hope this works
VB Code:
Private Sub Command1_Click()
PrintText "This is my really really really really long text that i want to print"
End Sub
Sub PrintText(StrText As String)
If Picture1.TextWidth(StrText) > Picture1.ScaleWidth Then
x = InStr(1, StrReverse(StrText), " ")
While Not x = 0
If Picture1.TextWidth(Left(StrText, Len(StrText) - x)) < Picture1.ScaleWidth Then
StrText = Left(StrText, Len(StrText) - x) & vbCrLf & Right(StrText, x)
x = 0
Else
x = InStr(x + 1, StrReverse(StrText), " ")
End If
Wend
End If
Picture1.Print StrText
End Sub
-
Oct 28th, 2006, 09:07 AM
#3
Hyperactive Member
Re: VB6 Writing text to a picture box
THere is also the DrawText Api.
VB Code:
Option Explicit
Private Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type
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
Private Const DT_WORDBREAK = &H10
'usage
Dim rct As RECT
Picture1.ScaleMode = vbPixels
With rct
.Left = 0
.Right = Picture1.ScaleWidth
.Top = 0
.Bottom = Picture1.ScaleHeight
End With
DrawText Picture1.hDC, Text1.Text, -1, rct, DT_WORDBREAK
-
Oct 29th, 2006, 06:53 AM
#4
Thread Starter
Hyperactive Member
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
-
Oct 29th, 2006, 09:49 AM
#5
Addicted Member
Re: VB6 Writing text to a picture box
With the second method you can also use this const to center your text :
-
Oct 29th, 2006, 10:37 AM
#6
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:
Private Sub Form_Load()
Label1.Move 0, 0, Picture1.Width, Picture1.Height
Label1.BackStyle = vbTransparent
Label1.WordWrap = True
End Sub
Private Sub Command1_Click()
Label1.Caption = "This is my really really really really long text that i want to print"
End Sub
Pradeep
-
Oct 29th, 2006, 12:17 PM
#7
Thread Starter
Hyperactive Member
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
-
Oct 29th, 2006, 12:29 PM
#8
Re: VB6 Writing text to a picture box
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:
e.g.
label1.caption = "wwwwwwwwwweeeeerrtyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy"
2. Use vbcrlf to concatenate the strings
VB Code:
Label1 = Label1 & vbCrLf & "XYZXYZXYZ"
Pradeep
-
Oct 29th, 2006, 02:51 PM
#9
Thread Starter
Hyperactive Member
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.
-
Oct 29th, 2006, 02:56 PM
#10
Thread Starter
Hyperactive Member
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
-
Oct 29th, 2006, 03:58 PM
#11
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
-
Oct 29th, 2006, 06:28 PM
#12
Thread Starter
Hyperactive Member
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
-
Oct 29th, 2006, 09:16 PM
#13
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|