【问题标题】:Update listbox output with a textbox使用文本框更新列表框输出
【发布时间】:2018-04-11 19:03:11
【问题描述】:

我正在尝试使用文本框 + 按钮更新 .csv 文件的列表框输出。我应该使用 TryParse 来更新列表框中所选项目的“数量”。我不太确定如何实现 TryParse 并更新列表框中的“数量”值。

这是我目前为按钮编写的代码:

decimal value1;
    private void updateSoldQtyButton_Click(object sender, EventArgs e)
    {
        if (this.outputListBox.SelectedIndex == -1) //checks if an item in the listbox has been selected
        {
            MessageBox.Show("Please selected an inventory item to increment sold qty");
        } else if (qtySoldTextBox.Text == "")
        {
            MessageBox.Show("Please input a numerical value in the corresponding text box");
        } else if (!decimal.TryParse(qtySoldTextBox.Text, out value1))
        {

        }
    }

以下是 .csv 加载到列表框中的方式:

private void InvLoad(Inventory InventoryItems) //method
    {
        OpenFileDialog file = new OpenFileDialog();
        if (file.ShowDialog() == DialogResult.OK)
        {
            try //try catch
            {
                List<Inventory> InventoryList = new List<Inventory>();
                StreamReader ipFile = File.OpenText("inventory_ip.csv"); //input file is the csv
                outputListBox.Items.Clear(); //clear listbox
                ipFile.ReadLine(); //skips the first line
                while (!ipFile.EndOfStream)
                {
                    string[] items; //tokenize
                    string itemz = ipFile.ReadLine();
                    items = itemz.Split(','); //, splits the text into columns
                    Inventory InvList = new Inventory();
                    //fields
                    InvList.ID = items[0];
                    InvList.ItemName = items[1];
                    InvList.StartingQty = int.Parse(items[2]);
                    InvList.MinRestockQty = int.Parse(items[3]);
                    InvList.SoldQty = int.Parse(items[4]);
                    InvList.RestockedQty = int.Parse(items[5]);
                    InvList.UnitPrice = decimal.Parse(items[6]);
                    //formatted string
                    string invOp = InvList.ID.PadRight(12, ' ') + 
                            InvList.ItemName.PadRight(23, ' ') + 
                            InvList.StartingQty.ToString().PadRight(10, ' ') + 
                            InvList.MinRestockQty.ToString().PadRight(10, ' ') + 
                            InvList.SoldQty.ToString().PadRight(10, ' ') + 
                            InvList.RestockedQty.ToString().PadRight(10, ' ') + 
                            InvList.UnitPrice.ToString("c").PadRight(10, ' ');
                    InventoryList.Add(InvList);
                    outputListBox.Items.Add(invOp); //output string invOP to listbox
                }
            } catch (Exception ex)
            {
                MessageBox.Show(ex.Message); //exception msg
            }
        }
    }

【问题讨论】:

    标签: c# textbox listbox tryparse


    【解决方案1】:

    你可以使用

    else if (!qtySoldTextBox.Text == decimal.TryParse(qtySoldTextBox.Text))
    

    在你的第一个 else if 上,然后

    else if (qtySoldTextBox.Text == decimal.TryParse(qtySoldTextBox.Text))
    

    第二个。 如果不解析,第一个命中为真。如果第二次为真,您可以更新数量。 然后只需更新您的消息以反映发生的情况。

    【讨论】:

      猜你喜欢
      • 2013-02-04
      • 2014-04-12
      • 2012-12-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多