User999999
User999999

Reputation: 2520

Ordering Controls inside FlowLayoutpanel

I have a Windows.Forms.FlowLayoutPanel that is being filled at runtime with controls of the type MasterMeter (and controls inheriting from MasterMeter). This is your standard Usercontrol. Each of those controls has a property ErrorTime. This value keeps the duration of the current error or alert.

 Datetime? ErrorTime {get; set;}

I'm currently trying to order the controls using the bubblesort-method based on the duration of the error (descending). Right now I was wondering is there a faster/more performant/more reliable way to do this?

If not, is there another container i could be using? It needs to have the following caracteristics:

Current Code (that won't sort)

I'm using a 1D-Array to keep it simple (in reality its a jagged Array). Note: The possibilty of NULL values

MasterMeter[] meterList = {new MasterMeter() { ErrorTime = null },
                           new MasterMeter() { ErrorTime = DateTime.Now },
                           new MasterMeter() { ErrorTime = DateTime.Now.AddDays(-10) },
                           new MasterMeter() { ErrorTime = DateTime.Now.AddDays(37) },
                           new MasterMeter() { ErrorTime = DateTime.Now.AddDays(53) },
                           new MasterMeter() { ErrorTime = DateTime.Now.AddDays(3) },
                           new MasterMeter() { ErrorTime = null },
                           new MasterMeter() { ErrorTime = DateTime.Now.AddDays(-17) },
                           new MasterMeter() { ErrorTime = DateTime.Now.AddDays(-10) },
                           new MasterMeter() { ErrorTime = null }
                          };

I'm Bubblesorting as follows:

        for (int outer = 0; outer < meterList.Count(); outer++)
        {
            for (int inner = 0; inner < (meterList.Count() - 1); inner++)
            {
                if (meterList[inner].ErrorTime > meterList[inner + 1].ErrorTime)
                {
                    var temp = meterList[inner + 1];
                    meterList[inner + 1] = meterList[inner];
                    meterList[inner] = temp;
                }
            }
        }

My ouput result (Using Console.Writeline):

        foreach (MasterMeter meterlist in meterList)
        {
            Console.WriteLine(meterlist.ErrorTime);
        }
        Console.ReadLine();

But my array still isn't sorted. What is going wrong here?

Upvotes: 1

Views: 2109

Answers (2)

user3500176
user3500176

Reputation: 63

I just read this and I am not sure the answer really convinced, and we can quietly bet that many people in the next ten years will have controls to order in FlowLayoutPanels.

I had the problem of a FlowLayoutPanel not showing the controls in the order I wanted.

As I clear the controls at each modification, before loading them again from a TableAdapter (to a DataSet), all I had to do was to correct the query of the TableAdapter, so that it fills the DataSet in the order I want.

Then, the controls of the FlowLayoutPanel are loaded in the order of the DataSet.

In the proposals here I saw SetChildIndex : this means you have to browse through the collection of controls in the FlowLayoutPanel, and give each the index you want it to have.

It would be interesting to operate both, to compare the time it takes. But one point has been left unsaid : what method to use to determine each control's index, when avoiding to clear the controls to fill them again.

In this case I guess you have to pay attention that your code is readable.

Upvotes: 0

LarsTech
LarsTech

Reputation: 81610

You have to decide what to do with those null values in your list since it does interfere with your bubble sort:

if (!meterList[inner].ErrorTime.HasValue || 
    meterList[inner].ErrorTime > meterList[inner + 1].ErrorTime) {

As far as re-ordering the controls in the FlowLayoutPanel, that code was missing from your post, but in general, you would use the FlowLayoutPanel.Controls.SetChildIndex method to re-order your controls.

Upvotes: 1

Related Questions