Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

How to refer 2 PictureBox control objects in C# Code?

Posted on 2011-03-20
10
Medium Priority
?
499 Views
Last Modified: 2012-05-11
Can they be placed in an array? How to refer them if the control names are pb1, pb2 in Properties In Design Mode? How to disappear them in C# Code?
0
Comment
Question by:jl66
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
10 Comments
 
LVL 8

Assisted Solution

by:crysallus
crysallus earned 400 total points
ID: 35177768
If you are writing code within the form object that contains them, then you can simply refer to them by name. Eg.

pb1.Enabled = true;

Open in new window

I normally like to prepend form controls with "this.", as it identifies it as belonging to the form, and I find it helps with intellisence in vs, though it's not necessary. eg. this.pb1. etc.

If you need to access them in other classes, you'll need to pass the reference pb1 or pb2 to those objects via constructor or method arguments. Or you can simply pass the form that contains it, and access the pictureboxes through the form object.

If you want to make the picture box no longer visible, but still existing on the form, the picture box has a visiible property you can set to false, i.e.:

pb1.Visible = false;

Open in new window

0
 
LVL 27

Accepted Solution

by:
nmarun earned 600 total points
ID: 35177777
Here's how I'd do it:

Say you have pb1, pb2, pb3 picture boxes on your designer. When the form loads, you might want to do something like the snippet below:

Arun
using System.Collections.Generic;
List<PictureBox> pictureBoxes = new List<PictureBox>();
pictureBoxes.Add(pb1);
pictureBoxes.Add(pb2);
pictureBoxes.Add(pb3);

for(int i = 0; i < pictureBoxes.Count; i++)
{
     // not sure which one of these would work!
     pictureBoxes[i].Visible = false;
     pictureBoxes[i].Hide();;
}

Open in new window

0
 
LVL 30

Assisted Solution

by:anarki_jimbel
anarki_jimbel earned 400 total points
ID: 35177821
I agree to nmarun about List of picture boxes.

But the task is not clear.

It looks like you want to access these picture boxes from outside the form they are located on.

In this case you need to create a property in this form class that returns these picture boxes,

Like:

public PictureBox firstPB
{
   get{
        return pb1;
   }
}

Or, alternatively you return a List of PictureBox objects, and access them by index from list. Something like below:

// Form class
public List<PictureBox> allPictureBoxes
{
   get{
        return pictureBoxes;//you create them in constructor, e.g.
   }
}

// other form that uses these PBs:

List<PictureBox> pbs = myForm.allPictureBoxes();
PictureBox firstPB = pbs.Items[0];

Open in new window

0
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 

Author Comment

by:jl66
ID: 35177918
Thank all of you very much. That is what I want to know. One quesdton is left:
For this,
pictureBoxes.Add(pb1);
pictureBoxes.Add(pb2);
pictureBoxes.Add(pb3);

If I want to use a for loop like

for ( int i=1; i<3; i++)
  pictureBoxes.Add(pb???);

how should I do this?? Do I need cast the type first?
0
 
LVL 12

Assisted Solution

by:starlite551
starlite551 earned 200 total points
ID: 35177930
You can use an ArrayList object to store PictureBox Controls in object form or List<PictureBox> collections.. Incase of an ArrayList you'll require a casting to convert the objects to PictureBoxes.. Incase of List<> type collection they are in their original form so no need for casting.. I`ll show you both the examples as follows :
// PictureBoxes Using ArrayList..

//Create an object of an ArrayList first..
ArrayList aListPicBox = new ArrayList();

//Then you add picture box objects to the ArrayList using Add() method..
PictureBox pb1 = new PictureBox();
PictureBox pb2 = new PictureBox();
PictureBox pb3 = new PictureBox();

aListPicBox.Add(pb1);
aListPicBox.Add(pb2);
aListPicBox.Add(pb3);

//You can then retrieve the PictureBoxes individually using index positions in the ArrayList you need casting here..

PictureBox pb1 = (PictureBox)aListPicBox[0];
PictureBox pb2 = (PictureBox)aListPicBox[1];
PictureBox pb3 = (PictureBox)aListPicBox[2];

Open in new window

//2nd Example using List<> type collections..

List<PictureBox> picList = new List<PictureBox>();

PictureBox pb1 = new PictureBox();
PictureBox pb2 = new PictureBox();
PictureBox pb3 = new PictureBox();

picList.Add(pb1);
picList.Add(pb2);
picList.Add(pb3);

//To retrieve the PictureBoxes you dont need Casting here..
PictureBox pb1 = picList[0];
PictureBox pb2 = picList[1];
PictureBox pb3 = picList[2];

Open in new window

0
 

Author Comment

by:jl66
ID: 35177972
starlite551: Thank you so much for quick response. The point is that
If I have 60 pictureboxes, should I have to list 60 lines like

pictureBoxes.Add(pb1);
....
or
PictureBox pb1 = picList[0];
....

Is there any way to use a FOR loop and play with the number 1,2, ....???
0
 
LVL 86

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 400 total points
ID: 35178022
You can do:
public partial class Form1 : Form
    {

        private List<PictureBox> pictureBoxes = new List<PictureBox>();

        private void Form1_Load(object sender, EventArgs e)
        {
            Control[] matches;
            for (int i = 1; i <= 60; i++)
            {
                matches = this.Controls.Find("pb" + i.ToString(), true);
                if ((matches.Length > 0) && (matches[0] is PictureBox))
                {
                    pictureBoxes.Add((PictureBox)matches[0]);
                }   
            }
        }

    }

Open in new window

0
 

Author Comment

by:jl66
ID: 35178085
Idle_Mind:thanks a lot. It works.
0
 

Author Closing Comment

by:jl66
ID: 35178100
Excellent. I wish I had more points for everyone.
0
 
LVL 30

Expert Comment

by:anarki_jimbel
ID: 35178114
Or may be even easier - to iterate through all controls and add them to a List if control type is a listbox. In this case we don't really need to worry about names. But it depends on your specific task.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question