Passing the name of a component and use it to do something to that component

I

Ivan Sammut

Hi,

I have an form with 64 picture boxes named pic1 ... pic64.

Then I have a procedure which create a bitmap filled with red and then I
place it in the PictureContainer. Now I would like to know if it is possible
to pass to this procedure name of the picture box in which to place the
bitmap

Example (this does not work)
string sTxt = "pic1";
Bitmap bmp1 = new Bitmap(32,32);
(PictureBox)sTxt.Image=bmp1; <<Error Here

Any idea if it is possible to achieve this.

Regards
Ivan Sammut
 
I

Ignacio Machin \( .NET/ C# MVP \)

hi,

Each control has a Name property, you can search the Controls collection
for the correct Name value.

foreach( Control control in Controls)
if ( control.Name == theName )
( (PictureBox)control).Image = bmp1;


cheers,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top