Solved

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

Posted on 2011-03-20
10
491 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 100 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 150 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 100 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
The Orion Papers

Are you interested in becoming an AWS Certified Solutions Architect?

Discover a new interactive way of training for the exam.

 

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 50 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 100 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

Enroll in June's Course of the Month

June’s Course of the Month is now available! Experts Exchange’s Premium Members, Team Accounts, and Qualified Experts have access to a complimentary course each month as part of their membership—an extra way to sharpen your skills and increase training.

Question has a verified solution.

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

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…

719 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