Question If the DateTimePicker has changed

h-bo

Member
Joined
Feb 2, 2023
Messages
5
Programming Experience
Beginner
I am new and trying to learn C#.
My children gave me the booklet (Learning to program in C #) to teach myself, but I'm getting stuck now.

Now I need to create a form, with a DateTimePicker a Button and a checkBox.

The question is :
If the DateTimePicker (dtpGeboorteDatum) has changed, there must be a check mark in the
associated checkBox (checkBoxA1).”

I got the following example that I need to use.

dtpGeboorteDatum.Enabled = checkBoxA1.Checked;

C#:
        private void btnGeboorteDatum_Click(object sender, EventArgs e)
        {
            DateTime leeftijd = DateTime.Now;
            DateTime geboortedatum = dtpGeboorteDatum.Value;                 

            if (leeftijd <= geboortedatum.AddYears (18))
            {
                dtpGeboorteDatum.Enabled = checkBoxA1.Checked;
                MessageBox.Show("Je mag nog niet auto rijden");
            }
            else
            {
                MessageBox.Show("Je mag auto rijden");
            }
        }
 

Attachments

  • DateTimePicker.PNG
    DateTimePicker.PNG
    2.9 KB · Views: 5
The DateTimePicker has checkbox functionality integrated, with the ShowCheckBox and Checked properties. If you show checkbox and set Checked to false initially the picker appears disabled, then if user select a date (or a date is set programmatically) the checkbox is automatically checked. User can also uncheck the picker at runtime to signal that no date has been set.
 
Hi JohnH
It is correct what you say, but I want checkBoxA1 to be enabled and not the DateTimePicker. Because the question is to use the separate checkBox
 
The question is :
If the DateTimePicker (dtpGeboorteDatum) has changed, there must be a check mark in the
associated checkBox (checkBoxA1).”
How do you define "DateTimePicker has changed" ? The control has a ValueChanged event if that is what you mean. Then you simply put checkBoxA1.Checked = true in that event handler.
 
My english is not very good but I will try to clarify.

Later it will be expanded to multiple checkBoxes

I have three checkBoxes, A1, A2, and A3
If the date of the dtpGeboorteDatum is more than 18 years, then checkBox A1 must be ticked.

If the date of the dtpGeboorteDatum is more than 20 years, checkBox A2 must be ticked.

And then i have to use this code to achieve that
C#:
dtpGeboorteDatum.Enabled = checkBoxA1.Checked;
 
You want to enable or disable the DateTimePicker based on age and the checked state of different checkboxes ?
 
No, the DateTimePicker must always be enabled. When the date changes in DateTimePicker, a checkmark should appear in the checkbox.
 
Then you can see how confusing the question is, with the code dtpGeboorteDatum.Enabled = checkBoxA1.Checked; you are setting dtpGeboorteDatum.Enabled property and getting checkBoxA1.Checked property.

I would set up the answers first:
C#:
var olderThan20 = dtpGeboorteDatum.Value.AddYears(20) < DateTime.Now.Date;
var olderThan18 = dtpGeboorteDatum.Value.AddYears(18) < DateTime.Now.Date;
Then you can use if statement and set the Checked property of checkbox conditionally. Check the largest value olderThan20 first, then olderThan18, else age is less than 18.
 
Thanks for your help. I'm just starting out with C# so it's a bit tricky to ask a question in the right way. but i think i get it now.
 
Most programming languages have assignment statements written as:
C#:
target = source
The target will be changed to the value of source.
 
By the way, you probably don't need if statements here since those results are boolean values:
C#:
checkBoxA1.Checked = olderThan18
checkBoxA2.Checked = olderThan20
I don't know what the A3 was supposed to be.
 
Back
Top Bottom