![](/img/trans.png)
[英]How to create a flat columnheader button in listview (Windows Forms C#)
[英]c# windows forms link button to listview
我正在使用C#Windows窗體。 我有多個鏈接到列表視圖的按鈕,當按下按鈕時,會將新項目添加到列表視圖。 列表視圖中的列標題為“名稱”和“金額”。 當按下其他按鈕時,會將另一個項目添加到列表視圖。 我需要幫助的內容如下:當兩次按下同一按鈕時,我希望第二次單擊時金額從“ 1”變為“ 2”。 因此,項目名稱不重復,但數量增加了。 問題是我現在使用文本將按鈕鏈接到鏈接列表,例如(“可口可樂”,“ 1”),其中將商品名稱添加為可口可樂,數量添加為1。我知道這與整數,所以請幫忙!
謝謝
當用戶按下按鈕時,在添加新行之前,只需循環瀏覽所有當前的ListViewItems並檢查它們是否已經具有相同的名稱,如果是,則增加數量列。 否則,請像現在一樣添加行。
bool found = false;
foreach (ListViewItem item in listView1.Items)
{
if (item.Text.Equals("Coke"))
{
int amt = int.Parse(item.SubItems[1].Text);
amt++;
item.SubItems[1].Text = amt.ToString();
found = true;
}
}
if (!found)
{
ListViewItem item = listView1.Items.Add("Coke");
item.SubItems.Add("1");
}
var amount = new Dictionary<string, int>();
Button1_Click()
{
if(amount["Coca Cola"]<=0)
{
add a listview items with amount 0
}
// find the listitem with the value "Coca Cola" using FindItemWithText() Method
// set the value of ++amount["Coca Cola"] to that listitem in the amount field
}
擁有地圖將避免在其他情況下閱讀列表視圖項。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.