入力された値の妥当性をチェック
入力値が想定した範囲にない場合に、メッセージダイアログを表示して入力欄を赤く色付けするようにする。
コードの例は以下の通り。
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._textBox1 = System.Windows.Forms.TextBox() self._button1 = System.Windows.Forms.Button() self.SuspendLayout() # # textBox1 # self._textBox1.BackColor = System.Drawing.Color.White self._textBox1.Location = System.Drawing.Point(33, 51) self._textBox1.Name = "textBox1" self._textBox1.Size = System.Drawing.Size(222, 19) self._textBox1.TabIndex = 0 self._textBox1.Text = "1.0" self._textBox1.TextAlign = System.Windows.Forms.HorizontalAlignment.Right # # button1 # self._button1.Location = System.Drawing.Point(180, 113) self._button1.Name = "button1" self._button1.Size = System.Drawing.Size(75, 23) self._button1.TabIndex = 1 self._button1.Text = "button1" self._button1.UseVisualStyleBackColor = True self._button1.Click += self.Button1Click # # MainForm # self.ClientSize = System.Drawing.Size(284, 184) self.Controls.Add(self._button1) self.Controls.Add(self._textBox1) self.Name = "MainForm" self.Text = "test1117" self.ResumeLayout(False) self.PerformLayout() def Button1Click(self, sender, e): val = float(self._textBox1.Text) if val < 0.0: self._textBox1.BackColor = System.Drawing.Color.Red MessageBox.Show("error") else: self._textBox1.BackColor = System.Drawing.Color.White
実行例は以下の通り。