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
Printable View
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
why would a different PC change the position of a control relative to the form it is on??
you sure about this?
VB Code:
Public Sub CenterControlHorizontal(ctl As Control, obj As Object) ' Horizontally centers a control within an object With ctl .Left = (obj.Width - .Width) / 2 End With End Sub Public Sub CenterControlVertical(ctl As Control, obj As Object) ' Vertically centers a control within an object With ctl .Top = (obj.Height - .Height) / 2 End With End Sub Public Function CenterForm(frm As Form, Optional frmParent) As Long ' Centers a form on the parent or on the screen On Error GoTo errHandler With frm If IsMissing(frmParent) Then .Move (Screen.Width - .Width) / 2, (Screen.Height - .Height) / 2 Else If frmParent.WindowState = vbMinimized Then .Move (Screen.Width - .Width) / 2, (Screen.Height - .Height) / 2 Else .Move (frmParent.Width - .Width) / 2, (frmParent.Height - .Height) / 2 End If End If End With Exit Function errHandler: CenterForm = Err LogError Error, Err, vbNullString, "bForms.CenterForm" End Function Public Function objBottom(ctl As Control) As Single ' Returns the Y position of the bottom edge of the control On Error Resume Next With ctl objBottom = .Top + .Height End With End Function Function objRight(ctl As Control) As Single ' Returns the X position of the right edge of the control On Error Resume Next With ctl objRight = .Left + .Width End With End Function
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.
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
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:
sub PositionControls CONST BORDER as SINGLE = 180 With myControl .Move (BORDER, BORDER, ScaleWidth - (2 * BORDER), ScaleHeight -(2 * BORDER)) end with End Sub
sorry, was typing my last post when this one came in.Quote:
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.
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 .... :eek: if you dont want to use the control.
The absolute easiest way around the problem is to design with the lowest possible screen resolution.
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:
Sub Form_Resize () PositionControls End Sub Sub PositionControls(X As Single) Dim i As Long On Error GoTo errHandler 'set the width If X < SPLITTER_LIMIT Then X = SPLITTER_LIMIT If X > (Me.ScaleWidth - SPLITTER_LIMIT) Then X = Me.ScaleWidth - SPLITTER_LIMIT SplitterLeft = X lblSavedQueries.Top = objBottom(tbrSQL) + 120 lblSQLStatement.Top = lblSavedQueries.Top If SF.UserPrivilege >= PRIVILEGE_ADMINISTRATOR Then With picSQL .Top = objBottom(lblSavedQueries) + 60 .Width = X .Height = ScaleHeight - (.Top + 60) - framPrivilege.Height - 60 framPrivilege.Top = objBottom(picSQL) + 60 framPrivilege.Width = .Width - 60 framPrivilege.Visible = True End With Else With picSQL .Top = objBottom(lblSavedQueries) + 60 .Width = X .Height = ScaleHeight - (.Top + 60) End With framPrivilege.Visible = False End If For i = optSQL_Type.LBound To optSQL_Type.Count - 1 With optSQL_Type(i) .Left = 0 .Width = picSQL.ScaleWidth End With Next i With lstSQL If SF.UserPrivilege >= PRIVILEGE_ADMINISTRATOR Then optSQL_Type(ADMINISTRATOR_SQL).Visible = True .Top = objBottom(optSQL_Type(ADMINISTRATOR_SQL)) Else .Top = objBottom(optSQL_Type(SUMMARY_SQL)) optSQL_Type(ADMINISTRATOR_SQL).Visible = False End If .Left = 0 .Width = picSQL.ScaleWidth .Height = picSQL.ScaleHeight - .Top End With picSQL.Height = lstSQL.Top + lstSQL.Height + 60 imgSplitter.Left = X With txtComments .Top = ScaleHeight - .Height - 60 lblComments.Top = .Top + 30 lblComments.Left = X + 40 .Left = lblComments.Left + lblComments.Width + 60 .Width = Me.ScaleWidth - .Left End With With txtSQL .Top = picSQL.Top .Left = lblComments.Left .Width = Me.ScaleWidth - .Left .Height = txtComments.Top - .Top - 60 lblSQLStatement.Left = .Left + 60 End With imgSplitter.Top = picSQL.Top imgSplitter.Height = picSQL.Height With cmbFontSizes .Left = tbrSQL.Buttons("Font").Left .Width = tbrSQL.Buttons("Font").Width End With PositionToolbar Exit Sub errHandler: LogError Error, Err, vbNullString, Me.Name & ".PositionControls" End Sub
wonder if there is an api for this ... ??