Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Control Arrays

Control Arrays

Scheduled Pinned Locked Moved C#
csharpdata-structuresjsonhelpquestion
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    SanShou
    wrote on last edited by
    #1

    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.

    H 1 Reply Last reply
    0
    • S SanShou

      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.

      H Offline
      H Offline
      Heath Stewart
      wrote on last edited by
      #2

      You don't actually declare the variable as Array, but as Control[], which inherits from Array 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's Controls collection property anyway, you can always enumerate them all after adding them and initialize their properties.

      Microsoft MVP, Visual C# My Articles

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups