HowTo to make a concentric centres PushButton

This is a HowTo to make a concentric centres PushButton:

create a new project “Centres Push Button”
add a new Class
rename the class to “CentresPushButton”
set the class super to “PushButton”
add a property “PlacedWindow” with type “Window” to the “CentresPushButton”
add a methode “Center” to the “CentresPushButton”
add this in the methode “Center”
[i]Sub Center()
dim Xwidth as integer
dim XLeft as integer

Xwidth = PlacedWindow.Width
XLeft = ( Xwidth - me.Width ) / 2

me.Left = XLeft
End Sub[/i]

add “CentresPushButton” to Window1
change the text from “Untitled” to any text that you want
place the button at the left bottom corner on Window1
set the locking to left, bottom
add an Open event to “CentresPushButton1” and enter this:
Sub Open()
me.PlacedWindow = self
End Sub

add an Open event to Window1 and enter this:
Sub Open()
CenteresPushButton1.Center
End Sub

add a Resizing event to Window1 and enter this:
Sub Resizing()
CenteresPushButton1.Center
End Sub

If you run the project, the “CentresPushButton” is concentric centres on Window1. If you resized Window1 the button is always concentric centres.

Later today I will share a example project for this.

Seems like a lot of steps to get a control to centre on a window…

How about add a method to the app:

Sub CentreControl(w as window, c as control) c.left = ( w.width - c.Width ) / 2 end sub

and call it in the Window’s resized/ resizing event:

Sub Resizing() app.CentreControl(self,CenteresPushButton1) app.CentreControl(self,SomeOtherControl) End Sub

The sub CentreControl doesn’t work.

Type “Control” has no member name “Width”

Control is too generic then…I assumed that the control would inherit from a Rect

You could cast the passed control as PushButton , or maybe a specific method such as

Sub CentrePushButton(w as window, c as PushButton) c.left = ( w.width - c.Width ) / 2 end sub

would be easier for you ?

That’s the resolution:

Sub CentreControl(w as window, c as RectControl)
c.left = ( w.width - c.Width ) / 2
end sub

This works only on Mac OS X, Windows and RedHat6/7, but not on ubuntu 14.04 LTS 32Bit. Any idea why not?

On Linux Ubuntu we must refresh the RectControl.

Sub CentreControl(w as window, c as RectControl)
c.left = ( w.width - c.width ) / 2
#if TargetLinux then
c.Refresh
#endif
end sub