handling control array in runtime
-
i'm trying to implement control array in my app. the no of control will depend on user's requirement. total number will be gathered from database. therefore i had to make the code within a method.
private void CreatePictureBoxes() { .... .... PictureBox[] pcbMem = new PictureBox[intCount]; Label[] lblMem = new Label[intCount]; .... }
since i'm doing the whole code in a method i can't access those controls outside of that method. it's also not possible for me to create them in class level as the total index is unkown to me initially. i don't know how to reasign the length of an array so it's being a problem for me. so please help me out to creating the controls so that i can use them from anywhere. -
i'm trying to implement control array in my app. the no of control will depend on user's requirement. total number will be gathered from database. therefore i had to make the code within a method.
private void CreatePictureBoxes() { .... .... PictureBox[] pcbMem = new PictureBox[intCount]; Label[] lblMem = new Label[intCount]; .... }
since i'm doing the whole code in a method i can't access those controls outside of that method. it's also not possible for me to create them in class level as the total index is unkown to me initially. i don't know how to reasign the length of an array so it's being a problem for me. so please help me out to creating the controls so that i can use them from anywhere.vatzcar wrote:
it's also not possible for me to create them in class level as the total index is unkown to me initially
Just declare them at class level and instanciate them inside the method.
PictureBox[] pcbMem;
Label[] lblMem;private void CreatePictureBoxes()
{
....
....
this.pcbMem = new PictureBox[intCount];
this.lblMem = new Label[intCount];
....
}
"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook
-
i'm trying to implement control array in my app. the no of control will depend on user's requirement. total number will be gathered from database. therefore i had to make the code within a method.
private void CreatePictureBoxes() { .... .... PictureBox[] pcbMem = new PictureBox[intCount]; Label[] lblMem = new Label[intCount]; .... }
since i'm doing the whole code in a method i can't access those controls outside of that method. it's also not possible for me to create them in class level as the total index is unkown to me initially. i don't know how to reasign the length of an array so it's being a problem for me. so please help me out to creating the controls so that i can use them from anywhere.vatzcar wrote:
i don't know how to reasign the length of an array so it's being a problem for me.
To solve this problem, you could use a generic list. using System.Collections.Generic; List pcbMem = new List(); Private void CreatePictureBoxes() { ... for (int i = 0; i < intCount; i++) { PictureBox myPcb = new PictureBox(); ... pcbMem.Add(myPcb); } }