lara
lara

Reputation: 1

How to make label text(number) change by clicking checkbox?

enter image description here

Teacher gave us an assignment. Checkboxes are lessons that students may choose and labels under them are the free spots left. Basically everytime a lesson(checkbox) selected the number connected to it at label should lessen minus 1. if person unchecks it, number should return to basic.

Sorry for my English, i hope it's understandable.

Upvotes: 0

Views: 656

Answers (2)

大陸北方網友
大陸北方網友

Reputation: 3767

You can try to subscribe to the CheckedChanged event for each checkbox. And use Convert.ToInt32 Method to get the value in labels. Then judge the checkbox selected via swicth statement.

public Form1()
{
    InitializeComponent();

    checkBox1.CheckedChanged += checkBox_CheckedChanged;
    checkBox2.CheckedChanged += checkBox_CheckedChanged;
    checkBox3.CheckedChanged += checkBox_CheckedChanged;
}

private void checkBox_CheckedChanged(object sender, EventArgs e)
{
    if (((CheckBox)sender).Checked)
    {
        switch (((CheckBox)sender).Name)
        {
            case "checkBox1":
                labelofcb1.Text = (Convert.ToInt32(labelofcb1.Text) + 1).ToString();
                break;
            case "checkBox2":
                labelofcb2.Text = (Convert.ToInt32(labelofcb2.Text) + 1).ToString();
                break;
            case "checkBox3":
                labelofcb3.Text = (Convert.ToInt32(labelofcb3.Text) + 1).ToString();
                break;
        }
    }
    else
    {
        switch (((CheckBox)sender).Name)
        {
            case "checkBox1":
                labelofcb1.Text = (Convert.ToInt32(labelofcb1.Text) - 1).ToString();
                break;
            case "checkBox2":
                labelofcb2.Text = (Convert.ToInt32(labelofcb2.Text) - 1).ToString();
                break;
            case "checkBox3":
                labelofcb3.Text = (Convert.ToInt32(labelofcb3.Text) - 1).ToString();
                break;
        }
    }
}

Upvotes: 1

Mohammed
Mohammed

Reputation: 151

That's an example where I picked the checkedchanged property of each of the checkboxs and this is the function in each of them. you can change the initial value or the changed value as you want by changing the 0 or the ++ or the --. You just add the two if conditions from each function to your function and change the name in them to reflect the name of your label.

public partial class Form1 : Form
    {
        int counter=0;
        public Form1()
        {
            InitializeComponent();
            label1.Text = counter.ToString();
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox1.Checked)
            {
                counter++;
                label1.Text = counter.ToString();
            }
            if (!checkBox1.Checked)
            {
                counter--;
                label1.Text = counter.ToString();
            }
        }

        private void checkBox3_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox3.Checked)
            {
                counter++;
                label1.Text = counter.ToString();
            }
            if (!checkBox3.Checked)
            {
                counter--;
                label1.Text = counter.ToString();
            }
        }

        private void checkBox2_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox2.Checked)
            {
                counter++;
                label1.Text = counter.ToString();
            }
            if (!checkBox2.Checked)
            {
                counter--;
                label1.Text = counter.ToString();
            }
        }
    }

Upvotes: 1

Related Questions