How to add ListBox items to StringBuilder using WinForms in C#

1 Answer

0 votes
namespace WinFormsApp1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            listBox1.Items.Add("c#");
            listBox1.Items.Add("c");
            listBox1.Items.Add("c++");
            listBox1.Items.Add("java");

            System.Text.StringBuilder sb = new System.Text.StringBuilder();

            foreach (object item in listBox1.Items)
            {
                sb.Append(item.ToString());
                sb.Append(", ");
            }

            MessageBox.Show(sb.ToString());
        }
    }
}




/*
run:
 
"c#, c, c++, java, "
  
*/

 



answered Jan 5, 2024 by avibootz

Related questions

1 answer 128 views
1 answer 146 views
1 answer 139 views
1 answer 167 views
1 answer 133 views
1 answer 155 views
1 answer 102 views
...