.NET General

Moderators: None (Apply to moderate this forum)
Number of threads: 797
Number of posts: 1359

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
UnRecognized Picture Boxes Posted by meetvissuin on 1 May 2009 at 12:30 AM



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?
Report
Re: UnRecognized Picture Boxes Posted by Psightoplazm on 1 May 2009 at 9:07 AM
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:
        [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();

        }


></\/~Psightoplasm`~
Report
Re: UnRecognized Picture Boxes Posted by meetvissuin on 1 May 2009 at 10:47 PM
Hi thanks for ur reply.

I didnt use delegates till now,so after looking ur code i am now concentrating on them.



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.