Can you fix this? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Can you fix this?

Button should be enabled in the time of 7:36 AM and it works but my problem is the button will be disabled in 8:00 AM. Can you help me in this. Here is my code: private void timer1_Tick(object sender, EventArgs e) { labelTime.Text = DateTime.Now.ToShortTimeString(); OutBtn.Enabled = (labelTime.Text == "7:36 AM" ); timer1.Start(); }

13th Jun 2021, 2:53 PM
Khem Olayta
Khem Olayta - avatar
4 Answers
+ 1
My problem in here sir I set the labelTime to my form as a time and I use timer_tick when the form is running the labelTime will shown as a time and I have buttons the In and Out. I want to when the time is 7:36 AM to 7:59 AM the button out will be enabled and then when the time is 8:00 AM the button will be disabled The problem in my code is when time turns to 7:37 AM the button out will disable. What can I do for my code?
13th Jun 2021, 3:16 PM
Khem Olayta
Khem Olayta - avatar
0
What is the interval of the timer ? How long should the button be enabled. Can you improve your if statement ?
13th Jun 2021, 3:09 PM
sneeze
sneeze - avatar
0
your code only test for time string equals to "7:36 AM"... you should convert the time string to two numbers (one for hours, the other for minutes) and one boolean (am = true if "AM" in time string else false), and test if you are in the good time range: OutBtn.Enabled = (am && h==7 && m>=36);
13th Jun 2021, 6:24 PM
visph
visph - avatar
0
When comparing date/time. Always convert the string to a datatime-variable. string simpleTime = "1/1/2000"; DateTime time = DateTime.Parse(simpleTime); https://www.dotnetperls.com/datetime-parse After that you can write if label1time > 736time && label1.time < 759time { enable button;} What is the interval of this timer ? Where is the timer started ?
13th Jun 2021, 7:53 PM
sneeze
sneeze - avatar