Results 1 to 7 of 7

Thread: Alignment in picturebox

  1. #1

    Thread Starter
    Member
    Join Date
    May 2003
    Location
    India
    Posts
    37

    Alignment in picturebox

    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.

  2. #2
    Frenzied Member KayJay's Avatar
    Join Date
    Jul 2001
    Location
    Chennai
    Posts
    1,849
    As a start, play around with TextWidth and TextHeight

    "Brothers, you asked for it."
    ...Francisco Domingo Carlos Andres Sebastian D'Anconia

  3. #3

    Thread Starter
    Member
    Join Date
    May 2003
    Location
    India
    Posts
    37
    Hi,
    But how do i do it. Could u send me the source code for doing this

  4. #4
    Frenzied Member KayJay's Avatar
    Join Date
    Jul 2001
    Location
    Chennai
    Posts
    1,849
    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.
    VB Code:
    1. Private Const strData As String = "This is a sample text that should perhaps run " & _
    2. "into more than three lines depending on the font " & _
    3. "that is used to display the text in this picturebox." & _
    4. "The technique I would suggest is to read this text into a " & _
    5. "variable and split by a space and then print onto this " & _
    6. "picture box depending on the position of the current " & _
    7. "pixel in relation to the current font's TextHeight and/or TextWidth"
    8.  
    9. Private Sub Command1_Click()
    10. Dim strDataArray() As String
    11. Dim strToPrint As String
    12. strDataArray = Split(strData, " ", -1, vbBinaryCompare)
    13. i = 0
    14. Do
    15. If i > UBound(strDataArray) Then
    16.     Exit Do
    17. End If
    18. Do
    19.     If i > UBound(strDataArray) Then
    20.         Exit Do
    21.     End If
    22.        If (picBox.CurrentX + picBox.TextWidth(strToPrint & strDataArray(i))) > (picBox.ScaleWidth) Then
    23.         'i = i + 1
    24.             Exit Do
    25.         End If
    26.     strToPrint = strToPrint & strDataArray(i) & " "
    27.     picBox.CurrentX = (picBox.ScaleWidth - (picBox.TextWidth(strToPrint))) / 2
    28.     i = i + 1
    29. Loop
    30. picBox.Print strToPrint
    31. strToPrint = ""
    32. picBox.CurrentX = 0
    33. Loop
    34. End Sub
    35.  
    36. Private Sub Form_Load()
    37. With picBox
    38.     .ScaleMode = vbPixels
    39.     .Font.Name = "Century Gothic"
    40.     .Font.size = 10
    41.     .Font.Bold = True
    42.     .Font.Italic = True
    43.     .Height = 300
    44.     .Width = 300
    45.     .AutoRedraw = True
    46. End With
    47. End Sub
    HTH

    "Brothers, you asked for it."
    ...Francisco Domingo Carlos Andres Sebastian D'Anconia

  5. #5

    Thread Starter
    Member
    Join Date
    May 2003
    Location
    India
    Posts
    37
    Hi,

    Thank You Very much for the reply. It worked.

  6. #6
    Frenzied Member KayJay's Avatar
    Join Date
    Jul 2001
    Location
    Chennai
    Posts
    1,849
    Ur welcome

    Cheers and Good Luck

    Regards

    KayJay

  7. #7

    Thread Starter
    Member
    Join Date
    May 2003
    Location
    India
    Posts
    37
    Hi,

    Thank You Very much for the reply. It worked.

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