Basic Controls in Mono Winforms
This part of the IronPython Mono Winforms programming tutorial will be about basic controls.
Winforms controls are basic building blocks of an application. Winforms has a wide range of various controls. Buttons, check boxes, trackbars, labels etc. Everything a programmer needs for his job. In this section of the tutorial, we will describe several useful controls.
Label Control
Label is a simple control for displaying text
or images. It does not receive focus.
#!/usr/bin/ipy
import sys
import clr
clr.AddReference("System.Windows.Forms")
clr.AddReference("System.Drawing")
from System.Windows.Forms import Application, Form, Label
from System.Drawing import Size, Point, Font
text = """Meet you downstairs in the bar and heard
Your rolled up sleeves and your skull t-shirt
You say why did you do it with him today?
And sniffed me out like I was tanqueray
Cause you're my fella, my guy
Hand me your stella and fly
By the time I'm out the door
You tear me down like roger moore
I cheated myself
Like I knew I would
I told ya, I was trouble
You know that I'm no good
Upstairs in bed, with my ex boy
He's in a place, but I cant get joy
Thinking of you in the final throws, this is when my buzzer goes"""
class IForm(Form):
def __init__(self):
self.Text = "You know I'm No Good"
font = Font("Serif", 10)
lyrics = Label()
lyrics.Parent = self
lyrics.Text = text
lyrics.Font = font
lyrics.Location = Point(10, 10)
lyrics.Size = Size(290, 290)
self.CenterToScreen()
Application.Run(IForm())
In our example, we show some lyrics on the form.
lyrics = Label()
Label control is created.
text = """Meet you downstairs in the bar and heard
... """
This is our text.
font = Font("Serif", 10)
...
lyrics.Font = font
The font of the text of the label is set to 10px Serif.
CheckBox
CheckBox is a control that has two states: on and off.
It is a box with a label or an image. If the CheckBox is checked,
it is represented by a tick in a box. A CheckBox can be used to
show or hide splashscreen at startup, toggle visibility of a toolbar etc.
#!/usr/bin/ipy
import clr
clr.AddReference("System.Windows.Forms")
clr.AddReference("System.Drawing")
from System.Windows.Forms import Application, Form, CheckBox
from System.Drawing import Size, Point
class IForm(Form):
def __init__(self):
self.Text = "CheckBox"
self.Size = Size(220, 170)
cb = CheckBox()
cb.Parent = self
cb.Location = Point(30, 30)
cb.Text = "Show Title"
cb.Checked = True
cb.CheckedChanged += self.OnChanged
self.CenterToScreen()
def OnChanged(self, sender, event):
if sender.Checked:
self.Text = "CheckBox"
else:
self.Text = ""
Application.Run(IForm())
Our code example shows or hides the title of the window depending on its state.
cb = CheckBox()
CheckBox control is created.
cb.Text = "Show Title" cb.Checked = True
When the application starts, we show the title. And we set the
CheckBox control to checked state.
cb.CheckedChanged += self.OnChanged
When we click on the CheckBox control,
the CheckedChanged event is triggered.
if sender.Checked:
self.Text = "CheckBox"
else:
self.Text = ""
Here we toggle the title of the window.
TrackBar
TrackBar is a component that lets the user graphically
select a value by sliding a knob within a bounded interval.
Our example will show a volume control.
#!/usr/bin/ipy
import sys
import clr
clr.AddReference("System.Windows.Forms")
clr.AddReference("System.Drawing")
from System.Windows.Forms import Application, Form, PictureBox
from System.Windows.Forms import TrackBar, TickStyle
from System.Drawing import Size, Point, Bitmap
class IForm(Form):
def __init__(self):
self.Text = 'TrackBar'
self.Size = Size(260, 190)
tb = TrackBar()
tb.Parent = self
tb.Size = Size(150, 30)
tb.Location = Point(30, 50)
tb.TickStyle = TickStyle.None
tb.SetRange(0, 100)
tb.ValueChanged += self.OnChanged
self.LoadImages()
self.pb = PictureBox()
self.pb.Parent = self
self.pb.Location = Point(210, 50)
self.pb.Image = self.mutep
self.CenterToScreen()
def LoadImages(self):
try:
self.mutep = Bitmap("mute.png")
self.minp = Bitmap("min.png")
self.medp = Bitmap("med.png")
self.maxp = Bitmap("max.png")
except Exception, e:
print "Error reading images"
print e.msg
sys.exit(1)
def OnChanged(self, sender, event):
val = sender.Value
if val == 0:
self.pb.Image = self.mutep
elif val > 0 and val <= 30:
self.pb.Image = self.minp
elif val > 30 and val < 80:
self.pb.Image = self.medp
else: self.pb.Image = self.maxp
Application.Run(IForm())
In the code example, we show a TrackBar
and a PictureBox. By dragging the track bar,
we change the image on the PictureBox control.
tb = TrackBar()
TrackBar control is created.
tb.TickStyle = TickStyle.None
We show no ticks for this TrackBar.
self.pb = PictureBox() ... self.pb.Image = self.mutep
PictureBox control is created. It is used to display an image.
At the start, it shows the mute image.
self.mutep = Bitmap("mute.png")
self.minp = Bitmap("min.png")
self.medp = Bitmap("med.png")
self.maxp = Bitmap("max.png")
Here we create four images that we will use.
val = sender.Value
if val == 0:
self.pb.Image = self.mutep
elif val > 0 and val <= 30:
self.pb.Image = self.minp
elif val > 30 and val < 80:
self.pb.Image = self.medp
else: self.pb.Image = self.maxp
We determine the value of the TrackBar.
Depending on its value, we update the PictureBox control.
ComboBox
ComboBox is a control that combines
a button or editable field and a drop-down list.
The user can select a value from the drop-down list,
which appears at the user's request. If you make the combo
box editable, then the combo box includes an editable field
into which the user can type a value.
#!/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 ComboBox, Label
from System.Drawing import Size, Point
class IForm(Form):
def __init__(self):
self.Text = "ComboBox"
self.Size = Size(240, 240)
cb = ComboBox()
cb.Parent = self
cb.Location = Point(50, 30)
cb.Items.AddRange(("Ubuntu",
"Mandriva",
"Red Hat",
"Fedora",
"Gentoo"))
cb.SelectionChangeCommitted += self.OnChanged
self.label = Label()
self.label.Location = Point(50, 140)
self.label.Parent = self
self.label.Text = "..."
self.CenterToScreen()
def OnChanged(self, sender, event):
self.label.Text = sender.Text
Application.Run(IForm())
Our code programming example shows a combobox with five items. The selected item is shown in a label control.
cb = ComboBox()
ComboBox control is created.
cb.Items.AddRange(("Ubuntu",
"Mandriva",
"Red Hat",
"Fedora",
"Gentoo"))
The ComboBox control is filled with items.
cb.SelectionChangeCommitted += self.OnChanged
If we select an item from the combobox, the SelectionChangeCommitted event
is triggered.
def OnChanged(self, sender, event):
self.label.Text = sender.Text
Here the selected text from the combobox is copied to the label.
We have finished chapter of the IronPython Mono Winforms tutorial, dedicated to basic controls.