在 KeyDown 事件中需判斷 Enter 鍵並操作傳入的參數 short i,但事件簽名無法直接加入額外參數。
// 綁定事件時將參數存入 Tag
textBox1.Tag = (short)5; // 假設 i = 5
textBox1.KeyDown += Mission_KeyDown;
// 事件處理
private void Mission_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
TextBox tb = sender as TextBox;
if (tb != null && tb.Tag is short i)
{
// 對 i 進行操作
i += 1;
tb.Tag = i; // 寫回
MessageBox.Show("參數已更新:" + i);
}
}
}
CommandParameter 傳遞參數。透過 Tag 屬性是最簡單且符合 WinForms 慣用模式的做法,避免修改事件簽名導致無法綁定。