分享
三行代码  ›  专栏  ›  技术社区  ›  Mason Boce

如何知道下一个按钮是按c#

  •  0
  • Mason Boce  · 技术社区  · 5 天前

    我正在为一家比萨店开发一个订单接收系统,我想按一下比萨饼的大小,然后将按下的任何尺寸按钮+什么比萨饼添加到列表框中。例如小尺寸+夏威夷风格。到目前为止,我一直在尝试这样做,但它只起一次作用,因为我不确定如何重置布尔值。

        bool ButtonSmallClicked = false;
    
        private void ButtonSmall_Click(object sender, EventArgs e)
        {
            ButtonSmallClicked = true;
        }
    
        private void ButtonHawaiian_Click(object sender, EventArgs e)
        {
            if (ButtonSmallClicked == true)
            {
                listBox1.Items.Add(string.Format("{0} {1}", buttonSmall.Text, buttonHawaiian.Text));
            }
    
    2 回复  |  直到 5 天前
        1
  •  0
  •   Fabio    5 天前

    使用 .Tag 属性并将单击的值保存到变量以供进一步处理。

    public class SelectedPizza
    {
        public string Size { get; set; }
        public string Name { get; set; }
    
        public string Format() => $"{Size} {Name}";
    }
    
    public class MyForm
    {
        private readonly SelectedPizza _selectedPizza;
    
        public MyForm()
        {
            buttonSmallSize.Tag = "Small";
            buttonSmallSize.Click += ButtonSizeClick;
            buttonLargeSize.Tag = "Large";
            buttonLargeSize.Click += ButtonSizeClick;
           
            buttonHawaiian.Tag = "Hawaiian";
            buttonHawaiian.Click += ButtonNameClick;
    
            _selectedPizza = new SelectedPizza();
        }   
    
        private void ButtonSizeClick(object sender, EventArgs e)
        {
            var button = (Button)sender;
            _selectedPizza.Size = button.Tag.ToString();
        }
    
        private void ButtonNameClick(object sender, EventArgs e)
        {
            var button = (Button)sender;
            _selectedPizza.Name = button.Tag.ToString();
            
            listBox1.Items.Add(_selectedPizza.Format());
        }
    }
    
        2
  •  0
  •   shayan kamalzadeh    5 天前

    private void ButtonSmall_Click(object sender, EventArgs e)
        {
            if(ButtonSmallClicked ==true)
                  ButtonSmallClicked = false;
            else
                  ButtonSmallClicked = true;
        }
    

    
     private void ButtonHawaiian_Click(object sender, EventArgs e)
        {
            if (ButtonSmallClicked == true)
            {
                listBox1.Items.Add(string.Format("{0} {1}", buttonSmall.Text, buttonHawaiian.Text));
    ButtonSmallClicked == false;
            }
    }