リストボックスで項目を選択したときの処理
リストボックスで項目を選択したときの処理をどう書くか。リストボックスの項目を選択したときのイベントを登録して、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._listBox1 = System.Windows.Forms.ListBox() self._label1 = System.Windows.Forms.Label() self.SuspendLayout() # # listBox1 # self._listBox1.FormattingEnabled = True self._listBox1.ItemHeight = 12 self._listBox1.Items.AddRange(System.Array[System.Object]( ["Car", "Truck", "OpenCar", "Taxi", "Minicar", "SportsCar"])) self._listBox1.Location = System.Drawing.Point(37, 35) self._listBox1.Name = "listBox1" self._listBox1.Size = System.Drawing.Size(120, 76) self._listBox1.TabIndex = 0 # *** イベントの登録 *** self._listBox1.SelectedIndexChanged += self.ListBox1SelectedIndexChanged # # 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(37, 138) self._label1.Name = "label1" self._label1.Size = System.Drawing.Size(133, 23) self._label1.TabIndex = 1 self._label1.Text = """None is selected. """ # # MainForm # self.ClientSize = System.Drawing.Size(284, 188) self.Controls.Add(self._label1) self.Controls.Add(self._listBox1) self.Name = "MainForm" self.Text = "test" self.ResumeLayout(False) def ListBox1SelectedIndexChanged(self, sender, e): self._label1.Text = sender.Text