UnRecognized Picture Boxes


Hi I am trouble shooting a small issue in c#.net 2003 where i am creating picture box array dynamically and assigning the pictures dynamically.

i am doing this in a panel with autoscroll enabled.

so my picture boxes are having the names like picB[1],picB[2],.....
But if i want to use this names in another event i couldn't do as these are the names will be recognized dynamically by compiler but i am reffering these names when writing code.

so finally i couldn't run the application .

can u give me the solution or better alternatives?


  • Well, it would help if you would give more specific examples of what events you want to tap into and what you want to do with them, but you can actually also assign event handlers dynamically using delegates:

    try this:
    public static void Main()
    var win = new Form() { ClientSize = new Size(1000, 100)};
    var pnl = new Panel()
    Dock = DockStyle.Fill,
    AutoScroll = true,

    for (int i = 0; i < 1000; i++)
    var pct = new PictureBox()
    Dock = DockStyle.Left,
    Image = new Bitmap(100, 100),
    BorderStyle = BorderStyle.FixedSingle,
    Name = "PictureBox" + i,
    pct.Click +=
    // this delegate will retain the reference of the current pct instance
    // So any code you write in this delegate will handle each itteration
    // of pct seperately from each other.

    // You can also make a class that inherits eventargs and adds a property
    // that will allow you to pass the current instance of pct to an actual event
    // handler that is tapped into from outside your form class or wherever this
    // code may reside. Then any handlers you add will just use the referenced
    // picturebox.

    MessageBox.Show("The name of this control is " + pct.Name);



  • Hi thanks for ur reply.

    I didnt use delegates till now,so after looking ur code i am now concentrating on them.
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!