locked
Problem with inserting controls in FlowlayoutPanel RRS feed

  • Question

  • hi everyone ,

    i am using FlowlayoutPanel to creates dynamic controls.The problem is, I am not able to insert controls at some particular location.

     

    For Eg:-

    I have created 3 controls in FlowlayoutPanel:- label1,combo,label2(in sequence)

    Now when my user selects a combo value it(combobox) should disappear and at that location 3 new controls should be placed.Suppose we insert 3 buttons in place of combo so the sequence of my controls should be ...

    Label1,button1,button2,button3,label2.

     

    Is it possible to do?

    Or is there any alternative to FlowlayoutPanel control?

     

    Any help will be appreciated.

    Thanks in advanceSmile

     

     

     

    Friday, May 9, 2008 9:46 AM

Answers

  • I just found an interesting method in the Controls collection that might just fit your needs:

    setChildIndex(Control, newIndex);

     

    so you can try doing something like this, assuming you have created your buttons properly and only need to add them to the flowlayoutpanel

     

    Code Snippet

    void replaceControl(FlowLayoutPanel fl,Control Control2Replace,Control[] Controls2Add){

      int index = fl.Controls.IndexOf(Control2Replace);

      if (index==-1) return;

      fl.Controls.Remove(Control2Replace);

      foreach(Control c in Controls2Add){

        fl.Controls.Add(c);

        fl.SetChildIndex(c,index++);

      }

    }

     

     

     

    I did not test it myself, it may need some tweaking.
    Monday, May 12, 2008 7:57 AM

All replies

  • The flowlayoutpanel's control property is the one that is causing you problems.

    That is because it is a normal controlcollection that does not support insert, it does support removing at a certain position though, but it only supports add. So every control you add is added at the very end.

     

    The tablelayout control does not have this problem, because it has a different controls property that allows inserting at a dedicated cell.

     

    So you have 3 options.

    -You can use the tablelayout, but then I am not sure if this fits for you.

    -When you need to insert something at a certain position, you remove all controls to the right, insert your controls you wanted to insert, and add the ones you removed again. Maybe not a very good solution.

    - You derive your own flowlayout panel from the flowlayout panel and only replace the controls property with a different collection of your own that supports inserting. Don't forget to repaint the flowlayout after inserting something!

     

     

     

    Sunday, May 11, 2008 8:30 AM
  • Thanks Philips.

     

    I would like you to tell that i don't want to remove all controls on the right side.Please note the example that I have given.

    I am replacing the combo with 3 new buttons but the controls to the right(Lable2) and the controls to the left(Label 1) are as it is.

     

    I will try this with TableLayout Panel.

    But i am little confused about how to implement your second option.

    If possible can u demonstrate it with a small example?

     

    Thanks for all the help....

     

     

    Monday, May 12, 2008 5:49 AM
  • I just found an interesting method in the Controls collection that might just fit your needs:

    setChildIndex(Control, newIndex);

     

    so you can try doing something like this, assuming you have created your buttons properly and only need to add them to the flowlayoutpanel

     

    Code Snippet

    void replaceControl(FlowLayoutPanel fl,Control Control2Replace,Control[] Controls2Add){

      int index = fl.Controls.IndexOf(Control2Replace);

      if (index==-1) return;

      fl.Controls.Remove(Control2Replace);

      foreach(Control c in Controls2Add){

        fl.Controls.Add(c);

        fl.SetChildIndex(c,index++);

      }

    }

     

     

     

    I did not test it myself, it may need some tweaking.
    Monday, May 12, 2008 7:57 AM
  • Perfect !!! It works the way I want .

    Thank You So Much Philips

     

    The last line should be

    f1.Controls.SetChildIndex(c,index++);

     

    and not

    fl.SetChildIndex(c,index++);

     

    Tuesday, May 13, 2008 10:29 AM
  •  

    hi Philip_Stuyck__,

     

    great job this code helped me alot i just wana rectify a line of code. the last line of code should be

     

    fl.Controls.SetChildIndex(c,index++);

     

    here is the complete rectified code

     

    Code Snippet

    void replaceControl(FlowLayoutPanel fl,Control Control2Replace,Control[] Controls2Add)

    {

      int index = fl.Controls.IndexOf(Control2Replace);

      if (index==-1) return;

      fl.Controls.Remove(Control2Replace);

      foreach(Control c in Controls2Add)

      {

        fl.Controls.Add(c);

        fl.Controls.SetChildIndex(c,index++);

      }

    }

     

     

    Wednesday, June 25, 2008 6:15 AM