1 回答

TA貢獻1801條經驗 獲得超16個贊
這是因為在示例代碼中,sender 被傳遞,而不是傳遞 s。另外你應該傳入 e1 而不是 e。
這將給出以下代碼:
pb.Click += new EventHandler((s, e1) => this.PictureBox1_Click(s, e1, tempValue))
您還可以使用閉包,而不是將點擊處理代碼提取到單獨的函數中,它可以全部內聯完成。
例如:
private void Form1_Load(object sender, EventArgs e)
{
daoConn dc = new daoConn();
List<Model> models = new List<Model>();
string url;
string fName;
models = dc.GetAllModels();
foreach (Model m in models)
{
int tempValue;
tempValue = 1;
PictureBox pb = new PictureBox();
url = baseUrl + m.MhsUrl;
fName = m.mFirstName;
Size size = new Size(100, 100);
pb.ImageLocation = url;
pb.Size = size;
pb.Click += (s,clickEvent) => {
// You can use of pb directly here
// You also have access to things like models too.
string j = pb.ImageLocation;
MfNameTxt.Text = "Chris";
MessageBox.Show("Clicked! " + j);
};
pb.SizeMode = PictureBoxSizeMode.Zoom;
modelHsFlowLayout.Controls.Add(pb);
}
}
- 1 回答
- 0 關注
- 104 瀏覽
添加回答
舉報