チェックボックスの使い方
チェックボックスがオンになるときとオフになるときで、どのようにコードを書いたら処理が実装できるのか?つまり、オンになるとき、オフになるときをどのように表現するのか。
コードの例を下に示す。最後尾のCheckBox1CheckedChangedが、チェックボックスのイベントハンドラになる。
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._checkBox1 = System.Windows.Forms.CheckBox() self._label1 = System.Windows.Forms.Label() self.SuspendLayout() # # checkBox1 # self._checkBox1.Font = System.Drawing.Font("MS UI Gothic", 12, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, 128) self._checkBox1.Location = System.Drawing.Point(42, 53) self._checkBox1.Name = "checkBox1" self._checkBox1.Size = System.Drawing.Size(205, 24) self._checkBox1.TabIndex = 0 self._checkBox1.Text = "checkBox demo" self._checkBox1.UseVisualStyleBackColor = True self._checkBox1.CheckedChanged += self.CheckBox1CheckedChanged # # 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(42, 125) self._label1.Name = "label1" self._label1.Size = System.Drawing.Size(198, 23) self._label1.TabIndex = 1 self._label1.Text = "This is a sentence." # # MainForm # self.ClientSize = System.Drawing.Size(284, 182) self.Controls.Add(self._label1) self.Controls.Add(self._checkBox1) self.Name = "MainForm" self.Text = "test" self.ResumeLayout(False) def CheckBox1CheckedChanged(self, sender, e): if self._checkBox1.Checked: self._label1.Text = "CheckBox is checked." else: self._label1.Text = "CheckBox is not checked."
実行例のスクリーンショットを以下に示す。