Rivaarn
Rivaarn

Reputation: 13

C# making a text box display what check boxes have been checked

Afternoon folks,

My question concerns check boxes and text boxes.

To give it context i am making a timetabling system that allows users to add timetable events to their own timetables and in order to select what times their event occurs, i have a series of check boxes.

I have 25 check boxes in all and i was wondering if i can make a text box (or label) display the name of the check box that is checked?

For example the user checks Monday at 9:15 and Friday at 2:15, my text box would display Monday 9:15 and Friday 2:15.

Thanks

Upvotes: 0

Views: 8530

Answers (4)

Xaruth
Xaruth

Reputation: 4104

You can use the event CheckedChanged. As you use many checkboxes, you can write only one method and use it for all checkboxes.

I recommand you to use a list of values to stock what you want to show in the textbox then work on it (in my example, I sort the data and concatene them).

public partial class Form1 : Form
{
    private List<String> listData;

    public Form1()
    {
        this.listData = new List<String>();
        InitializeComponent();
    }

    private void checkBoxs_CheckedChanged(object sender, EventArgs e)
    {
        CheckBox theCheckBoxChanged = sender as CheckBox;

        // verify the event is from a checkbox
        if (theCheckBoxChanged != null)
        {
            if (theCheckBoxChanged.Checked)
            {
                // add data
                this.listData.Add(theCheckBoxChanged.Text);
            }
            else
            {
                // remove data
                this.listData.Remove(theCheckBoxChanged.Text);
            }

            this.UpdateTextBox();
        }
    }

    private void UpdateTextBox()
    {
        // be carefull, you can't concatenate if there is no data.
        if (this.listData.Count > 0)
        {
            // sort and concatenate
            this.textBox1.Text = this.listData
                                     .OrderBy(s => s)
                                     .Aggregate((s1, s2) => s1 + " and " + s2);
        }
        else
        {
            this.textBox1.Text = String.Empty;
        }
    }
}

Don't forget to assign the event CheckedChanged of all checkboxes to checkBoxs_CheckedChanged

Upvotes: 0

Tim van Gool
Tim van Gool

Reputation: 305

I'd do some thing like this:

private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
   if(checkBox1.Checked)  
   listbox1.items.add(checkBox1.Text);
}

In this way the date and time will get added to a list. Personaly i think this way works beter as you can see all the dates you list.

Upvotes: 1

Emi987
Emi987

Reputation: 385

I'm sorry cause I can't comment your post, @Rouge. I would like to add

if(checkBox1.Checked) { // your statement }

cause in your case I think the textBox will change its text even when it changes its state from checked to unchecked.

Upvotes: 0

ElectricRouge
ElectricRouge

Reputation: 1279

Do this in checkBox1_CheckedChanged event

 private void checkBox1_CheckedChanged(object sender, EventArgs e)
 {
    if(checkBox1.Checked)  
       textBox1.Text = checkBox1.Text;
 }

Upvotes: 3

Related Questions