2009-01-08 56 views
11

我在C#winform应用程序中使用flowlayoutPanel时遇到了问题。我基本上有三个部分的流布局面板。在流布局面板中重新排序控件

第1号是一组2个控制..两个下拉控件,他们总是以相同的顺序,始终显示在所有情况下

第2号是一组的5个不同的控制...基于在一系列因素中,5个控件中的1个可见,所有其他控件的可见属性设置为false

第3节是一组3个控件..与第1节一样,它们始终处于相同的顺序并始终可见。

所以这个归结为第2节是可变的,其他是静态的。

问题出现在第2节......当我改变任何控件的可见性时,它们显得很好(IE ...第1节然后第2节然后第3节)...除了当我设置组合框控件是可见的....在这种情况下,只有在这种情况下...顺序变为(第1节,然后第3节,然后第2节)...我不知道什么会导致排序出在这种情况下同步。

我在我的方法开始时的基本工作是将所有控件设置为Visible = false ...然后设置第1部分Visible = true ...然后循环通过第2部分的条件并设置适当的控件可见= true,最后设置Section 3控制Visible = true。

有没有人有任何流程布局面板控制订购的经验?我无法弄清楚ComboBox发生了什么。

回答

5

为第2部分放置另一个flowlayout面板可能更容易,然后将第2部分控件放入该部分?这样,顶部面板中的可见控件永远不会改变,您不必担心订购。

26

FlowLayoutPanel.Controls是一个名为SetChildIndex(Control c,int index)的方法函数,它允许您将对象设置为特定的索引。

由于FlowLayoutPanel使用控件的索引来确定将它们引入的顺序,因此可以将其设置为您想与其交换的控件的索引,并且它会将控件索引向上控制一个,然后每个索引控制一次。

这里是我的重排PictureBoxes的blog片段在FlowLayoutPanel中

//添加FlowLayoutPanel的上一个WinForm - 名为flowLayoutPanel1

public partial class TestForm: Form 
{ 
     public TestForm() 
     { 
      InitializeComponent(); 
      this.flowLayoutPanel1.AllowDrop = true 
     } 
     private void AddImageToBlog(System.Drawing.Image image) 
     { 
      PictureBox pbox = new PictureBox(); 
      pbox.SizeMode = PictureBoxSizeMode.Zoom;    
      pbox.Height = (_picturebox_height * _ScaleFactor); 
      pbox.Width = (_picturebox_width * _ScaleFactor); 
      pbox.Visible = true; 
      pbox.Image = image; 

      pbox.MouseDown += new MouseEventHandler(pbox_MouseDown); 
      pbox.DragOver += new DragEventHandler(pbox_DragOver);    
      pbox.AllowDrop = true; 
      flpNewBlog.Controls.Add(pbox); 
     } 
     void pbox_DragOver(object sender, DragEventArgs e) 
     { 
      base.OnDragOver(e); 
      // is another dragable 
      if (e.Data.GetData(typeof(PictureBox)) != null) 
      { 
       FlowLayoutPanel p = (FlowLayoutPanel)(sender as PictureBox).Parent;     
       //Current Position    
       int myIndex = p.Controls.GetChildIndex((sender as PictureBox)); 

       //Dragged to control to location of next picturebox 
       PictureBox q = (PictureBox) e.Data.GetData(typeof(PictureBox));     
       p.Controls.SetChildIndex(q, myIndex); 
      }   
     } 
     void pbox_MouseDown(object sender, MouseEventArgs e) 
     { 
      base.OnMouseDown(e); 
      DoDragDrop(sender, DragDropEffects.All); 
     } 



} 
0

SetChildIndex不会重置在的FlowLayout面板控件的顺序。所以当我们执行FlowLayoutPanel.GetNextControl(q, true)时,输出不正确。

+0

您还需要更改TabOrder。您也可以在索引更改时进行设置。 – 2011-07-14 03:36:38

2

您可以重新排序流程面板上的控件,更改控件的父属性并使用您需要的顺序重新指定父属性。

1

尝试此通用解决方案,您可以根据用户控件中的属性对控件进行排序。

// When adding and removing controls, the order is not kept. 
var runsOrderedByStartDate = this.nodesFlowLayoutPanel.Controls.Cast<RunNodeControl>().Select(_ => new { StartDate = _.StartDateTime, RunControl = _ }).OrderBy(_ => _.StartDate).ToList(); 

// Sets index of controls according to their index in the ordered collection 
foreach (var anonKeyValue in runsOrderedByStartDate) 
{ 
    this.nodesFlowLayoutPanel.Controls.SetChildIndex(anonKeyValue.RunControl, runsOrderedByStartDate.IndexOf(anonKeyValue)); 
}