ラジオボタンを押したときの処理
IronPythonの勉強。
ラジオボタンを配置する。どのように配置するかということと、ボタンが押されたときの処理をどう書くか、がここでの課題。
コントロールの配置はデザイン画面で行うが、RadioButtonをただ並べて置くのではなく、必ずGroupBox内に配置する。どのボタンが押されたかの判定は、イベントハンドラにおいてsender.Textによって押されたボタンのテキストが取得できるので、それで行うことができる。
import System.Drawing import System.Windows.Forms from System.Drawing import * from System.Windows.Forms import * class MainForm(Form): def __init__(self): self.InitializeComponent() def InitializeComponent(self): self._label1 = System.Windows.Forms.Label() self._groupBox1 = System.Windows.Forms.GroupBox() self._radioButton1 = System.Windows.Forms.RadioButton() self._radioButton2 = System.Windows.Forms.RadioButton() self._groupBox1.SuspendLayout() self.SuspendLayout() # # label1 # self._label1.Font = System.Drawing.Font("MS UI Gothic", 12, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, 128) self._label1.Location = System.Drawing.Point(44, 150) self._label1.Name = "label1" self._label1.Size = System.Drawing.Size(198, 23) self._label1.TabIndex = 1 self._label1.Text = "This is a sentence." # # groupBox1 # self._groupBox1.Controls.Add(self._radioButton2) self._groupBox1.Controls.Add(self._radioButton1) self._groupBox1.Location = System.Drawing.Point(42, 29) self._groupBox1.Name = "GroupBox1" self._groupBox1.Size = System.Drawing.Size(200, 100) self._groupBox1.TabIndex = 2 self._groupBox1.TabStop = False self._groupBox1.Text = "GroupBox" # # radioButton1 # self._radioButton1.Location = System.Drawing.Point(43, 27) self._radioButton1.Name = "radioButton1" self._radioButton1.Size = System.Drawing.Size(104, 24) self._radioButton1.TabIndex = 0 self._radioButton1.TabStop = True self._radioButton1.Text = "Radio-button-1" self._radioButton1.UseVisualStyleBackColor = True self._radioButton1.Click += self.rb_Click # # radioButton2 # self._radioButton2.Location = System.Drawing.Point(43, 58) self._radioButton2.Name = "radioButton2" self._radioButton2.Size = System.Drawing.Size(104, 24) self._radioButton2.TabIndex = 1 self._radioButton2.TabStop = True self._radioButton2.Text = "Radio-button-2" self._radioButton2.UseVisualStyleBackColor = True self._radioButton2.Click += self.rb_Click # # MainForm # self.ClientSize = System.Drawing.Size(284, 188) self.Controls.Add(self._groupBox1) self.Controls.Add(self._label1) self.Name = "MainForm" self.Text = "test" self._groupBox1.ResumeLayout(False) self.ResumeLayout(False) def rb_Click(self, sender, e): if sender.Text == "Radio-button-1": self._label1.Text = "First button selected." elif sender.Text == "Radio-button-2": self._label1.Text = "Second button selected."