Hi everyone
I am trying to display a listbox(list of Column Names selected from a table in database)into a textbox displaying like a CSV file format.
Please be notice that I do not want to create a CSV file, as I said I just like to pass items from listbox into a Textbox in a comma seperated format.
lets say I have a listbox populated like:

lstCols.Items.Add("Name");
lstCols.Items.Add("ID");
lstCols.Items.Add("Age");
lstCols.Items.Add("City");

Now how I can display the above list in a text box like

Name, ID, Age, City

I realy appreciate your time in advance

Edited 5 Years Ago by Behseini: n/a

for(int i = 0; i< lstCols.Items.Count; i++)
{
    if((i +1) < lstCols.Count)
        textBox1.Text += lstCols.Items[i] + ", ";
    else
       textBox1.Text += lstCols.Items[i];
}
Comments
Perfect answer

Thanks

but I just modify your code a little bit on

if((i +1) < lstCols.Count)

to:

for(int i = 0; i<listBox1.Items.Count; i++)
{
 if((i +1) < listBox1.Items.Count)
 textBox1.Text += listBox1.Items[i] + ", ";
 else
 textBox1.Text += listBox1.Items[i];
}

Thanks again and appreciate for your time

Edited 5 Years Ago by Behseini: n/a

Thanks

but I just modify your code a little bit on

if((i +1) < lstCols.Count)

to:

for(int i = 0; i<listBox1.Items.Count; i++)
{
 if((i +1) < listBox1.Items.Count)
 textBox1.Text += listBox1.Items[i] + ", ";
 else
 textBox1.Text += listBox1.Items[i];
}

Thanks again and appreciate for your time

No problem it was just a typing error logic was right....

This question has already been answered. Start a new discussion instead.