【问题标题】:Extract a number from a Listbox item从列表框项中提取数字
【发布时间】:2014-02-24 13:11:08
【问题描述】:

我是编程新手,找不到从列表框中取回数字的方法。

我正在尝试从列表框中的选定项目中提取一个数字。

列表框中的信息类型为“Travel: €15.66 on 12/3/13。

private void btnRemove_Click(object sender, RoutedEventArgs e)
{
    decimal inputdata = (decimal)lbxDisplay.SelectedItems;

    //Remove selected items from Listbox
    lbxDisplay.Items.RemoveAt(lbxDisplay.SelectedIndex);

    //Update Expenses Total text block
    tblkTotal.Text = string.Format("Total Expenses\t{0}", expense.DeductedTotal();
}

谢谢

【问题讨论】:

  • 如果列表框中的信息是“Travel: €15.66 on 12/3/13”,你只想要“15.66”吗?
  • @JohnnyC 你为什么要它你只是删除项目,你甚至没有使用那个值
  • 因为是删除功能?
  • 我有一个运行总费用,随着项目从列表中删除,必须向下修改。

标签: c# listbox


【解决方案1】:

您可以在SelecteItem 上使用Regex 来提取号码

private void button1_Click_1(object sender, EventArgs e)
    {

        var sel = listBox1.SelectedItem;
        Regex reg = new Regex(@"[0-9\.]+");
          var res =   reg.Match(sel.ToString());
        //if you want to compute the total 
        double total = 0; 
        var allItems = listBox1.SelectedItems;
        foreach (var item in allItems)
        {
            double dres = double.Parse(reg.Match(sel.ToString()).Value);
            total = dres + total; 

        }
        tblkTotal.Text = string.Format("Total Expenses\t{0}", total);

    }

如果你想要SelectedItems 的总数,而不仅仅是一个项目,你应该在表单的构造函数中做这样的事情

public Form1()
        {
            InitializeComponent();
            listBox1.SelectionMode = SelectionMode.MultiExtended;


        }

【讨论】:

    【解决方案2】:

    获得所需数量/价格的简单方法是使用拆分:

    var inputDataParts = inputdata.Split(' ');
    

    现在第二部分应该是价格

    string price = inputDataParts[1];
    

    现在去掉 Euro-Sign 并修剪它:

    price = price.Replace('€', '').Trim();
    

    现在价格应该保持“15.66” - 如果需要,可以转换它。

    更难的方法是使用 RegEx。

    【讨论】:

      【解决方案3】:

      你可以使用这样的正则表达式(看看那里找到如何使用正则表达式http://msdn.microsoft.com/fr-fr/library/0z2heewz(v=vs.110).aspx):

      string pattern = @"[0-9]+(\.[0-9]+)? ";
      

      然后,您只需要将匹配的字符串转换为双精度:

      double value = Convert.ToDouble(matchedString);
      

      【讨论】:

        【解决方案4】:

        我建议你创建费用类:

        public class Expense
        {
            public Expense(string name, decimal amount, DateTime date)
            {
                Name = name;
                Amount = amount;
                Date = date;
            }
        
            public string Name { get; private set; }
            public decimal Amount { get; private set; }
            public DateTime Date { get; private set; }
        
            public override string ToString()
            {
                return String.Format("{0}: €{1} {2}", 
                                     Name, Amount, Date.ToShortDateString());
            }
        }
        

        然后将列表框绑定到费用对象列表 - 它们都将以Travel: €15.66 on 12/3/13的格式显示:

        var expenses = new List<Expense> {
            new Expense("Travel", 16.5M, new DateTime(2013, 12, 3)),
            new Expense("Sports", 13.0M, new DateTime(2013, 12, 4)),
        };
        
        listBox1.DataSource = expenses;
        

        获取所选费用的金额将很容易,因为所选项目将是Expense对象:

        var expense = (Expense)listBox1.SelectedItem;
        // expense.Amount
        

        注意:您可以创建一些 Display 或 Description 属性,而不是覆盖 ToString() 功能,它们将返回格式化字符串,并将此属性指定为列表框的 DisplayMember 名称。

        【讨论】:

          【解决方案5】:

          试试这个:

          string str = listBox1.SelectedItem.ToString();
          int startIndex = str.IndexOf('€');
          str = str.Substring(startIndex+1).Split(' ')[0].Trim();
          decimal value = Convert.ToDecimal(str);
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2021-05-29
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多