Control Arrays
-
I was just wondering if there is a way to do a control array for ease of setting up some controls. Basically I am trying to do this (syntax is more psuedo code):
controlA_View1.Top = controlA_View2.Top; controlA_View1.Left = controlA_View2.Left; controlA_View1.Visible = false; controlB_View1.Top = controlB_View2.Top; controlB_View1.Left = controlB_View2.Left; controlB_View1.Visible = false; ... controlN_View1.Top = controlN_View2.Top; controlN_View1.Left = controlN_View2.Left; controlN_View1.Visible = false;
I was wondering if I could do this:// Assume all controls were inserted by the designer Array arrView1; arrView1.Add(controlA_View1); arrView1.Add(controlB_View1); ... arrView1.Add(controlN_View1); Array arrView2; arrView2.Add(controlA_View2); arrView2.Add(controlB_View2); ... arrView2.Add(controlN_View2); for(int i = 0; i < arrView1.Count() && i < arrView2.Count(); i ++) { // Not casting even though I know array returns an object arrView1[i].Top = arrView2[i].Top; arrView1[i].Left = arrView2[i].Left; arrView2[i].Visible = false; }
I am adding the controls via the form desinger because it is obviously easier to lay them out. I am using my current home project to explore deeper in to C# however I know I could do something like this with pointers or maybe with unsafe code. I remember the ref keyword can cause a reference to be passed to a function but can a ref be passed to an array? Thanks for any help... Brian If you start a fire for a man, he will be warm for a day. If you start that same man on fire, he will be warm for the rest of his life. -
I was just wondering if there is a way to do a control array for ease of setting up some controls. Basically I am trying to do this (syntax is more psuedo code):
controlA_View1.Top = controlA_View2.Top; controlA_View1.Left = controlA_View2.Left; controlA_View1.Visible = false; controlB_View1.Top = controlB_View2.Top; controlB_View1.Left = controlB_View2.Left; controlB_View1.Visible = false; ... controlN_View1.Top = controlN_View2.Top; controlN_View1.Left = controlN_View2.Left; controlN_View1.Visible = false;
I was wondering if I could do this:// Assume all controls were inserted by the designer Array arrView1; arrView1.Add(controlA_View1); arrView1.Add(controlB_View1); ... arrView1.Add(controlN_View1); Array arrView2; arrView2.Add(controlA_View2); arrView2.Add(controlB_View2); ... arrView2.Add(controlN_View2); for(int i = 0; i < arrView1.Count() && i < arrView2.Count(); i ++) { // Not casting even though I know array returns an object arrView1[i].Top = arrView2[i].Top; arrView1[i].Left = arrView2[i].Left; arrView2[i].Visible = false; }
I am adding the controls via the form desinger because it is obviously easier to lay them out. I am using my current home project to explore deeper in to C# however I know I could do something like this with pointers or maybe with unsafe code. I remember the ref keyword can cause a reference to be passed to a function but can a ref be passed to an array? Thanks for any help... Brian If you start a fire for a man, he will be warm for a day. If you start that same man on fire, he will be warm for the rest of his life.You don't actually declare the variable as
Array
, but asControl[]
, which inherits fromArray
implicitly:Control[] controls = new Control[]
{
myControl1,
myControl2,
// ...
};
for (int i=0; i<controls.Length; i++)
{
Control c = controls[i];
if (i == 0) c.Location = new Point(8, 8);
else c.Location = new Point(8, 8 + controls[i - 1].Location.Y;
// ...
}You could also use a collection or list, but then you either have to cast or use a typed collection or list like
Control.ControlCollection
. Also, since you need to add your controls to their container'sControls
collection property anyway, you can always enumerate them all after adding them and initialize their properties.Microsoft MVP, Visual C# My Articles