这是因为您在循环中反复使用字符串字段,更新其值,直到循环完成,最后一个值将出现在字段中:
//begin loop
// *** here is your problem; there is only one PaintLabel ***
this.PaintLabel = serialno;
Shapes[i].Paint += new PaintEventHandler(ctl_Paint);
// end loop
PaintLabel
一个数组,包含的元素和形状的数目一样多。或者更简单,做一个
Dictionary
var PaintLabels = new Dictionary<Control, string>();
//begin loop
PaintLabels.Add(Shapes[i], serialno);
Shapes[i].Paint += new PaintEventHandler(ctl_Paint);
// end loop
// in the paint event
e.Graphics.DrawString(PaintLabel[tmp], myFont, Brushes.LightYellow, new Point(62, 2));