简体   繁体   中英

Output string array to listbox

I have created a string array named 'coin' and I am trying to output the content of the string array to a item box along with another integer variable, and I keep getting the list box saying String[] Array.

To create the array, I have used the following:

string[] coins = new string[4];

Then on the click of the enter button, a new item will be added to the array, along with the count variable which increments on the click of the button, then it outputs the contents of the coin array to the list box:

coins[valuesEntered] = "coin" + valuesEntered.ToString();
valuesEntered++;
listBox1.Items.Add(coins);

However, this seems not to work and instead just outputs String[] Array to the group box each time the enter button is clicked. I cant seem to get past this brick wall at the moment.

You are attempting to add an object to your ListBox, specifically a string array represented in syntax as String[] Array. You want the contents of the array, not the array itself.

You first need to join the individual contents of the array to a separate string variable before you attempt to add it to the ListBox.

string listOfCoins = string.Join(" ",coins);
listBox1.Items.Add(listOfCoins.ToString());

this might work too

listBox1.Items.Add(coins.Text);

I don't use Visual C# much, so take this with a grain of salt.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM