CheckBoxes & RadioButtons

IronPython & Windows Forms, Part VI

Windows Forms

Note

This is part of a series of tutorials on using IronPython with Windows Forms.

Contents

 

 

Introduction

Two common widgets in any programmers toolbox are the radio button and the checkbox.

These are ways of visually providing the user several choices. I'm sure you're all familiar with these forms of input, but the checkbox allows the user to switch individual options on and off, whilst the radio button allows users to choose on of several choices.

From IronPython we can setup these widgets fairly easily. We can interrogate them to find out what state they are in, or set an action to be performed when the user makes a choice. You can use this to dynamically alter the state of the GUI depending on which options are selected.

The two widgets are very similar in use. Let's start with an example of using the CheckBox.

Once again, the code examples in the Microsoft MSDN documentation show us that the constructor for CheckBox takes no arguments :

CheckBox checkBox1 = new CheckBox();

The example below shows several of the useful properties available on the CheckBox.

import clr
clr.AddReference('System.Windows.Forms')
clr.AddReference('System.Drawing')

from System.Drawing import Point
from System.Windows.Forms import Application, Button, CheckBox, Form, Label

class CheckBoxExampleForm(Form):
    def __init__(self):
        self.Text = "CheckBox Example"

        self.Width = 400
        self.Height = 125

        self.label = Label()
        self.label.Text = "Choose the Foods You Like"
        self.label.Location = Point(25, 25)
        self.label.AutoSize = True

        self.check1 = CheckBox()
        self.check1.Text = "Bananas"
        self.check1.Location = Point(25, 50)
        self.check1.Width = 90

        self.check2 = CheckBox()
        self.check2.Text = "Chicken"
        self.check2.Location = Point(125, 50)
        self.check2.Width = 110

        self.check3 = CheckBox()
        self.check3.Text = "Stuffed Peppers"
        self.check3.Location = Point(240, 50)
        self.check3.Width = 120
        self.check3.Checked = True

        self.Controls.Add(self.label)
        self.Controls.Add(self.check1)
        self.Controls.Add(self.check2)
        self.Controls.Add(self.check3)

form = CheckBoxExampleForm()
Application.Run(form)

Notice that this time we used the AutoSize Property of the label to avoid having to explicitly set a size. When we update the text, the label will resize automatically so that its full contents is visible.

We set the third CheckBox to start checked by using the Checked Property. This is a property, so we can 'check' this value to see what state the button is in. Smile

The RadioButton is similarly easy to use.

It is used in the same way as the CheckButton, with a similar set of Members. We will set the first button to be Checked.

In addition to this we will set a method to be called whenever the radio buttons change state. To achieve this we will use the CheckedChange [1] event. We can check the state of the button, because we know that the sender parameter of our event will be the control that has changed.

Anyway, enough explanation and onto the code. To make it a bit more interesting we will mess around with the fonts we use for the last label.

import clr
clr.AddReference('System.Windows.Forms')
clr.AddReference('System.Drawing')

import sys
# To use random you must put the Python
# standard library on the path
sys.path.append('c:\\Python24\\Lib')

from random import random

from System.Drawing import Color, Font, FontStyle, Point
from System.Windows.Forms import (Application, BorderStyle, Button, CheckBox,
    Form, Label, Panel, RadioButton)

