Click here to Skip to main content
15,846,571 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi. I wanted to create a GUI program as a personal project in C++ using visual studio. This is what the program does: Lets say you run the program at 5:37pm. The program will check off a box that says "you are in hour five". This is because the hour of the day in which you decide to run the program is in the 5th hour. If you run the program at 11:23am, the program will check off a box that says "you are in hour 11". This is because the hour of the day in which you decide to run the program is in the 11th hour. Also, a label at the top of the window will show the current local time.

For this to work, I know need to make 12 check boxes for each hour of the day. I also need to disable the check boxes so that the user can not effect the check boxes. Next, the computer needs to check off the box based off of the local time. Lastly, I want a button that will refresh the check boxes based off of the new time and refreshes the label that shows the current local time(ex: If the user opens the app at 8:00pm the check box that says "you are in hour 8" will be checked off. If the user leaves the GUI program on for an hour, the refresh button should check off "you are in hour 9" because by then it will be 9:00pm. Then it should change the label to the new local time). I don't know some of the syntax in C++ to do this. Thank you in advance to anyone who can help me.

What I have tried:


			//Shows current time in label 2
			DateTime datetime = DateTime::Now;
			this->label2->Text = datetime.ToString("hh:mm");

			checkBox1->Enabled = false; //disables checkbox for hour 1
			checkBox2->Enabled = false; //disables checkbox for hour 2
			checkBox3->Enabled = false; //disables checkbox for hour 3
			checkBox4->Enabled = false; //disables checkbox for hour 4
			checkBox5->Enabled = false; //disables checkbox for hour 5
			checkBox6->Enabled = false; //disables checkbox for hour 6
			checkBox7->Enabled = false; //disables checkbox for hour 7							
			checkBox8->Enabled = false; //disables checkbox for hour 8
			checkBox9->Enabled = false; //disables checkbox for hour 9
			checkBox10->Enabled = false; //disables checkbox for hour 10
            checkBox11->Enabled = false;//disables checkbox for hour 11
            checkBox12->Enabled = false; //disables checkbox for hour 12
			//TODO: Add the constructor code here
/*Here is what I have done for hour 1. If I can get this to work then I can do the same for the other check boxes.*/

private: System::Void checkBox1_CheckedChanged(System::Object^ sender, System::EventArgs^ e) {
	//Controls time for binary clock
	time_t currentTime;
	struct tm* localTime;

	time(¤tTime);                   // Get the current time
	localTime = localtime(¤tTime);  // Convert the current time to the local time

	int Hour = localTime->tm_hour % 12; //Must mod time by 12 to get standard time

	if (Hour = 1) {

		checkBox1->Checked = true; //checks off box
/*This part of the code is for the refresh button. Right now, all it does is updates
the current time in label 2 but does not update the check boxes to reflect the time*/

private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
	DateTime datetime = DateTime::Now;
	this->label2->Text = datetime.ToString("hh:mm");
Updated 20-Dec-19 12:03pm
Rick York 17-Dec-19 20:27pm    
In my opinion, it would be easier to use a CheckListBox for this. Then you have just one control to manage instead of twelve.
tara lara 17-Dec-19 20:49pm    
Ok. I guess that's one thing I could fix. Would I still be able to edit the buttons individually even if they are in a list (and not separately)?
Rick York 18-Dec-19 10:57am    
Yes, you can check individual boxes if you want. There are also functions to check them all and uncheck them all.
tara lara 18-Dec-19 13:22pm    
Do you know what function this is and how to use it for buttons? I don't necessarily want all of the buttons to be checked off but I want to write an "if-statement" that will check off the box according to the local time.
Richard MacCutchan 18-Dec-19 4:30am    
5:37 pm is hour 17 not 5.

1 solution

To enable or disable a button you can call EnableWindow.

To set a checkbox you can call SetCheck( BST_CHECKED );

To check a box in a CheckListBox you can call SetCheck();
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900