2016-03-14 20 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"); 
    } 
}