Results 1 to 9 of 9

Thread: Centering a control on a form

  1. #1
    Dimension
    Guest

    Centering a control on a form

    How do i accomplish this? What might looked centered on my screen will be different on another pc if their settings are not the same as mine. How do i position the control so it's centered in the program. Thanks

  2. #2
    PowerPoster
    Join Date
    Feb 2001
    Location
    Crossroads
    Posts
    3,046
    why would a different PC change the position of a control relative to the form it is on??

    you sure about this?

  3. #3
    PowerPoster cafeenman's Avatar
    Join Date
    Mar 2002
    Location
    Florida
    Posts
    2,819
    VB Code:
    1. Public Sub CenterControlHorizontal(ctl As Control, obj As Object)
    2.  
    3. ' Horizontally centers a control within an object
    4.  
    5. With ctl
    6.   .Left = (obj.Width - .Width) / 2
    7. End With
    8.  
    9. End Sub
    10. Public Sub CenterControlVertical(ctl As Control, obj As Object)
    11.  
    12. ' Vertically centers a control within an object
    13.  
    14. With ctl
    15.   .Top = (obj.Height - .Height) / 2
    16. End With
    17.  
    18. End Sub
    19. Public Function CenterForm(frm As Form, Optional frmParent) As Long
    20.  
    21. ' Centers a form on the parent or on the screen
    22.  
    23. On Error GoTo errHandler
    24.  
    25. With frm
    26.   If IsMissing(frmParent) Then
    27.     .Move (Screen.Width - .Width) / 2, (Screen.Height - .Height) / 2
    28.   Else
    29.     If frmParent.WindowState = vbMinimized Then
    30.       .Move (Screen.Width - .Width) / 2, (Screen.Height - .Height) / 2
    31.     Else
    32.       .Move (frmParent.Width - .Width) / 2, (frmParent.Height - .Height) / 2
    33.     End If
    34.   End If
    35. End With
    36.  
    37. Exit Function
    38.  
    39. errHandler:
    40. CenterForm = Err
    41. LogError Error, Err, vbNullString, "bForms.CenterForm"
    42.  
    43. End Function
    44. Public Function objBottom(ctl As Control) As Single
    45.  
    46. ' Returns the Y position of the bottom edge of the control
    47.  
    48. On Error Resume Next
    49.  
    50. With ctl
    51.   objBottom = .Top + .Height
    52. End With
    53.  
    54. End Function
    55. Function objRight(ctl As Control) As Single
    56.  
    57. ' Returns the X position of the right edge of the control
    58.  
    59. On Error Resume Next
    60.  
    61. With ctl
    62.   objRight = .Left + .Width
    63. End With
    64.  
    65. End Function

  4. #4
    Dimension
    Guest
    my form is maximized when first loaded. I have a control that takes up the whole form. If someone uses this program on their computer, and their settings aren't the same as mine, the control will overlap. Can't have this chief.

  5. #5
    PowerPoster
    Join Date
    Feb 2001
    Location
    Crossroads
    Posts
    3,046
    if you want to keep it centered when the form is resized, the just put some math in the form resize event. somthing like:

    Text1.Top = Form1.Height / 2 - Text1.Height

  6. #6
    PowerPoster cafeenman's Avatar
    Join Date
    Mar 2002
    Location
    Florida
    Posts
    2,819
    Then you probably don't need to center it. You need to resize it. You'll have to do additional math here if there are other controls on the form. This works well for a form that has only a text box or picture box or what have you. If you add a command button at the bottom, just subtract the height of the command button + one more BORDER.

    VB Code:
    1. sub PositionControls
    2. CONST BORDER as SINGLE = 180
    3.  
    4. With myControl
    5.    .Move (BORDER, BORDER, ScaleWidth - (2 * BORDER), ScaleHeight -(2 * BORDER))
    6. end with
    7.  
    8. End Sub

  7. #7
    PowerPoster
    Join Date
    Feb 2001
    Location
    Crossroads
    Posts
    3,046
    Originally posted by Dimension
    my form is maximized when first loaded. I have a control that takes up the whole form. If someone uses this program on their computer, and their settings aren't the same as mine, the control will overlap. Can't have this chief.
    sorry, was typing my last post when this one came in.

    I think the SizerOne control (Component One) might give you what you are looking for. cost $, but the demo is free.

    not sure of any other easy way to do this. might have to resize controls based on display res .... if you dont want to use the control.

    The absolute easiest way around the problem is to design with the lowest possible screen resolution.

  8. #8
    PowerPoster cafeenman's Avatar
    Join Date
    Mar 2002
    Location
    Florida
    Posts
    2,819
    it's too easy to resposition a small number of controls. Don't buy a 3rd party control. Here's a sample from one of my progs. I always do stuff like this:

    VB Code:
    1. Sub Form_Resize ()
    2.  
    3. PositionControls
    4.  
    5. End Sub
    6.  
    7. Sub PositionControls(X As Single)
    8. Dim i As Long
    9.  
    10. On Error GoTo errHandler
    11.  
    12. 'set the width
    13. If X < SPLITTER_LIMIT Then X = SPLITTER_LIMIT
    14. If X > (Me.ScaleWidth - SPLITTER_LIMIT) Then X = Me.ScaleWidth - SPLITTER_LIMIT
    15. SplitterLeft = X
    16.  
    17. lblSavedQueries.Top = objBottom(tbrSQL) + 120
    18. lblSQLStatement.Top = lblSavedQueries.Top
    19.  
    20. If SF.UserPrivilege >= PRIVILEGE_ADMINISTRATOR Then
    21.   With picSQL
    22.     .Top = objBottom(lblSavedQueries) + 60
    23.     .Width = X
    24.     .Height = ScaleHeight - (.Top + 60) - framPrivilege.Height - 60
    25.     framPrivilege.Top = objBottom(picSQL) + 60
    26.     framPrivilege.Width = .Width - 60
    27.     framPrivilege.Visible = True
    28.   End With
    29. Else
    30.   With picSQL
    31.     .Top = objBottom(lblSavedQueries) + 60
    32.     .Width = X
    33.     .Height = ScaleHeight - (.Top + 60)
    34.   End With
    35.   framPrivilege.Visible = False
    36. End If
    37.  
    38. For i = optSQL_Type.LBound To optSQL_Type.Count - 1
    39.   With optSQL_Type(i)
    40.     .Left = 0
    41.     .Width = picSQL.ScaleWidth
    42.   End With
    43. Next i
    44.  
    45. With lstSQL
    46.   If SF.UserPrivilege >= PRIVILEGE_ADMINISTRATOR Then
    47.     optSQL_Type(ADMINISTRATOR_SQL).Visible = True
    48.     .Top = objBottom(optSQL_Type(ADMINISTRATOR_SQL))
    49.   Else
    50.     .Top = objBottom(optSQL_Type(SUMMARY_SQL))
    51.     optSQL_Type(ADMINISTRATOR_SQL).Visible = False
    52.   End If
    53.   .Left = 0
    54.   .Width = picSQL.ScaleWidth
    55.   .Height = picSQL.ScaleHeight - .Top
    56. End With
    57.  
    58. picSQL.Height = lstSQL.Top + lstSQL.Height + 60
    59.  
    60. imgSplitter.Left = X
    61.  
    62. With txtComments
    63.   .Top = ScaleHeight - .Height - 60
    64.   lblComments.Top = .Top + 30
    65.   lblComments.Left = X + 40
    66.   .Left = lblComments.Left + lblComments.Width + 60
    67.   .Width = Me.ScaleWidth - .Left
    68. End With
    69.  
    70. With txtSQL
    71.   .Top = picSQL.Top
    72.   .Left = lblComments.Left
    73.   .Width = Me.ScaleWidth - .Left
    74.   .Height = txtComments.Top - .Top - 60
    75.   lblSQLStatement.Left = .Left + 60
    76. End With
    77.  
    78. imgSplitter.Top = picSQL.Top
    79. imgSplitter.Height = picSQL.Height
    80.  
    81. With cmbFontSizes
    82.   .Left = tbrSQL.Buttons("Font").Left
    83.   .Width = tbrSQL.Buttons("Font").Width
    84. End With
    85.  
    86. PositionToolbar
    87.  
    88. Exit Sub
    89.  
    90. errHandler:
    91. LogError Error, Err, vbNullString, Me.Name & ".PositionControls"
    92.  
    93. End Sub

  9. #9
    PowerPoster
    Join Date
    Feb 2001
    Location
    Crossroads
    Posts
    3,046
    wonder if there is an api for this ... ??

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