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

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];
}
commented: Perfect answer +3

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

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....

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.