[英]c# removing item from listbox then it will deduct the value from the textbox
在我將一個項目刪除到列表框中后,它還將從文本框中扣除該值。 這是我的代碼。
private void listBox1_DoubleClick(object sender, EventArgs e)
{
int total1 = Convert.ToInt32(txtTotal.Text);
if (listBox1.Text =="item1 600")
{
listBox1.Items.RemoveAt(listBox1.SelectedIndex);
total1 -= 600;
}
}
提前致謝!
您可能應該考慮創建一個包含兩個信息的對象。 使用該對象,您可以覆蓋.ToString()方法以僅顯示Item1並從非查看屬性中獲取600。
就像是:
public class MyItem
{
public string Text {get; set;}
public string Amount {get; set;}
public override string ToString()
{
return Text;
}
}
然后,您可以實例化一個新對象並設置Name和Amount屬性,然后將其添加到列表框中。
當您要刪除項目時,可以從對象中獲取金額,然后從總計中減去。 這將減輕您不得不為列表中的每個項目創建If語句的麻煩。 哪一個
這應該可以正常工作,它將從總的TextBox中刪除600:
private void listBox1_DoubleClick(object sender, EventArgs e)
{
int total1 = int.Parse(txtTotal.Text);
if (listBox1.SelectedValue.ToString() == "item1 600")
{
listBox1.Items.RemoveAt(listBox1.SelectedIndex);
total1 -= 600;
txtTotal.Text = total1 + "";
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.