I am creating a few pictureboxes dynamically, then assigning the following:
// class variable
public String PaintLabel;
// private void Form2_Load(object sender, EventArgs e)
//begin loop
this.PaintLabel = serialno;
Shapes[i].Paint += new PaintEventHandler(ctl_Paint);
// end loop
// my event override
private void ctl_Paint(object sender, PaintEventArgs e)
{
Control tmp = (Control)sender;
using (Font myFont = new Font("Arial", 9, FontStyle.Bold))
{
e.Graphics.DrawString(this.PaintLabel, myFont, Brushes.LightYellow, new Point(62, 2));
} // using (Font myFont = new Font("Arial", 10))
} // private void ctl_Paint(object sender, EventArgs e)
It is supposed to create the picture boxes and write a different serial number on each one. But it ends up writing the last serial number found on all of the picture boxes
EDIT:
Ok, your solution is very advanced for me. But I have tried to understand it.
I have added your piece of code into mine.
Then changed my picture box array as follows
MyControl[] Shapes = new MyControl[Num_Picbox];
In my loop I then did the following
Shapes[i].SerialNumber = serialno;
Shapes[i].Paint += new PaintEventHandler(ctl_Paint);
But when I compile and run the code it doesnt draw any serial number on the picturebox.
RESOLUTION:
Thank your for all your help. I changed your
var PaintLabels = new Dictionary<Control, string>();
to
Dictionary<Control, string> PaintLabels = new Dictionary<Control, string>();
Which sorted it out, the paint event couldnt see the local variable.