比较列表框和列表,然后打印到标签

时间:2018-10-22 23:54:08

标签: c# winforms

我正在使用<?php date_default_timezone_set("America/Los_Angeles"); header("Cache-Control: no-cache"); header("Content-Type: text/event-stream"); //stream new webpages to the main view //if the value for the active page has changed //initialize $myPDO to point at our database $myPDO = new PDO('sqlite:/home/pi/pi_DB.sqlite3'); $previousQuery = $myPDO->query("SELECT ValueInt FROM Configuration WHERE Name = 'IndexPage'"); $previousQueryResult = $previousQuery->fetch(PDO::FETCH_ASSOC); $previousQueryValue = $previousQueryResult['ValueInt']; $currentPDO = new PDO('sqlite:/home/pi/pi_DB.sqlite3'); while (1) { //query the database again $currentQuery = $currentPDO->query("SELECT ValueInt FROM Configuration WHERE Name = 'IndexPage'"); $currentQueryResult = $currentQuery->fetch(PDO::FETCH_ASSOC); $currentQueryValue = $currentQueryResult['ValueInt']; if ($currentQueryValue == $previousQueryValue) { //if the new result is equal to our current result, do nothing also: echo "data: No database change detected. \n\n"; } else { //there is a new value in our index entry, // so we need to to signal index.html to reload itself with updated layout echo "data: " . $currentQueryValue . "\n\n"; $previousQueryValue = $currentQueryValue; } flush(); //need to add some html to indicate when a command has been received by the //page viewswitcher.html that will show us our updated perspective. sleep(1); } 存储诸如露营装备之类的物品。 ListBox将提供帐篷,露营椅,咖啡等。
然后,我创建了一个列表,其中包含每个名为ListBox的商品的价格。

当我双击该条目时,它将出现在另一个lstprices中,同时从ListBox中提取价格并将其添加到lstprices subtotal中。

Label

我假设我会以某种方式向try { if (lstItems.Items.Count > 0) { lstOrder.Items.Add(lstItems.SelectedItem.ToString()); } } catch (Exception ex) { MessageBox.Show(ex.Message); } 添加信息,因为我知道元素编号,但不知道如何在lstOrder.Items中显示它。
我希望这是有道理的。

也适用于此声明:

Label

我双击该项目时错误指出其格式错误。

1 个答案:

答案 0 :(得分:0)

对不起,我无法发表评论,因此必须发布答案:

如果必须跟踪对象的多个属性,则应创建一个对象类(例如,面向对象编程):

public class Product 
{
    public string Name {get;set;}
    public double Price {get;set;}

    public Product(string name, double price) 
    {
       Name = name;
       Price = price
    }
}

这样您就可以做到:Product chair = new Product("Camping Chair", 12.99); 或者您可以填写列表

List<Product> products = new List<Product>()
                         {
                          new Product("Camping Chair", 12.99),
                          new Product("Tent", 25.99)
                         }

然后在列表或datagridview等中显示您的产品。与维护两个包含名称和价格的列表相比,这是一种更好的方法。

这还将使列表框的使用更加轻松,它具有一个名为SelectedItem的属性,您可以将该属性转换为您的类,然后引用它的属性,例如:

    Product selectedProduct = myListbox.SelectedItem as Product;
    myLabel.Text = selectedProduct?.Price.ToString();

编辑:

由于类尚未涵盖,因此您可以使用Dictionary<string,double>来跟踪价格和键(产品名称)或至少使用List<KeyValuePair<string,double>>

下面是一个快速入门示例:

 Dictionary<string, double> products = new Dictionary<string, double>()
        {
            {"Camping Chair",33.0},
            {"Stool",12.99},
        };
 //accessing the price via the key, which 
 var price = products.Where(x => x.Key == "Stool").SingleOrDefault();

 // you could then pass in the selected item of your list box as string
 var price = products.Where(x => x.Key == listbox.SelectedItem.ToString()).SingleOrDefault();

有关dictionary at msdn

的更多信息

有关KeyValuePair at msdn

的更多信息