The order of controls in the Control property form in C #

I had a special problem with the order of adding FlowLayoutPanels to the control property . This is what I tried

I added 7 FlowLayoutPanels to the C # window app from left to right in vertical stripes. Then I marked the thread layouts as 1, 2, 3, ... 7 again from left to right. Now, in the form load handler, I wrote the following snippet,

    foreach (FlowLayoutPanel aDaysControl in this.Controls)
    {
        MessageBox.Show(aDaysControl.Tag.ToString());
    }

Messages were expected to appear in the order 1, 2, ... 7 . But I received it in reverse order (7, 6, ... 1). Can someone help me with the mistake I made?

The reason for maintaining order,

I am trying to do calendar management with each row representing the day. If the month starts on Wednesday, then I need to add an empty label in the first (Monday) and second (Tuesday) row. So order matters

+5
source share
5 answers

look at the order in which they are added to the form in the file yourForm.designer.cs

+3
source

I know this is a pretty old question, but ...

You might want to use one SetChildIndex. egthis.Controls.SetChildIndex(button1, 0);

+7
source

, Form1.designer.cs, :

        // 
        // Form1
        // 
        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.ClientSize = new System.Drawing.Size(658, 160);
        this.Controls.Add(this.flowLayoutPanel7);
        this.Controls.Add(this.flowLayoutPanel6);
        this.Controls.Add(this.flowLayoutPanel5);
        this.Controls.Add(this.flowLayoutPanel4);
        this.Controls.Add(this.flowLayoutPanel3);
        this.Controls.Add(this.flowLayoutPanel2);
        this.Controls.Add(this.flowLayoutPanel1);
        this.Name = "Form1";
        this.Text = "Form1";
        this.ResumeLayout(false);

, , 1 , 2 .. 7, 6.

InitializeComponent(), .

?

, . :

        var flowpanelinOrder = from n in this.Controls.Cast<Control>()
                               where n is FlowLayoutPanel
                               orderby int.Parse(n.Tag.ToString())
                               select n;

        /* non linq
        List<Control> flowpanelinOrder = new List<Control>();
        foreach (Control c in this.Controls)
        {
            if (c is FlowLayoutPanel) flowpanelinOrder.Add(c);                
        }
        flowpanelinOrder.Sort();
         * */

        foreach (FlowLayoutPanel aDaysControl in flowpanelinOrder)
        {
            MessageBox.Show(aDaysControl.Tag.ToString());
        }
+1

, - , ..? . . ( Linq):

public static List<Control> ToControlsSorted(this Control panel)
{
    var controls = panel.Controls.OfType<Control>().ToList();
    controls.Sort((c1, c2) => c1.TabIndex.CompareTo(c2.TabIndex));
    return controls;
}

:

foreach (FlowLayoutPanel aDaysControl in this.ToControlsSorted())
{
    MessageBox.Show(aDaysControl.TabIndex.ToString());
}

( TabIndex). Tag .

+1

?

(, ), , , .

EDIT: , . , - , Controls. "". , , . , 7- , :

FlowLayoutPanel[] panels = new FlowLayoutPanel[7];

foreach(FlowLayoutPanel panel in this.Controls)
{
    panels[(int)panel.Tag] = panel;
}

// Now, you can reference the panels directly by subscript:

panels[2].BackColor = Color.Aquamarine;

, !

0

All Articles