KGS
KGS

Reputation: 685

List order (or iteration) malfunction

Hello I am having trouble with an iteration through a list of 17 labels:

for (int i = 0; i < labels.Count - 1; i++)
{
    MessageBox.Show(labels[i].Name);
    if (labels[i].Visible == false && labels[i + 1].Visible == true)
    {
        ...

Here are the results I get:

First it goes from label10 to label17, and then in descending order from label9 to label2.

Here is how I add the labels to the list:

private void newGameToolStripMenuItem_Click(object sender, EventArgs e)
{
    foreach (Control c in this.Controls)
    {
        if (c is Label)
        {
            labels.Add(c);
            c.Enabled = true;
            if (c.Visible == false)
            {
                c.Visible = true;
            }
        }
    }
}

I want it to go from label1 to label16, since the loop is just a loop I guess the problem lies in the order in which the labels were added to the list, but I am not sure how to fix it.

Upvotes: 1

Views: 136

Answers (5)

Idle_Mind
Idle_Mind

Reputation: 39142

Try this out:

    private void newGameToolStripMenuItem_Click(object sender, EventArgs e)
    {
        labels.Clear();
        Control[] matches;
        for (int i = 1; i <= 16; i++)
        {
            matches = this.Controls.Find("label" + i.ToString(), true);
            if (matches.Length > 0 && matches[0] is Label)
            {
                Label lbl = (Label)matches[0];
                labels.Add(lbl);
                lbl.Enabled = true;
                if (lbl.Visible == false)
                {
                    lbl.Visible = true;
                }

            }
        }
    }

Upvotes: 0

kundan
kundan

Reputation: 73

assuming that you have Labels id as Label1,Label2..........,Label16 in order to get the labels serially you have to write the following code

labels = labels.ConvertAll<Control>(GetIdFromLabel);
labels.Sort((x, y) => { return x.Id.CompareTo(y.Id); });

public Control GetIdFromLabel(Control c)
    {
        c.Id = c.Name.Replace("Label", "") == "" ? 0 : Convert.ToInt32(c.Name.Replace("Label", ""));
        return c;
    }

add this class in your code also

public class Control
{
    public string Name { get; set; }
    public int Id { get; set; }
}

Upvotes: 0

Nikola Davidovic
Nikola Davidovic

Reputation: 8656

Your main problem is lexicographic order which is inherently used when you sort by Name of the label, what you want is to sort by numbers after the term label. In that case, first sort the labels list and then run the for statement over it, check the code:

var lst = labels.OrderBy(x => int.Parse(x.Name.Substring("label".Length))).ToList();
for (int i = 0; i < lst.Count - 1; i++)
{
    MessageBox.Show(lst[i].Name);
    ...

But have in mind that this code is simple and presumes that label Name property always starts with "label" string. If that can change you must handle that case.

Upvotes: 1

nmat
nmat

Reputation: 7591

Check the designer.cs file to see in which order the labels are added to the Form

Upvotes: 0

Palatis
Palatis

Reputation: 138

I guess you want to sort the labels according to their names?

labels.Sort((x, y) => { return x.Name.CompareTo(y.Name); });

but what are the difference between:

  1. Show "Label 1" first, then "Label 2", and
  2. Show "Label 2" first, then "Label 1"?

Upvotes: 0

Related Questions