Windows Develop Bookmark and Share   
 index > Windows Forms General > Cannot Access my pictureboxs!
 

Cannot Access my pictureboxs!

I have a form for Juxtaposing two images, it has a TabControl, whenever I add a TabPage, a SplitContainer is added to the page, then two PictureBoxes are added on both sides of the SplitContainer, then two labels are added one each side. The problem is I don't know how to access my PictureBoxes.

        private void tabControl1_ControlAdded(object sender, ControlEventArgs e)
        {
            if (e.Control is TabPage)
            {
                //Add split container
                var split = new SplitContainer();
                split.Dock = DockStyle.Fill;
                split.SplitterDistance = 75;
                split.AutoScaleMode = AutoScaleMode.Inherit;
                split.Width = 10;
                split.IsSplitterFixed = true;
                e.Control.Controls.Add(split);
                //add left picturepox to splitcontainer
                var pb1 = new PictureBox();
                pb1.BackColor = Color.White;
                pb1.ImageLocation = "http://www.defensetech.org/images/df-21.jpg";
                pb1.SizeMode = PictureBoxSizeMode.Zoom;
                split.Panel1.Controls.Add(pb1);
                pb1.Dock = DockStyle.Fill;
                pb1.DoubleClick += new EventHandler(pb1_DoubleClick);
                //add right picturebox to splitcontainer
                var pb2 = new PictureBox();
                pb2.BackColor = Color.White;
                pb2.ImageLocation = "http://www.evemariebridalveils.com/JHJ2019.jpg";
                pb2.SizeMode = PictureBoxSizeMode.Zoom;
                split.Panel2.Controls.Add(pb2);
                pb2.Dock = DockStyle.Fill;
                pb2.DoubleClick +=new EventHandler(pb2_DoubleClick);
                //add before label
                var beforeLbl = new Label();
                beforeLbl.AutoSize = false;
                beforeLbl.TextAlign = ContentAlignment.MiddleCenter;
                beforeLbl.Font = new Font("Tahoma", 15f, FontStyle.Bold);
                beforeLbl.Text = "BEFORE";
                beforeLbl.Dock = DockStyle.Bottom;
                split.Panel1.Controls.Add(beforeLbl);
                //add after label
                var afterLbl = new Label();
                afterLbl.AutoSize = false;
                afterLbl.Font = new Font("Tahoma", 15f, FontStyle.Bold);
                afterLbl.Text = "AFTER";
                afterLbl.TextAlign = ContentAlignment.MiddleCenter;
                afterLbl.Dock = DockStyle.Bottom;
                split.Panel2.Controls.Add(afterLbl);
            }
        }

http://code.google.com/p/notepadenhanced/downloads/list
  • Moved byTaylorMichaelLMVPMonday, October 05, 2009 2:03 PMWinForms related (From:Visual C# General)
  •  
GOBS13  Monday, October 05, 2009 11:53 AM
Move the pb1 and pb2 variables outside of the method so you'll have them as class members.

Hans Passant.
nobugz  Monday, October 05, 2009 12:14 PM

Hi,

the code below returns you the Collection of controls by name at anywhere in code

this.FindForm().Controls.Find("controlName", true);

And to cast it to picturebox you can use

((PictureBox)this.FindForm().Controls.Find("controlName", true)[0]);

Tamer Oz  Monday, October 05, 2009 12:15 PM

You can use google to search for other answers

Custom Search

More Threads

• arg - this exception makes no sense to me!
• Windows Server 2K3
• Revert back focus set by SetForegroundWindow()
• IME Mode Setting Progrmatically
• Webbrowser problem with body.innerhtml
• Problem in displaying the message box always on top
• how to make use of listbox itemdata property in vb.net window application
• Create a 'form' that is interactive on the screen and printable...
• SqlClient error
• how to get other behavior in a textbox when it is focused