Looping through all items in ListBox?

26,412

Solution 1

A more efficient way to write your answer would be like this:

static readonly string[] FieldNames = new string[] { "CustomerID", "Name", "Address", ..., "Email" };

using(SqlBulkCopy bulkCopy = new SqlBulkCopy(sqlConnectionString, options)) {

    bulkCopy.DestinationTableName = "Customers";
    //amount to bulkcopy at once to prevent slowing with large imports
    bulkCopy.BatchSize = 200;

    for(int i = 0; i < FieldNames.Length; i++) {
        bulkCopy.ColumnMappings.Add(
            new SqlBulkCopyColumnMapping(lstOutcome.Items[i].ToString(), FieldNames[i])
        );
    }
}

Solution 2

You need to put the textboxes in an array, like this:

Textbox[] textboxes = new Textbox[] { textbox, textbox2, textbox3 };

for (int i = 0; i < listBox1.Items.Count; i++) {
    textboxes[i].Text = "Item: " + listBox1.Items[i].ToString();
}

Note that if there are more than three items in the listbox, this will fail because it will run out of textboxes. For a more complete solution, please add some context. What are the textboxes for? What is the data in the listbox coming from?

Share:
26,412
SLaks
Author by

SLaks

Schabse Laks Developer, C# MVP (2010 - 2018) Contact: [email protected] Twitter: @Schabse I've been programming since I was 12. I started with VB6, then moved to C# 2 Beta 2 when I was 14. I never liked VB6, but I found my calling in C#, and I've stuck with it ever since. Since then, I've also mastered web development with jQuery. I also wrote a jQuery tutorial. I'm now working at Google within the Google Docs team. My best of SO: Tracking down a bug in the .Net runtime An exercise in psychic debugging (see the question history) Working around a C# compiler limitation Decoding a Javascript virus jQuery selector magic Simple coroutines using iterators Understanding Javascript scoping in inline event handlers WM_NCHITTEST magic A bug in the C# compiler

Updated on July 18, 2022

Comments

  • SLaks
    SLaks almost 2 years

    I have a list box which is populated by this code:

    • lstOutcome.Items.Add(lstMap.SelectedItem.Text);

    In the listbox lstOutcome, I need to be able to loop through the listbox and be able to take the value of the first,second, etc, list items.

    The reason I need to loop through each line and grab the value of that line is so I can use whatever value was in that line for populating something else.

    For example, in my list box I have:

    • 1
    • 2
    • 3

    I need to be able to loop through the listbox on button click and have the values assigned to txtboxes:

    • textbox1.Text = 'item 1 in the listbox';
    • textbox2.Text = 'item 2 in the listbox';
    • textbox3.Text = 'item 3 in the listbox';

    I am not sure if I need an array or how this can be accomplished. The point of this example is that I will actually be using the items in the listbox to map columns. I am importing an Microsoft Excel spreadsheet. In lstMap I have the column names and I am trying to get the column names to match my database. Using this code I am trying to take the values of the listbox:

    foreach(object li in lstOutcome.Items)
    {
        bulkCopy.DestinationTableName = "Customers";
        //Amount to bulkcopy at once to prevent slowing with large imports.
        bulkCopy.BatchSize = 200;
        SqlBulkCopyColumnMapping map1 = new SqlBulkCopyColumnMapping(li.ToString(), "CustomerID");
        bulkCopy.ColumnMappings.Add(map1);
    
    • Admin
      Admin over 14 years
      actually this is a hypothetical example, I am going to be taking the value of the listitems and plugging them into a column mapping, but there will never be more items than textboxes if you think about it like that
    • Admin
      Admin over 14 years
      see what i mean after I put in the update? I will never have more columns in the listbox than columns in the database, but this solution I feel can come easily, I just dont know how to loop thru the listbox and grab EACH list item and assign it a value, like grabbing the text 1 from the listbox and assigning it a value in an array
    • SLaks
      SLaks over 14 years
      It's ASP.Net. No other framework has a ListBoxItem class (lstMap.SelectedItem.Text, near the top)
  • Admin
    Admin over 14 years
    ok I get what you're saying. I am going to do a little tweaking.
  • Admin
    Admin over 14 years
    meh couldnt get it to work, cant interchange textbox with SqlBulkCopyColumnMapping
  • SLaks
    SLaks over 14 years
    You don't need to create an ArrayList. Instead, you can write lstOutcome.Items[4].ToString(). Also, you don't need a separate variable for each mapping. Instead, you can write bulkCopy.ColumnMappings.Add(new SqlBulkCopyColumnMapping(...))