[英]Update listbox output with a textbox
我正在嘗試使用文本框+按鈕更新.csv文件的列表框輸出。 我應該使用TryParse來更新列表框中所選項目的“數量”。 我不太確定如何實現TryParse並更新列表框中的“ quantity”值。
目前,這是我為按鈕編寫的代碼:
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
}
}
}
你可以用
else if (!qtySoldTextBox.Text == decimal.TryParse(qtySoldTextBox.Text))
在其他情況下,然后
else if (qtySoldTextBox.Text == decimal.TryParse(qtySoldTextBox.Text))
在第二個。 如果不解析,則第一個命中為true。 如果第二次得到確認,則可以更新數量。 然后只更新您的消息以反映發生了什么。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.