tags:

views:

635

answers:

2

This is all I have so far. :P

tabControl1.TabPages[0].???

I'm stuck. I have a pictureBox inside of TabPage1 of my TabControl. How can I, say, change the .image location with code and not the Properties pane.

+1  A: 

Although the controls appear inside a container (as a TabControl), they're all defined on the form, so there is no need to access them through the container.

Instead of:


tablControl1.TabPages[0].MyContainedControl...

Simply type:


MyContainedControl...
Alfred Myers
Oh lol. You genius you. Thanks. :D
Sergio Tapia
+1  A: 

Unless you've set GenerateMember to false on the picture box or you're building the form dynamically you should be able to reference the picture box by its name:

pictureBox1.ImageLocation = "...";

Otherwise, assuming the picture box is the first control in the first tab page you can use the Controls collection:

var picBox = (PictureBox) tabControl1.TabPages[0].Controls[0];
picBox.ImageLocation = "...";

If you know there is exactly one picture box somewhere but you're not sure what page it's on or where on that page it is you can use Linq:

var picBox = tabControl1.TabPages.Cast<Control>()
    .SelectMany(page => page.Controls.OfType<PictureBox>())
    .First();
picBox.ImageLocation = "...";
Nathan Baulch