class ChecksAndRadiosForm(Form):
    def __init__(self):
        self.Text = "CheckBox Example"

        self.Width = 400
        self.Height = 300

        self.setupCheckButtons()
        self.setupRadioButtons()

        self.Controls.Add(self.checkPanel)
        self.Controls.Add(self.radioPanel)

    def newPanel(self, x, y):
        panel = Panel()
        panel.Width = 400
        panel.Height = 150
        panel.Location = Point(x, y)
        panel.BorderStyle = BorderStyle.Fixed3D
        return panel

    def setupCheckButtons(self):
        self.checkPanel = self.newPanel(0, 0)

        self.checkLabel = Label()
        self.checkLabel.Text = "Choose Your Favourite Food"
        self.checkLabel.Location = Point(25, 25)
        self.checkLabel.AutoSize = True

        self.check1 = CheckBox()
        self.check1.Text = "Bananas"
        self.check1.Location = Point(25, 50)
        self.check1.Width = 90

        self.check2 = CheckBox()
        self.check2.Text = "Chicken"
        self.check2.Location = Point(125, 50)
        self.check2.Width = 110

        self.check3 = CheckBox()
        self.check3.Text = "Stuffed Peppers"
        self.check3.Location = Point(240, 50)
        self.check3.Width = 120

        self.checkPanel.Controls.Add(self.checkLabel)
        self.checkPanel.Controls.Add(self.check1)
        self.checkPanel.Controls.Add(self.check2)
        self.checkPanel.Controls.Add(self.check3)

    def setupRadioButtons(self):
        self.radioPanel = self.newPanel(0, 150)

        self.radioLabel1 = Label()
        self.radioLabel1.Text = "Tell Me Your Gender"
        self.radioLabel1.Location = Point(25, 25)
        self.radioLabel1.AutoSize = True

        self.radio1 = RadioButton()
        self.radio1.Text = "Male"
        self.radio1.Location = Point(25, 50)
        self.radio1.Checked = True
        self.radio1.CheckedChanged += self.checkedChanged

        self.radio2 = RadioButton()
        self.radio2.Text = "Female"
        self.radio2.Location = Point(150, 50)
        self.radio2.CheckedChanged += self.checkedChanged

        self.radio3 = RadioButton()
        self.radio3.Text = "Alien"
        self.radio3.Location = Point(300, 50)
        self.radio3.CheckedChanged += self.checkedChanged

        self.radioLabel2 = Label()
        self.radioLabel2.Text = "Go On, Tell Me"
        self.radioLabel2.Location = Point(25, 80)
        self.radioLabel2.AutoSize = True
        self.radioLabel2.Font = Font("Arial", 15, FontStyle.Bold)
        self.radioLabel2.ForeColor = Color.Red

        self.radioPanel.Controls.Add(self.radioLabel1)
        self.radioPanel.Controls.Add(self.radioLabel2)
        self.radioPanel.Controls.Add(self.radio1)
        self.radioPanel.Controls.Add(self.radio2)
        self.radioPanel.Controls.Add(self.radio3)

    def checkedChanged(self, sender, args):
        if not sender.Checked:
            return
        if sender.Text == "Alien":
            self.radioLabel2.Text = "You're Joking Right ?"
            style = FontStyle.Bold | FontStyle.Italic
            self.radioLabel2.Font = Font("Times", 15, style)
            self.radioLabel2.ForeColor = Color.FromArgb(int(random() * 256),
                                                        int(random() * 256),
                                                        int(random() * 256))
        else:
            self.radioLabel2.Text = "Fair Enough"
            self.radioLabel2.Font = Font("Courier", 15, FontStyle.Regular)
            self.radioLabel2.ForeColor = Color.Red

form = ChecksAndRadiosForm()
Application.Run(form)

On Windows it looks like this :

When this example launches

If you tell it you are an alien, it does something like this :

It doesn't believe you are an alien

The RadioButton stuff is straightforward, what is new is the way we have used fonts and colours.

We initialise a new font using the Font class. From the Font Constructor docs you can see that there are several different ways we can initialise it.

The two we use are :

  1. Font (String, Single)

    This initialises a new font from the name and the size.

  2. Font (String, Single, FontStyle)

    This initialises a new font from the name, the size and a style.

In the first case we just supply a font name and the size. The constructor signature call the type of object used for the size a Single. In the C# example you can see that it is specified as a float. IronPython isn't fussy and lets us use an integer.

In the second case (used in ChecksAndRadiosForm.checkedChanged) we specify a FontStyle as well. FontStyle is an enumeration with various different members to represent the different styles :

  • Bold - Bold text.
  • Italic - Italic text.
  • Regular - Normal text.
  • Strikeout - Text with a line through the middle.
  • Underline - Underlined text.

If we want to combine them, we can use the or operator |. This returns a new FontStyle with several styles selected.

The following code would produce bold, italic underlined text :

style = FontStyle.Bold | FontStyle.Italic | FontStyle.Underline

You can't change the style of a font. The Style Property is read only. So is it's size, which you can check using the SizeInPoints property. If you want to change the style of text, you will have to create a new font and replace the old one.

We've also used a neat trick with our old friend Color. Instead of using a predefined color, we've created one using the Color.FromArgb Method. Again this has several constructors. We've passed in three integers representing the red, green and blue components. Smile

Right, that was easy. I think we know enough to learn a more sensible way of laying out our controls; so onto the next entry...

[1]Try saying that with a mouthful of cheese.

For buying techie books, science fiction, computer hardware or the latest gadgets: visit The Voidspace Amazon Store.

Hosted by Webfaction

Return to Top

Page rendered with rest2web the Site Builder

Last edited Fri Nov 27 18:32:35 2009.

Counter...