Solved

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

Posted on 2011-03-20
10
485 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
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 29

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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 

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 85

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 29

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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

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…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

773 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