Layout management
The IronPython Mono Winforms tutorial continues with the layout management of controls. After we have placed controls on their parent containers, we have to ensure their proper layout.
Anchor
The Anchor property of a control determines
how it is resized with its parent. Anchor is a term from the marine
world. When an anchor is dropped into the water, the ship is fixed in
certain place. Same applies for the Winforms controls.
Each control in Winforms can have one of these
AnchorStyles values:
- Top
- Left
- Right
- Bottom
Notice that controls are not restricted to one value. They
can take any combination of these values using the | operator.
Basic Anchor example
The following example shows a very basic example, demonstrating
the Anchor property.
#!/usr/bin/ipy
import clr
clr.AddReference("System.Windows.Forms")
clr.AddReference("System.Drawing")
from System.Windows.Forms import Application, Form
from System.Windows.Forms import Button, AnchorStyles
from System.Drawing import Size, Point
class IForm(Form):
def __init__(self):
self.Text = 'Anchor'
self.Size = Size(210, 210)
btn1 = Button()
btn1.Text = "Button"
btn1.Parent = self
btn1.Location = Point(30, 30)
btn2 = Button()
btn2.Text = "Button"
btn2.Parent = self
btn2.Location = Point(30, 80)
btn2.Anchor = AnchorStyles.Right
self.CenterToScreen()
Application.Run(IForm())
This is a very basic code example that clearly shows what the
Anchor property is all about. We have two buttons
on the form. The first button has the default AnchorStyles
values, which are AnchorStyles.Top | AnchorStyles.Left.
The second button has explicitly set the AnchorStyles.Right.
btn2.Anchor = AnchorStyles.Right
We explicitly set the Anchor property of the second
button to AnchorStyles. Right value.
Now have a look at the following two images. The left one shows the application at the beginning. The right one shows the same application after resizement. The first button keeps its distance from the left and top borders of the form. The second button keeps its distance from the right border of the form. But it does not keep any distance in the vertical direction.
Dock
The Dock property allows us to stick a control to a certain edge of the
parent form or control.
The following are possible DockStyle values.
- Top
- Left
- Right
- Bottom
- Fill
- None
Editor skeleton
The following code example shows the Dock property in action.
#!/usr/bin/ipy
import clr
clr.AddReference("System.Windows.Forms")
clr.AddReference("System.Drawing")
from System.Windows.Forms import Application, Form, MainMenu, StatusBar
from System.Windows.Forms import Shortcut, MenuItem, TextBox, DockStyle
from System.Drawing import Size, Point
class IForm(Form):
def __init__(self):
self.Text = 'Editor'
self.Size = Size(210, 180)
mainMenu = MainMenu()
filem = mainMenu.MenuItems.Add('&File')
filem.MenuItems.Add(MenuItem('E&xit',
self.OnExit, Shortcut.CtrlX))
self.Menu = mainMenu
tb = TextBox()
tb.Parent = self
tb.Dock = DockStyle.Fill
tb.Multiline = True
sb = StatusBar()
sb.Parent = self
sb.Text = 'Ready'
self.CenterToScreen()
def OnExit(self, sender, event):
self.Close()
Application.Run(IForm())
We show a menubar and a statusbar. And the remaining area is taken by the
TextBox control.
tb = TextBox() tb.Parent = self
Here we create the TextBox control.
Form container is set to be
the parent for the text box.
tb.Dock = DockStyle.Fill
This code line makes the TextBox control take up the
remaining space inside the form container.
Anchored buttons
The next example shows two buttons placed in the bottom right corner of the form.
#!/usr/bin/ipy
import clr
clr.AddReference("System.Windows.Forms")
clr.AddReference("System.Drawing")
clr.AddReference("System")
from System.Windows.Forms import Application, Form, Button, Panel
from System.Windows.Forms import DockStyle, AnchorStyles
from System.Drawing import Size, Point
WIDTH = 250
HEIGHT = 150
BUTTONS_SPACE = 15
PANEL_SPACE = 8
CLOSE_SPACE = 10
class IForm(Form):
def __init__(self):
self.Text = 'Buttons'
self.Size = Size(WIDTH, HEIGHT)
ok = Button()
PANEL_HEIGHT = ok.Height + PANEL_SPACE
panel = Panel()
panel.Height = PANEL_HEIGHT
panel.Dock = DockStyle.Bottom
panel.Parent = self
x = ok.Width * 2 + BUTTONS_SPACE
y = (PANEL_HEIGHT - ok.Height) / 2
ok.Text = "Ok"
ok.Parent = panel
ok.Location = Point(WIDTH-x, y)
ok.Anchor = AnchorStyles.Right
close = Button()
x = close.Width
close.Text = "Close"
close.Parent = panel
close.Location = Point(WIDTH-x-CLOSE_SPACE, y)
close.Anchor = AnchorStyles.Right
self.CenterToScreen()
Application.Run(IForm())
The example displays OK, Close buttons in the bottom right corner of the window, as it is common in dialog windows.
WIDTH = 250 HEIGHT = 150
The WIDTH and HEIGHT variables
determine the width and height of the application window.
BUTTONS_SPACE = 15 PANEL_SPACE = 8 CLOSE_SPACE = 10
The BUTTONS_SPACE is the space between the OK and
the Close button. The PANEL_SPACE is the
space between the panel and the bottom of the form. Finally, the
CLOSE_SPACE variable sets the
space between the Close button and the right border of the form.
PANEL_HEIGHT = ok.Height + PANEL_SPACE
Here we compute the height of the panel. The height of the panel is based on the height of the OK button. And we add some additional space, so that the buttons are not too close to the border.
panel = Panel() panel.Height = PANEL_HEIGHT panel.Dock = DockStyle.Bottom panel.Parent = self
Here we create and manage the Panel control.
In this example, it is used as a container for our buttons. It is
glued to the bottom border of the form. And the buttons are placed within the panel.
ok.Text = "Ok" ok.Parent = panel ok.Location = Point(WIDTH-x, y) ok.Anchor = AnchorStyles.Right
The OK button's parent is set to the panel widget. The location is computed.
And the Anchor
property is set to the right. The other button is created similarly.
Player skeleton
The last example of this part of the IronPython Mono Winforms tutorial shows a more complex example. It is a skeleton of a music player.
#!/usr/bin/ipy
import clr
clr.AddReference("System.Windows.Forms")
clr.AddReference("System.Drawing")
from System.Windows.Forms import Application, Form, Button, Panel
from System.Windows.Forms import DockStyle, AnchorStyles, StatusBar
from System.Windows.Forms import TrackBar, MainMenu, MenuItem
from System.Windows.Forms import FlatStyle, TickStyle, Shortcut
from System.Drawing import Size, Point, Bitmap, Color
class IForm(Form):
def __init__(self):
self.Text = 'Player'
self.Size = Size(350, 280)
mainMenu = MainMenu()
filem = mainMenu.MenuItems.Add("&File")
playm = mainMenu.MenuItems.Add("&Play")
view = mainMenu.MenuItems.Add("&View")
tools = mainMenu.MenuItems.Add("&Tools")
favourites = mainMenu.MenuItems.Add("&Favourites")
help = mainMenu.MenuItems.Add("&Help")
filem.MenuItems.Add(MenuItem("E&xit",
self.OnExit, Shortcut.CtrlX))
self.Menu = mainMenu
panel = Panel()
panel.Parent = self
panel.BackColor = Color.Black
panel.Dock = DockStyle.Fill
buttonPanel = Panel()
buttonPanel.Parent = self
buttonPanel.Height = 40
buttonPanel.Dock = DockStyle.Bottom
pause = Button()
pause.FlatStyle = FlatStyle.Popup
pause.Parent = buttonPanel
pause.Location = Point(5, 10)
pause.Size = Size(25, 25)
pause.Image = Bitmap("pause.png")
play = Button()
play.FlatStyle = FlatStyle.Popup
play.Parent = buttonPanel
play.Location = Point(35, 10)
play.Size = Size(25, 25)
play.Image = Bitmap("play.png")
forward = Button()
forward.FlatStyle = FlatStyle.Popup
forward.Parent = buttonPanel
forward.Location = Point(80, 10)
forward.Size = Size(25, 25)
forward.Image = Bitmap("forward.png")
backward = Button()
backward.FlatStyle = FlatStyle.Popup
backward.Parent = buttonPanel
backward.Location = Point(110, 10)
backward.Size = Size(25, 25)
backward.Image = Bitmap("backward.png")
tb = TrackBar()
tb.Parent = buttonPanel
tb.TickStyle = TickStyle.None
tb.Size = Size(150, 25)
tb.Location = Point(200, 10)
tb.Anchor = AnchorStyles.Right
audio = Button()
audio.FlatStyle = FlatStyle.Popup
audio.Parent = buttonPanel
audio.Size = Size(25, 25)
audio.Image = Bitmap("audio.png")
audio.Location = Point(170, 10)
audio.Anchor = AnchorStyles.Right
sb = StatusBar()
sb.Parent = self
sb.Text = "Ready"
self.CenterToScreen()
def OnExit(self, sender, event):
self.Close()
Application.Run(IForm())
This is a more complex example showing both Dock and
Anchor properties in action.
mainMenu = MainMenu()
filem = mainMenu.MenuItems.Add("&File")
...
self.Menu = mainMenu
Here we create the menubar.
panel = Panel() panel.Parent = self panel.BackColor = Color.Black panel.Dock = DockStyle.Fill
This is the black panel, which takes all the remaining space, left by the menubar, statusbar and the control panel.
buttonPanel = Panel() buttonPanel.Parent = self buttonPanel.Height = 40 buttonPanel.Dock = DockStyle.Bottom
This is the control panel. Its parent is the form container. It is glued to the bottom of the form. Its height is 40px. Inside this control panel, we place all the buttons and the trackar.
pause = Button()
pause.FlatStyle = FlatStyle.Popup
pause.Parent = buttonPanel
pause.Location = Point(5, 10)
pause.Size = Size(25, 25)
pause.Image = Bitmap("pause.png")
The pause button is one of the four buttons that has the default
Anchor property value. The style of the button
is set to flat, because it looks better. We put a bitmap on the button.
tb.Anchor = AnchorStyles.Right ... audio.Anchor = AnchorStyles.Right
The last two controls are anchored to the right.
This part of the IronPython Mono Winforms tutorial was about the layout management of controls. We practised various possibilities that the Winforms library offers.