tags:

views:

25

answers:

1

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.

+1  A: 

That is because you within the loop are using the the string field over and over again, updating its value, until the loop is done, when the last value will be in the field:

//begin loop
// *** here is your problem; there is only one PaintLabel ***
this.PaintLabel = serialno;
Shapes[i].Paint += new PaintEventHandler(ctl_Paint);
// end loop

One solution would be to make PaintLabel into an array with as many elements as there are shapes. Or even easier, make a Dictionary that also holds the reference between shapes and serial no's:

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));
Fredrik Mörk
Thanks, I understand it better now. How do I assign the variable that I want to paint onto the picture box? Do I use a delegate function? I am not sure how to resolve this.
Thomas
As I need to run the paint function for each picture box and draw a different serial number on each one, I guess I would need to pass the paint event handler the serial somehow and not re-use a string as i am currently doing
Thomas
You have the answer in my code sample; the `PaintLabel` is a dictionary, using each `PictureBox` control as key. Inside your paint event handler, you can use the `tmp` variable as the key to fetch the corresponding string from the dictionary (see the `DrawString` sample in my answer).
Fredrik Mörk
Thanks, I didnt see you post a comment here, my apologies. I have tried to implement your solution. but I am getting an error in the paint event. Error -> The best overloaded method match for 'string.this[int]' has some invalid arguments.
Thomas
It doesnt seem to like the PaintLabel[tmp] parameter.
Thomas
Note that `PaintLabel` is of the type `Dictionary<Control, string>`.
Fredrik Mörk