Howdy, Stranger!

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

Sign In with Facebook Sign In with Google Sign In with OpenID

Categories

We have migrated to a new platform! Please note that you will need to reset your password to log in (your credentials are still in-tact though). Please contact lee@programmersheaven.com if you have questions.
Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

UnRecognized Picture Boxes

meetvissuinmeetvissuin Posts: 3Member
[size=2][color=Green][/color][/size]


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?

Comments

  • PsightoplazmPsightoplazm Posts: 332Member ✭✭
    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:
    [code]
    [STAThread]
    public static void Main()
    {
    var win = new Form() { ClientSize = new Size(1000, 100)};
    var pnl = new Panel()
    {
    Dock = DockStyle.Fill,
    AutoScroll = true,
    };
    win.Controls.Add(pnl);

    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,
    };
    pnl.Controls.Add(pct);
    pct.BringToFront();
    pct.Click +=
    delegate
    {
    // 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);
    };
    }

    win.ShowDialog();

    }

    [/code]
    ><//~Psightoplasm`~
  • meetvissuinmeetvissuin Posts: 3Member
    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.