I'm very confused on how to create a new list item on the click of a button, and then how to output the list items into a list box once list items have been created.
I have declared the list and named it coin globally:
List<int> coin = new List<int>();
Then on the click of a button named 'Enter' I am trying to add a new item to the list, which will determine the value of the list item through the for loop, so the value must be higher than the amount of items currently in the list:
private void btnEnter_Click(object sender, EventArgs e)
{
for (int i = 0; i > coin.Count(); i++)
{
coin.Add(i);
}
}
Then in the list box I am trying to output all of the list items by converting them to a string, and counting the amount of values inside the for loop, like so:
private void groupBox1_Enter(object sender, EventArgs e)
{
for (int i = 0; i < coin.Count(); i++)
{
string spacesOutput = coin.ToString();
groupBox1.Text = "/" + spacesOutput;
}
}
On the click of the enter button nothing happens, and nothing is displayed in the list box so I am a little confused. Thanks.
Your first for loop can go 2 ways:
It either does nothing (like right now), because i
will start at 0
, just like coin.Count
.
The other option is an endless loop. For this, you have to start counting at 1
: for (i = 1...
. Neither will do you any good.
I also suppose you want to have the text of your GroupBox to look something like this:
/1/2/3/4/5...
which you will not accomplish with your code. You convert the List itself to string.
To have the format seen above, I suggest a StringBuilder
object:
StringBuilder builder = new StringBuilder();
for (int i = 0; i < coin.Count(); i++)
{
builder.Append("/");
builder.Append(coin[i].ToString());
}
groupBox1.Text = builder.ToString();