2016-03-14 21 views
0

有人可以幫助我在我的問題我在做一個排序系統爲我們的論文到ListView和我不知道什麼,如果3按鈕被點擊C#顯示值,如果3按鈕被點擊

下一步怎麼辦我想要發生的事情: 用戶需要先選擇一個物品,然後他/她將爲該物品選擇一個數量(我有10個數量按鈕:1,2,3,4,5,6,7,8,9 ,0)

所以我加button0因此用戶需要選擇項目,然後單擊button1然後button0具有10數量將在我的listview

顯示現在

1-9按鈕是工作,但我的問題是什麼,如果用戶想要的項目爲10個或更多

所以這裏的量,我有我的代碼項目button1

private void button1_Click(object sender, EventArgs e) 
{ 
    ms = 1; 
} 

和量按鈕

private void button1_Click(object sender, EventArgs e) 
{ 
    if(ms == 1) 
    { 
     ListViewItem item = new ListViewItem(btnms1.Text); 
     item.SubItems.Add("1"); 
     item.SubItems.Add("118"); 
     listView1.Items.Add(item); 
     ms = 0; 
    } 
} 

private void button2_Click(object sender, EventArgs e) 
{ 
    //working 
} 
private void button3_Click(object sender, EventArgs e) 
{ 
    //working 
} 
private void button4_Click(object sender, EventArgs e) 
{ 
    //working 
} 
private void button5_Click(object sender, EventArgs e) 
{ 
    //working 
} 
private void button6_Click(object sender, EventArgs e) 
{ 
    //working 
} 
private void button7_Click(object sender, EventArgs e) 
{ 
    //working 
} 
private void button8_Click(object sender, EventArgs e) 
{ 
    //working 
} 
private void button9_Click(object sender, EventArgs e) 
{ 
    //working 
} 

這裏是0 button

private void button0_Click(object sender, EventArgs e) 
{ 
    // magic please 
} 
+1

如果什麼用戶想'20 +'項目,甚至更多? –

+0

我將使用接受的答案的邏輯 –

+0

從可用性的角度來看,用戶需要輸入一個數量(按下按鈕[0-9] +),然後按另一個按鈕接受輸入,然後輸入可以是視爲用戶想要購買的整數數量。 –

回答

0

我真的不知道你想要達到的目標,但它可能是像這樣的東西。

Sample Form

public partial class Form1 : Form 
{ 
    public Form1() 
    { 
     InitializeComponent(); 
    } 

    private void button1_Click(object sender, EventArgs e) 
    { 
     textBox1.Text += "1"; 
    } 

    private void button2_Click(object sender, EventArgs e) 
    { 
     textBox1.Text += "2"; 
    } 

    private void button3_Click(object sender, EventArgs e) 
    { 
     textBox1.Text += "3"; 
    } 

    private void button4_Click(object sender, EventArgs e) 
    { 
     textBox1.Text += "4"; 
    } 

    private void button5_Click(object sender, EventArgs e) 
    { 
     textBox1.Text += "5"; 
    } 

    private void button6_Click(object sender, EventArgs e) 
    { 
     textBox1.Text += "6"; 
    } 

    private void button7_Click(object sender, EventArgs e) 
    { 
     textBox1.Text += "7"; 
    } 

    private void button8_Click(object sender, EventArgs e) 
    { 
     textBox1.Text += "8"; 
    } 

    private void button9_Click(object sender, EventArgs e) 
    { 
     textBox1.Text += "9"; 
    } 

    private void button10_Click(object sender, EventArgs e) 
    { 
     textBox1.Text += "0"; 
    } 

    private void button11_Click(object sender, EventArgs e) 
    { 
     int quantity = 0; 

     bool quantityParse = int.TryParse(textBox1.Text, out quantity); 

     string productName = "Product Name"; // Sample Name 
     double productPrice = 300; // Sample Price 

     if (quantityParse) 
     { 
      ListViewItem lvi = new ListViewItem(productName); // Name 
      lvi.SubItems.Add(quantity.ToString()); // Quantity 
      lvi.SubItems.Add(productPrice.ToString()); // Price 
      lvi.SubItems.Add((productPrice * quantity).ToString()); // Subtotal 
      listView1.Items.Add(lvi); 
     } 
    } 
} 
+0

也許添加一個清除按鈕,將數量設置回0.我忘了添加這個雖然.. –

+0

謝謝先生!這解決了我的問題 –

0

首先,請開始爲變量選擇更有意義的名稱,因爲它們可以幫助您理解代碼,特別是當您的代碼對您而言過於複雜或者與其他人共享時。其次,您目前如何做這件事並不是最直觀的方法,總是儘量爭取儘可能少的代碼。

我會做的是爲每個數量按鈕調用相同的事件處理程序,但將按鈕中的文本解析爲整數並將其添加到總字符串中。

即:

private string QuantityText = string.Empty; 

private void QtyButton_Click(object sender, EventArgs e) 
{ 
    if (sender is Button) 
    { 
     Button theButton = (Button)sender; 
     string qtyText = theButton.Content.ToString(); 
     QuantityText += qtyText; 
    } 
} 

然後調用這樣的方法,當你要處理的量字符串:

Private Void ProcessQuantity() 
{ 
    int qtyAmount = -1; 
    int.TryParse(QuantityText, out qtyAmount) 
    if (qtyAmount > -1) 
    { 
    //Do Processing here 
    } 
    else 
    { 
    throw new InvalidArgumentException("Quantity is invalid"); 
    } 
}