user8833922
user8833922

Reputation:

"Reset" For loop in C#?

I have the following code

private List<CheckBox> list = new List<CheckBox>();
//Displays toppings in receipt
        foreach (CheckBox cbToppings in gbToppings.Controls)
        {
            if (cbToppings.Checked == true)
            {
                list.Add(cbToppings);
                iToppings++;

            }

        }

        for (Int32 i = 0; i < list.Count; i++)
        {

            txtToppingsReceipt.Text += Convert.ToString(list[i].Text + Environment.NewLine);

        }

I have a groupbox with checkboxes in them, and the goal is to display the checked items in a multiline textbox when a button (btnOrder) is clicked. That part works perfectly for the first time, however, right now, when I click the btnOrder again, it does not clear the original output, despite me putting txtToppingsReceipt.Clear();.

I have another button which basically resets all fields, and that the clear textbox code is under that button. However no matter where I put that code, it simply does not want to clear the box after everytime btnOrder is clicked.

Am I doing something wrong? Will you guys be able to help me, I guess, reset the loop after every time it runs? If I put "break;" in the for loop, the code only runs once, and refuses to cooperate the second time I click btnOrder.

I'm new to programming so I may be getting ahead of myself. Either way, hopefully you guys can help me figure this thing out...

Upvotes: 0

Views: 114

Answers (3)

Enigmativity
Enigmativity

Reputation: 117029

Your code should look something like this:

void MethodName()
{
    List<CheckBox> list = new List<CheckBox>();
    foreach (CheckBox cbToppings in gbToppings.Controls)
    {
        if (cbToppings.Checked == true)
        {
            list.Add(cbToppings);
            iToppings++;
        }
    }

    txtToppingsReceipt.Text = "";
    for (Int32 i = 0; i < list.Count; i++)
    {
        txtToppingsReceipt.Text += list[i].Text + Environment.NewLine;
    }
}

That way you're creating a new list each time and you're clearing the txtToppingsReceipt text before rebuilding it.

Please note that you didn't show us your full method so I don't know what name you've called it.

Or even better, like this:

void MethodName()
{
    txtToppingsReceipt.Text = "";
    foreach (CheckBox cbToppings in gbToppings.Controls)
    {
        if (cbToppings.Checked == true)
        {
            txtToppingsReceipt.Text += list[i].Text + Environment.NewLine;
            iToppings++;
        }
    }
}

You could have written your code like this though:

var list = gbToppings.Controls.OfType<CheckBox>().Where(x => x.Checked == true).Select(x => x.Text).ToList();
iToppings = list.Count;
txtToppingsReceipt.Text = String.Join(Environment.NewLine, list);

Upvotes: 2

Ankush Madankar
Ankush Madankar

Reputation: 3834

Don't use += operator to append string, make use of = operator to assign changed value of txtToppingsReceipt.Text. I have refactor code and will work as expected.

string totalToppings = string.Empty;

foreach (CheckBox cbToppings in gbToppings.Controls)
{
    if (cbToppings.Checked == true)
    {
        totalToppings += cbToppings.Text + Environment.NewLine;
        iToppings++;
    }
}

txtToppingsReceipt.Text = totalToppings;

Upvotes: -1

Sunil
Sunil

Reputation: 3424

Why not simply start with txtToppingsReceipt.Text = ""; to clear the textbox.

txtToppingsReceipt.Text = "";
list.Clear();
//Displays toppings in receipt
foreach (CheckBox cbToppings in gbToppings.Controls)
{
    if (cbToppings.Checked == true)
    {
        list.Add(cbToppings);
        iToppings++;
    }
}

for (Int32 i = 0; i < list.Count; i++)
{
    txtToppingsReceipt.Text += Convert.ToString(list[i].Text + Environment.NewLine);
}

If would not suggest to use a multiline text box for keep adding the line. Use a rich text box and use rtb.AppendText

Upvotes: 1

Related Questions