如何对FlowLayoutPanel上的控件进行排序 [英] How do I sort controls on a FlowLayoutPanel

查看:253
本文介绍了如何对FlowLayoutPanel上的控件进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好:



我有一个流程布局面板,用户可以在其中添加按钮。但是,虽然我的按钮很好地放在面板上,但它们并没有按名称排序。以下是用户添加按钮的方式:



Hello folks:

I have a flow layout panel where users can add buttons. However, while my buttons place themselves nicely on the panel, they are not sorted by name. Here is how the user adds the button:

Private Sub btnAddCategory_Click(sender As Object, e As EventArgs) Handles btnAddCategory.Click

        Dim newButton As New Button

        With newButton

            .Size = CType(New Point(211, 34), Drawing.Size)
            .FlatStyle = Windows.Forms.FlatStyle.Flat
            .FlatAppearance.BorderSize = 0
            .FlatAppearance.BorderColor = System.Drawing.Color.Gray
            .BackColor = System.Drawing.Color.Gray

            .Name = "btn" & TextBox3.Text
            .Text = TextBox3.Text

        End With

        pnlCategories.Controls.Add(newButton)
    


    End Sub





但是我的按钮没有排序。所以我想做以下操作来对我的按钮进行排序:





But my buttons are not sorted. So I thought of doing the following to sort my buttons:

Private Sub pnlCategories_ControlAdded(sender As Object, e As ControlEventArgs) Handles pnlCategories.ControlAdded

        For Each btn As Button In Me.Controls.OfType(Of Button)() _
                               .OrderBy(Function(b) b.Text)

        Next

    End Sub





但没有任何反应,我知道可以对按钮进行排序,但我可以'似乎让它工作。



But nothing happens, I know is possible to sort the buttons, but I can't seem to get it to work.

推荐答案

据我所知,当控件添加到 FlowLayoutPanel ,它们按照添加的顺序排序。



我建议使用列表< T> [ ^ ]用于管理控件的泛型类。它包含几个属性和方法,使您可以对元素进行排序,添加,检查是否存在,清除等。



实施:

1 )在将控件添加到 FlowLayoutPanel 之前,

  &NBSP; a)检查集合中是否存在,

  &NBSP; b)如果不存在 - 将其添加到列表< T>

  &NBSP; c)排序元素

2)从FlowLayoutPanel删除控件

3)从列表< T> 向FlowLayoutPanel添加控件



也许这不是优雅的解决方案,但此刻我没有更好的主意。
As far as i know, when controls are added to FlowLayoutPanel, they are "sorted" in the order of addition.

I'd suggest to use List<T>[^] generic class to manage controls. It contains several properties and methods which enables you to sort elements, add, check if exists, clear, etc.

Implementation:
1) Before you add control to FlowLayoutPanel,
    a) check if it exists in the collection,
    b) if not exists - add it to List<T>
    c) sort elements
2) Remove controls from FlowLayoutPanel
3) Add controls to FlowLayoutPanel from List<T>

Maybe it's not elegant solution, but at this moment i have no better idea.


这是因为你没有做什么东西来整理孩子们。方法 OrderBy 没有任何副作用。它返回一个重新排序的对象的集合。您还需要执行一个步骤:从其父级中删除所有控件并再次填充它们,这次按照您在订购后返回的对象中的顺序进行填充。



此外,使用 OfType 是值得怀疑的。很明显如果所有孩子都是按钮该怎么办,但在这种情况下,你应该使用所有控件,而不仅仅是按钮。如果某些控件不是按钮,则可以使用其他一些排序条件。



要填充控件,请使用属性 System.Windows.Forms.Control.Controls 和方法 System.Windows.Forms.Control.ControlCollection.Add

http://msdn.microsoft.com/en-us/library/system.windows.forms.control.controls%28v = vs.110%29.aspx [ ^ ],

http://msdn.microsoft.com/en-us/library/system.windows.forms.control.controlcollection。 add(v = vs.110).aspx [ ^ ],

http://msdn.microsoft.com/en-us/library/system .windows.forms.control.controlcollection.addrange%28v = vs.110%29.aspx [ ^ ]。



-SA
This is because you are not doing anything to sort the children. The method OrderBy does no have any side effects. It returns a collection of reordered object. You need to do one more step: remove all controls from its parent and populate them again, this time, in the order you got in the object returned as the result of the ordering.

Besides, the use of OfType is questionable. It's clear what to do if all the children are buttons, but in this case, you should use all the controls, not just buttons. If some of the control are not buttons, you can use some other sorting criteria.

To populate controls, use the property System.Windows.Forms.Control.Controls and the method System.Windows.Forms.Control.ControlCollection.Add:
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.controls%28v=vs.110%29.aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.controlcollection.add(v=vs.110).aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.controlcollection.addrange%28v=vs.110%29.aspx[^].

—SA


这篇关于如何对FlowLayoutPanel上的控件进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