【问题标题】:Get a single string out of a ListBox Item从 ListBox 项中获取单个字符串
【发布时间】:2016-11-06 01:19:48
【问题描述】:

我有一个列表框,里面有 X 个项目。一个项目是像字符串、双倍、双倍一样建立起来的。我希望第二个双精度值较小的项目与它的字符串一起显示在标签中。

示例项目:名称 Value1 Value2 所以每个部分都由空格分隔。该代码仅适用于获取第二个双精度的最小值,但不获取该项目的字符串。

vName 的功能不起作用。

    private void bVergleich_Click(object sender, RoutedEventArgs e)
    {
        if (listBox.Items.Count <= 0)
        {
            MessageBox.Show("Bitte erst Einträge hinzufügen.");
        }
        else
        {
            int anzahl = listBox.Items.Count;
            string str = listBox.Items[anzahl].ToString();
            string vName = str.Substring(0, str.IndexOf(" ") + 1);

            var numbers = listBox.Items.Cast<string>().Select(obj => decimal.Parse(obj.Split(' ').First(), NumberStyles.Currency, CultureInfo.CurrentCulture));

            decimal minValue = listBox.Items.Cast<string>().Select(obj => decimal.Parse(obj.Split(' ').Last(), NumberStyles.Currency, CultureInfo.CurrentCulture)).Min();

            lVergleich.Content = vName + " " + minValue + "€";
        }
    }

任何想法我也可以得到字符串?

【问题讨论】:

  • 我建议使用数据项类(具有适当的属性)而不是字符串作为列表框项。查看 MSDN 上的 Data Templating Overview 文章。
  • 我希望你的值(第二个双精度值)不明显或者?=!
  • @Clemens 除了使用数据模板,您还有其他想法吗?

标签: c# wpf listbox substring


【解决方案1】:

我将尝试使用您的代码示例。您可以使用老派的方法并使用 for 循环遍历所有条目。

private void bVergleich_Click(object sender, RoutedEventArgs e)
{
    if (listBox.Items.Count <= 0)
    {
        MessageBox.Show("Bitte erst Einträge hinzufügen.");
    }
    else
    {
        List<decimal> tmpListe = new List<decimal>();
        int anzahl = listBox.Items.Count;

        for (int i = 0; i < anzahl; i++)
        {
            string str = listBox.Items[i].ToString();
            // collect all the second double values
            tmpListe.Add(decimal.Parse(str.Split(' ').Last(), NumberStyles.Currency, CultureInfo.CurrentCulture));
        }

        // get the minimal value and its index
        decimal minValue = tmpListe.Min();
        int index = tmpListe.IndexOf(tmpListe.Min());

        // use the index to get the name
        string str2 = listBox.Items[index].ToString();
        string vName = str2.Substring(0, str2.IndexOf(" ") + 1);

        // write down your comparison
        lVergleich.Content = vName + " " + minValue + "€";
    }
}

这应该会在您的列表中显示第一个最低值。

我个人还建议使用具有 3 个属性和重写的 ToString 方法的自定义类进行显示。然后收集通用List 中的所有项目并将此列表绑定到ListBox

【讨论】:

  • 谢谢您,完美运行:) 但是您在该行中犯了一个小错误: string vName = str.Substring(0, str2.IndexOf(" ") + 1);它以这种方式显示错误。它适用于: string vName = str2.Substring(0, str2.IndexOf(" ") + 1);
  • 感谢您的提示。典型的复制粘贴错误。我纠正了它。很高兴我能帮上忙
【解决方案2】:

您可以按所需值对集合进行排序,并按顺序获取第一个元素

List<string> list = listBox.Items.ToList();

list.Sort((x1, x2) => decimal.Compare(decimal.Parse(x1.Split(' ')[1]), decimal.Parse(x2.Split(' ')[1])));

string x = list.First();

或者只是

string result = listBox.Items.OrderBy(y =>  decimal.Parse(y.Split(' ')[1])).First();

【讨论】:

  • 无论我尝试使用哪一个,它总是告诉我“它不包含 'ToList'/'OrderBy' 的定义并且没有扩展方法......”尝试了一些不同的 using 语句,但它并没有真正帮助。我该怎么办?
  • listBox.Items 的类型是什么?并确保您参考了 Linq 库
  • 一个 listBox 项由 3 个字符串组成
  • 它是你的类型的 Observable 集合吗?是一个带空格的字符串还是另一种类型?
  • 每个字符串之间是一个空格。只是为了确保,这是我的代码:listBox.Items.Add(sName + " " + "[Annuitätendarlehen]" + " " + Annuitätenrechner.zmErg.ToString("0.00") + "€" + " " + Annuitätenrechner。 zgErg.ToString("0.00") + "€");
猜你喜欢
  • 2012-03-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-09-10
  • 1970-01-01
  • 1970-01-01
  • 2013-03-01
相关资源
最近更新 更多