none
Display all ComboxBox values as a comma string RRS feed

  • Question

  • Hi, I need help with the code to take all the values in a combobox drop down and convert them to a comma separated string. My end goal is to display this comma separated string in a textbox. Thanks in advance for the help.

    svk

    Wednesday, May 15, 2019 1:24 AM

Answers

  • Hi czark,

    Thank you for posting here.

    For your question, you want to display all ComBox values as a comma string.

    I do a simple code, you could have a look.

      private void Button1_Click(object sender, EventArgs e)
            {
                string a = null;
                List<string> list = new List<string>();
                foreach (string item in comboBox1.Items)
                {
                    list.Add(item);
                }
    
                comboBox1.Items.Clear();
                foreach (var item in list)
                {
                    comboBox1.Items.Add(item + ",");
                }
                foreach (string item in comboBox1.Items)
                {
                    a = a + item;
                }
                textBox1.Text = a;
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
                comboBox1.Items.Add("test1");
                comboBox1.Items.Add("test2");
                comboBox1.Items.Add("test3");
                comboBox1.Items.Add("test4");
            }

    Result:

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by czarvk Wednesday, May 15, 2019 12:19 PM
    Wednesday, May 15, 2019 3:30 AM
    Moderator
  • Try this too:

     

       string s = string.Join( ",", comboBox1.Items.Cast<object>( ) );


    • Edited by Viorel_MVP Wednesday, May 15, 2019 5:08 AM
    • Marked as answer by czarvk Wednesday, May 15, 2019 12:19 PM
    Wednesday, May 15, 2019 5:04 AM

All replies

  • Hi czark,

    Thank you for posting here.

    For your question, you want to display all ComBox values as a comma string.

    I do a simple code, you could have a look.

      private void Button1_Click(object sender, EventArgs e)
            {
                string a = null;
                List<string> list = new List<string>();
                foreach (string item in comboBox1.Items)
                {
                    list.Add(item);
                }
    
                comboBox1.Items.Clear();
                foreach (var item in list)
                {
                    comboBox1.Items.Add(item + ",");
                }
                foreach (string item in comboBox1.Items)
                {
                    a = a + item;
                }
                textBox1.Text = a;
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
                comboBox1.Items.Add("test1");
                comboBox1.Items.Add("test2");
                comboBox1.Items.Add("test3");
                comboBox1.Items.Add("test4");
            }

    Result:

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by czarvk Wednesday, May 15, 2019 12:19 PM
    Wednesday, May 15, 2019 3:30 AM
    Moderator
  • Try this too:

     

       string s = string.Join( ",", comboBox1.Items.Cast<object>( ) );


    • Edited by Viorel_MVP Wednesday, May 15, 2019 5:08 AM
    • Marked as answer by czarvk Wednesday, May 15, 2019 12:19 PM
    Wednesday, May 15, 2019 5:04 AM