Dynamically moving controls

I have six panels, each panel contains a textbox and its label. Each panel has a checkbox associated with it on the form, but outside of its respective panel. I only want the panels whos checkboxes are checked to be visible. If another checkbox is checked, I want that respective panel to be displayed directly under the last panel displayed. Once that checkbox is unchecked, I want the panel to disapear, and all the other panels that come after, to move up. I am going crazy trying to get the code right, and I thought maybe someone has already done this as could give me an idea on how to get all these panels to apear and dissapear, and reposition according to what panels are showing and where they are. Thanks.

Answer this question

Dynamically moving controls

  • helen b

    if it works, mark the answer.

    now. . . which lines of code don't you understand. I am happy to explain. Someone explained it to me, once.

    "Give someone a fish, they eat today; teach 'em to fish, they eat forever."



  • Robsterw

    Im not sure that is what I was looking for. Lets say the user chooses 3 check boxes, therefore displaying 3 textboxes

    1

    2

    3

     

    If then he/she decided to take 2 away, I need 3 to move up

    1

    3

    Then if they add another I need it to be added like this

    1

    3

    4

    And if 1 is taken away I want the rest to move up

    3

    4

     

    Make sence I got it where I can put one after the other, and even remove some, but eventually they start appearing on top of one another. Any ideas should I put my code up Thanks.


  • bkannappan

    Jeez.. looks like Ive got a lot to learn. To achieve the same thing, my code is about 10 times as much as yours. This is going to take some research, cause I like your way so much better.
  • sotto

    I got it.. thanks for the help.
  • Aceofspades

    did that work for you

  • Shane Meeks

    did you try my code

  • Kvltv

    I tried downloading your complete application, but my Visual Studio 2003 wouldnt open it. I looked over your code, and couldnt follow what was going on. My code works, but yours is much much shorter, Im just not sure what yours does
  • lorifajose

    I put your code in my app, and it works perfectly.. Im sure you knew that though. I cant understand it.. but who the hell cares.. thanks.
  • n-sankar

    drop a panel (panel1) on your form. . . dock left

    drop a panel (panel2) on panel1 . . . dock top

    drop a label (label1) on panel2

    click to select panel2, press [ctrl]+c to copy

    click in panel1 (outside of panel2) and press [ctrl]+v 5 times to paste panels 3 through 7

    drop 6 check boxes (checkbox1-6) on the right side of your form (not on panel1)

    select all the checkboxes and go to the events tab on the properties window. double click in the CheckedChanged entry and add this code:

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
          CheckBox cb = sender as CheckBox;
          if (cb == null) return;
          Panel pnl = cb.Tag as Panel;
          if (pnl == null) return;
          pnl.Visible = cb.Checked;

          for (int i = 0 ; i < panel1.Controls.Count;  i++)
            while(!
                panel1.Controls.IndexOf(panel1.Controls[ i ]).Equals(panel1.Controls[ i ].Tag))
                panel1.Controls.SetChildIndex(panel1.Controls[ i ],Convert.ToInt32(panel1.Controls[ i ].Tag));
        }

     

    and add this override to onLoad:

        protected override void OnLoad(EventArgs e)
        {
          checkBox1.Tag = panel2;
          checkBox2.Tag = panel3;
          checkBox3.Tag = panel4;
          checkBox4.Tag = panel5;
          checkBox5.Tag = panel6;
          checkBox6.Tag = panel7;

          panel2.Tag = 5;
          panel3.Tag = 4;
          panel4.Tag = 3;
          panel5.Tag = 2;
          panel6.Tag = 1;
          panel7.Tag = 0;

          base.OnLoad(e);
        }

    complete code here



  • Srefae

    sorry that is is vs 2005.

    the OnLoad sets the Tag property of the CheckBox to the panel it manages and sets the Tag property of the panel to the index it should have in the contorls collection of panel1. This is necessary because toggling visible on a docked control will change its position in its parents Controls collection if the toggled control is docked in the parent.

    all the handlers for the checkboxes checked event are pointed at the same handler. In this handler, sender is cast as a checkbox cb. cb's tag is cast to a panel, pnl. pnl's visible is set to the state of cb's checked. Then I iterate through the controls collection of panel1, and set the index of the contained controls to the value of its Tag (Converted to an int32 as Tag is an object property) This puts the panels in proper order on the panel.

    the onLoad override could be made short like this:

        protected override void OnLoad(EventArgs e)
        {
          checkBox1.Tag = panel2;
          checkBox2.Tag = panel3;
          checkBox3.Tag = panel4;
          checkBox4.Tag = panel5;
          checkBox5.Tag = panel6;
          checkBox6.Tag = panel7;
         
          foreach(Control ctrl in panel1.Controls)
            ctrl.Tag = panel1.Controls.IndexOf(ctrl);

          base.OnLoad (e);
        }

    here is it in vs2003

     

     

     



  • data_art

    My bad... when I say that I want the next visible control to be "under" the last visible control, I want it to be below it, i.e. the same X coord, but a higher Y coord. Not hidden behind it, but below it, like a list of controls.
  • PeterZ

    Set the control's 'top' property to be the value of the 'top' + 'height' of the control above. You will start moving then!
  • IainMc

    Wow.. that is a lot simpler than my solution. I was finding the Location.Y and simply adding or subtracting from that as needed, while keeping track with various variables, where the other labels were and moving them accordingly, with a bunch of if else statements. yours is a lot nicer, thanks so much for the help. I cant say that I understand it yet, but once I get home Ill be able to disect it further.. thanks again, real nice code.
  • merwinp

    Control.BringToFront Method

    Only the topmost panel is visible iff they share the same size and location.


  • Dynamically moving controls