Hi,
i have a picturebox. i would like to know how to align a large multiline text in the center of the picturebox when i press a command button. Could anyone please help me out this.
Printable View
Hi,
i have a picturebox. i would like to know how to align a large multiline text in the center of the picturebox when i press a command button. Could anyone please help me out this.
As a start, play around with TextWidth and TextHeight :)
Hi,
But how do i do it. Could u send me the source code for doing this
Here is an uncommented version. Step through (F8) the code, you should be able to see the logic.
The string is split by spaces, to retrieve each word. Then inside a loop each word is added to a temporary string (inserting the spaces as well). In each iteration this temporary string's width is compared with the picturebox's width to see whether it will fit in properly. When it exceeds the width, the loop is exited and the string is printed onto the box, the temporary string's contents are flushed and the loop is started again until all the words in the array are used up.
HTH :)VB Code:
Private Const strData As String = "This is a sample text that should perhaps run " & _ "into more than three lines depending on the font " & _ "that is used to display the text in this picturebox." & _ "The technique I would suggest is to read this text into a " & _ "variable and split by a space and then print onto this " & _ "picture box depending on the position of the current " & _ "pixel in relation to the current font's TextHeight and/or TextWidth" Private Sub Command1_Click() Dim strDataArray() As String Dim strToPrint As String strDataArray = Split(strData, " ", -1, vbBinaryCompare) i = 0 Do If i > UBound(strDataArray) Then Exit Do End If Do If i > UBound(strDataArray) Then Exit Do End If If (picBox.CurrentX + picBox.TextWidth(strToPrint & strDataArray(i))) > (picBox.ScaleWidth) Then 'i = i + 1 Exit Do End If strToPrint = strToPrint & strDataArray(i) & " " picBox.CurrentX = (picBox.ScaleWidth - (picBox.TextWidth(strToPrint))) / 2 i = i + 1 Loop picBox.Print strToPrint strToPrint = "" picBox.CurrentX = 0 Loop End Sub Private Sub Form_Load() With picBox .ScaleMode = vbPixels .Font.Name = "Century Gothic" .Font.size = 10 .Font.Bold = True .Font.Italic = True .Height = 300 .Width = 300 .AutoRedraw = True End With End Sub
Hi,
Thank You Very much for the reply. It worked.
Ur welcome :)
Cheers and Good Luck
Regards
KayJay
Hi,
Thank You Very much for the reply. It worked.