c# - Filling an array with pictureboxes -


i have array of picture boxes created by:

picturebox[] places = new picturebox[100]; 

i need fill picture boxes have in form. there way programmatically fill out array or need use:

places[0] = picturebox1; ... 

in first example making assumption want put pictureboxes array in order created picturebox1 = places[0]; etc. second example assigns order placed in array using tag property index, way use add controls array.

first method

private void button1_click(object sender, eventargs e) {     var places = new picturebox[10]; // used 10 test     (int = 0; < places.length; i++)     {         // work, searches through control collection find         // picturebox of requested name. fragile in fact the         // naming has exact.         try         {             places[i] = (picturebox)controls.find("picturebox" + (i + 1).tostring(), true)[0];         }         catch (indexoutofrangeexception)         {             messagebox.show("picturebox" + (i + 1).tostring() + " not exist!");         }      } } 

second method

private void button2_click(object sender, eventargs e) {     // example using tag property index     // keep in mind index 1 less      // total number of pictureboxes make sure      // array sized correctly.      var places = new picturebox[100];      int index;     foreach (var item in controls )     {         if (item picturebox)         {             picturebox pb = (picturebox)item;             if (int.tryparse(pb.tag.tostring(), out index))             {                 places[index] = pb;             }         }     }  } 

Comments

Popular posts from this blog

basic authentication with http post params android -

vb.net - Virtual Keyboard commands -

css - Firefox for ubuntu renders wrong colors -