Solved

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

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

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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 

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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
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…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

708 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now