Search code examples
c#labelchecklistbox

Simple order form C# buttons not changing labels


I am trying to create a simple generic order form. What would be the easiest way to do the check boxes so that when you click the boxes they are added to the $2.00 base price when added to cart and represented in the texboxt on the right. I also tried to just change the text of the Total Label when button clicked but it would not change when running. Any Ideas? I attached the picture of the design form so it makes it a little easier to see where I'm trying to go with things.

enter image description here

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

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


    //Add coffee to cart button
    private void Button1_click(object sender, EventArgs e)
    {
        label1.Text = " Change in label";
    }
    //clear right richtext box and total label
    private void Button2_click(object sender, EventArgs e)
    {
        richTextBox1.Text = " ";
    }
    //checkout popup alert with total and message
    private void Button3_click(object sender, EventArgs e)
    {
       System.Windows.Forms.MessageBox.Show("Total here");
    }

    private void tabPage1_Click(object sender, EventArgs e)
    {

    }

    private void label1_Click(object sender, EventArgs e)
    {

    }

    private void label3_Click(object sender, EventArgs e)
    {

    }
}

}


Solution

  • To elaborate on my above answer in the comments, ensure the button's click event has a handler attached to it.

    Such as:

        public Form1()
        {
            InitializeComponent();
            Button1.Click += Button1_click;
        }
    

    You can also set this via the designer by double clicking the control to automatically create the event handler in your code and then modify that. Or you can use the properties panel to do this as well (either select a handler you've created or double click the field to create a new one):

    enter image description here

    Given your code, that should be the only reason this isn't firing, unless something else is hidden from us that we can't comment on further. Hopefully that helps.