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
Solved

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

Posted on 2011-03-20
10
486 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
MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

 

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

829 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