Create and delete Controls At Runtime

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • MyMarlboro
    New Member
    • Mar 2008
    • 71

    Create and delete Controls At Runtime

    Assume i have a chart (a type of control)...
    at run time, how could i add more charts and close the charts on a form by end user?
    e.g.

    ---------------------
    chart1
    chart2
    ---------------------

    as i add a new chart it become as below...

    ---------------------
    chart1
    chart2
    chart3
    ---------------------

    when i close chart2, chart3 could move up immediately
    --------------------
    chart1
    chart3
    --------------------

    Could anyone tell me how to do that? please.
  • MyMarlboro
    New Member
    • Mar 2008
    • 71

    #2
    to remove the chart2.. i could use the this.Controls.R emoveByKey("cha rt2"); command... but the problem is the chart3 wont shift up...
    i'm not sure whether is it the right approach to use the command to delete the chart2.

    Comment

    • tlhintoq
      Recognized Expert Specialist
      • Mar 2008
      • 3532

      #3
      You will have to manually loop through your charts and change their .Location property


      Chart3.Location = new Point(Chart2.Lo cation.x, Chart2,Bounds.B ottom + 10);

      and so on.

      Comment

      • tlhintoq
        Recognized Expert Specialist
        • Mar 2008
        • 3532

        #4
        This just occured to me...A panel has auto layout features.

        Put a panel as the first control, with Dock to full. This becomes the primary panel. The "canvas" for your work.
        Put your chart on another panel, whose width is the same as the primary panel.
        Add the chart's panel to the primary panel. The primary panel will 'flow' the controls downward automatically.

        When you want to delete a chart and have all the rest slide up, just delete the panel with that chart. The rest will take care of itself.

        Comment

        • MyMarlboro
          New Member
          • Mar 2008
          • 71

          #5
          Originally posted by tlhintoq
          This just occured to me...A panel has auto layout features.

          Put a panel as the first control, with Dock to full. This becomes the primary panel. The "canvas" for your work.
          Put your chart on another panel, whose width is the same as the primary panel.
          Add the chart's panel to the primary panel. The primary panel will 'flow' the controls downward automatically.

          When you want to delete a chart and have all the rest slide up, just delete the panel with that chart. The rest will take care of itself.
          how to set dock to full??? Dock only could set to fill, bottom, right, left and top.

          i set panel1 to fill. add another 3 panels ( panel2, panel3, panel4) and 1 button on tops of it. for button1_click i add the code this.panel1.Con trols.RemoveByK ey("panel3");

          but the click button panel3 gone but panel4 didnt shift up...

          please guide

          Comment

          • tlhintoq
            Recognized Expert Specialist
            • Mar 2008
            • 3532

            #6
            My mistake

            I should have said "FlowControlPan el"

            Place a FlowControlPane l on your form and you will see several properties you want such as "FlowDirect ion" that you can set to "TopDown"

            The code below works with FlowControlPane l dragged to the form along with two buttons.


            Code:
                    private void button1_Click(object sender, EventArgs e)
                    {
                        Panel One = new Panel();
                        One.BackColor = Color.Red;
                        One.Name = "One";
                        flowLayoutPanel1.Controls.Add(One);
            
                        Panel Two = new Panel();
                        Two.BackColor = Color.White;
                        Two.Name = "Two";
                        flowLayoutPanel1.Controls.Add(Two);
            
                        Panel Three = new Panel();
                        Three.BackColor = Color.Blue;
                        Three.Name = "Three";
                        flowLayoutPanel1.Controls.Add(Three);
                    }
            
                    private void button2_Click(object sender, EventArgs e)
                    {
                        flowLayoutPanel1.Controls.RemoveByKey("Two");
                    }
            Depending on what your chart objects are like, you probably don't even need to place them on panels. Just put the charts directly into the FlowControlPane l. Just depends on the look and behavior you want. I tend to put in a Panel that is full width and alternate colors, then put the repetitive things like your charts on the panels making it easier to follow which record is which. Kind of like the old green/white bar computer paper.

            Comment

            • MyMarlboro
              New Member
              • Mar 2008
              • 71

              #7
              Thank you.
              i wonder if i can create a close button at the top right edge of each panel... so that i could close the panel with the 'x' button likewise the form we have.

              Comment

              Working...