private void AddEachSession(int SessionCount, string DayOfWeek) //this method helps to add each session's timespan details into the database { bool IfStartTimeChecked; bool IfEndTimeChecked; for (int i = 0; i < SessionCount; i++) //this loop will go through each dynamically created user control (if it is created) { string ControlName = DayOfWeek + (i + 1); //this is used assign give the control's name if (tblpnlSchedule.Controls.ContainsKey(ControlName)) //this selection is used to find the dynamically created user control fom the Table Layout Panel using it's name { AppointmentTimeSchedule SelectedControls = (AppointmentTimeSchedule)tblpnlSchedule.Controls[ControlName]; //the selected control is assigned to a variable to be used IfStartTimeChecked = SelectedControls.IfStartTimeChecked; //this boolean checks if the StartTime control is checked IfEndTimeChecked = SelectedControls.IfEndTimeChecked; //this boolean checks if the EndTime control is checked if (IfStartTimeChecked || IfEndTimeChecked) //this ensures that only the checked time is sent to the AddToDatabase method to be inserted into the database { DateTime StartTime = DateTime.ParseExact(SelectedControls.StartTime, "hh:mm tt", CultureInfo.CurrentCulture); //this converts the time to a DateTime Format DateTime EndTime = DateTime.ParseExact(SelectedControls.EndTime, "hh:mm tt", CultureInfo.CurrentCulture); //this converts the time to a DateTime Format string StartTimeString = StartTime.ToString("HH:mm"); string EndTimeString = EndTime.ToString("HH:mm"); DateTime StartTime24Hour = DateTime.ParseExact(StartTimeString, "HH:mm", CultureInfo.CurrentCulture); //this converts the 12-hour format into the 24-hour format DateTime EndTime24Hour = DateTime.ParseExact(EndTimeString, "HH:mm", CultureInfo.CurrentCulture); //this converts the 12-hour format into the 24-hour format //we are incrementing the i by 1 is becuase the for loop starts from 0, and the first sesson will be session one AddtoDatabase(DayOfWeek, StartTime24Hour, EndTime24Hour, i + 1); } } } }
private void AddItem(string ScheduleDay, int SessionNumber, int ColumnPosition)//to add each user controls dynamically { //get a reference to the previous existent row's details of the current column RowStyle temp = tblpnlSchedule.RowStyles[tblpnlSchedule.RowCount - 1]; //assigning the name, and it's groupbox title to the dynamically created user control to both visually and programmically keep track of the user control //the groupbox title is generated using the current session number (or SessionCount) UserControl TimeScheduleControl = new AppointmentTimeSchedule() { Name = ScheduleDay + SessionNumber, GroupBoxTitle = "Session " + SessionNumber }; //this is to ensure that all the newly created user control is placed in their correct position (the next empty row in their respective columns) if (SessionNumber >= tblpnlSchedule.RowCount)//this creates a new row in the TableLayout Panel to to accomodate the user control { //increase panel rows count by one tblpnlSchedule.RowCount++; //add a new RowStyle as a copy of the previous row's style tblpnlSchedule.RowStyles.Add(new RowStyle(temp.SizeType, temp.Height)); //adds your control tblpnlSchedule.Controls.Add(TimeScheduleControl, ColumnPosition, tblpnlSchedule.RowCount - 1); } tblpnlSchedule.Controls.Add(TimeScheduleControl, ColumnPosition, SessionNumber); //this places the user control in the already existing row, rather than creating a new row }