我如何在 Compact Framework 中循环遍历 ListBox 的选定项?

How can I loop through a ListBox's selected items in Compact Framework?

我从here

改编了以下代码
foreach (String table in tablesToTouch)
{
    foreach (Object selecteditem in listBoxSitesWithFetchedData.SelectedItems)
    {
        site = selecteditem as String;
        hhsdbutils.DeleteSiteRecordsFromTable(site, table);
    }
}

...但是,唉,SelectedItems 成员对我来说似乎不可用:“'System.Windows.Forms.ListBox' 不包含 'SelectedItems' 的定义并且没有扩展方法 'SelectedItems' 可以找到接受类型 'System.Windows.Forms.ListBox' 的第一个参数(您是否缺少 using 指令或程序集引用?)"

另一个建议是:

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

...但我也没有可用的 ListItem。

完成同样事情的解决方法是什么?

更新

我至少可以做两件事(有点笨拙):

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

但我仍然认为必须有比那些更直接的方法。

更新 2

我可以做到(编译):

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

...但我仍然遇到只能对已选择的项目进行操作的问题。

更新 3

由于 CF-Whisperer 说列表框多选在 CF(楔形文字)的阴暗迷宫世界中是不可能的,我将代码简化为:

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);
}

Compact Framework Listbox 仅包含 object 项的列表。它在每个上调用 ToString() 进行显示,但项目在那里。

假设我们有一个对象:

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);
    }
}

然后我们将一些放入 ListBox:

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

它们将显示为列表中的 ToString() 等效项(例如 "One: 1")。

您仍然可以像这样通过强制转换或 as 操作将它们作为源对象进行迭代:

foreach (var item in listBox1.Items)
{
    Console.WriteLine("A: " + (item as Thing).A);
    Console.WriteLine("B: " + (item as Thing).A);
}