List all combobox items in one messagebox

I am trying to list all combobox items in one messagebox. but all I get is every item that appears in its own messagebox. I know the message should be outside the loop, but when I do it it says the variable is not assigned. Any help would be great.

private void displayYachtTypesToolStripMenuItem_Click (object sender, EventArgs e) {

       string yachtTypesString;

        for (int indexInteger = 0; indexInteger < typeComboBox.Items.Count; indexInteger++)

        {
            yachtTypesString=typeComboBox.Items[indexInteger].ToString();
            MessageBox.Show(yachtTypesString);
        }

       }

      

+3


source to share


4 answers


Do it like this,

    StringBuilder yachtTypesString = new StringBuilder();
    for (int indexInteger = 0; indexInteger < typeComboBox.Items.Count; indexInteger++)
    {
        yachtTypesString.AppendLine(typeComboBox.Items[indexInteger].ToString());
    }
    MessageBox.Show(yachtTypesString.ToString());

      



NOTE. Do not do string concatenation with a string, use a StringBuilder object, as this creates a new instance on the string.

+3


source


You can try using Linq:

  MessageBox.Show(String.Join(Environment.NewLine, typeComboBox.Items.Cast<String>()));

      



and let him do all the work for you

+1


source


try it

            string yachtTypesString="";

    for (int indexInteger = 0; indexInteger < typeComboBox.Items.Count; indexInteger++)

    {
        yachtTypesString=yachtTypesString +  typeComboBox.Items[indexInteger].ToString();

    }

     MessageBox.Show(yachtTypesString);

      

0


source


list of all list items with a list in one message box

Please play with the code below to get Combobox all text

private void Form1_Load (object sender, EventArgs e) {DataTable dtcheck = new DataTable (); dtcheck.Columns.Add ("ID"); dtcheck.Columns.Add ("Name"); for (int i = 0; i <= 15; i ++) {dtcheck.Rows.Add (i, "A" + i); }

        comboBox1.ValueMember = "ID";
        comboBox1.DisplayMember = "Name";
        comboBox1.DataSource = dtcheck;


    }
}

    private void button1_Click(object sender, EventArgs e)
    {

        string MessageText = string.Empty;

        foreach(object item in comboBox1.Items)
        {
           DataRowView row = item as DataRowView;

           MessageText += row["Name"].ToString() + "\n";
        }

        MessageBox.Show(MessageText, "ListItems", MessageBoxButtons.OK,MessageBoxIcon.Information);
    }

      

0


source







All Articles