0

I adapted the following code from here

foreach (String table in tablesToTouch)
{
    foreach (Object selecteditem in listBoxSitesWithFetchedData.SelectedItems)
    {
        site = selecteditem as String;
        hhsdbutils.DeleteSiteRecordsFromTable(site, table);
    }
}

...but, alas, the SelectedItems member appears unavailable to me: "'System.Windows.Forms.ListBox' does not contain a definition for 'SelectedItems' and no extension method 'SelectedItems' accepting a first argument of type 'System.Windows.Forms.ListBox' could be found (are you missing a using directive or an assembly reference?)"

Another suggestion was:

foreach(ListItem listItem in listBox1.Items)
{
   if (listItem.Selected == True)
   {
     . . .

...but I also don't have ListItem available.

What is a workaround to accomplish the same thing?

UPDATE

There are at least two (somewhat kludgy) things I could do:

0) Manually keep track of items selected in listBoxSitesWithFetchedData (as they are clicked) and loop through *that* list
1) Dynamically create checkboxes instead of adding items to the ListBox (getting rid of the ListBox altogether), and use the text value of checked checkboxes to pass to the "Delete" method

But I'm still thinking there's got to be a more straightforward way than those.

UPDATE 2

I can do this (it compiles):

foreach (var item in listBoxSitesWithFetchedData.Items)
{
    hhsdbutils.DeleteSiteRecordsFromTable(item.ToString(), table);
}

...but I'm still left with the problem of only acting on the items that have been selected.

UPDATE 3

Since the CF-Whisperer said that listbox multiselection isn't possible in the murky and misty labyrinthine world of CF (cuneiform forms), I simplified the code to:

foreach (String table in tablesToTouch)
{
    // Comment from the steamed coder:
    // The esteemed user will have to perform this operation multiple times if they want 
to delete from multiple sites              
    hhsdbutils.DeleteSiteRecordsFromTable(listBoxSitesWithFetchedData.SelectedItem.ToString(), 
        table);
}
B. Clay Shannon-B. Crow Raven
  • 8,547
  • 144
  • 472
  • 862

1 Answers1

1

The Compact Framework Listbox simply contains a list of object Items. It calls ToString() on each for display, but the items are there.

So let's say we have an object:

class Thing
{
    public string A { get; set; }
    public int B { get; set; }

    public Thing(string a, int b)
    {
        A = a;
        B = b;
    }

    public override string ToString()
    {
        return string.Format("{0}: {1}", B, A);
    }
}

And we throw some into a ListBox:

listBox1.Items.Add(new Thing("One", 1));
listBox1.Items.Add(new Thing("Two", 2));
listBox1.Items.Add(new Thing("Three", 3));

They will show up as the ToString() equivalent in the list (e.g. "One: 1").

You can still iterate across them as the source objects via a cast or as operation like this:

foreach (var item in listBox1.Items)
{
    Console.WriteLine("A: " + (item as Thing).A);
    Console.WriteLine("B: " + (item as Thing).A);
}
ctacke
  • 66,480
  • 18
  • 94
  • 155