Home > Mobile >  Populating "Shift" field automatically based on time of day
Populating "Shift" field automatically based on time of day

Time:09-21

I am creating a WinForms application which will be used to track when an operator performs an inspection and the result, then submit it to a database. I have a button linked to DateTimePickers which serves to update the current date and time of day. My question is, is it possible to link this button to also auto populate the "shift" field I have a ComboBox set up for, based on the time of day? (E.g. 1st = 0700 - 1500, 2nd = 1500 - 2300, 3rd = 2300 - 0700) I considered using a timer object, but am having a problem finding how to link these time ranges to the event. Does anyone have an example of how I would accomplish this?

CodePudding user response:

This is how I ended up doing it:

TimeSpan First_Start = new TimeSpan(7, 0, 0);
TimeSpan First_End = new TimeSpan(14, 59, 59);
TimeSpan Second_Start = new TimeSpan(15, 0, 0);
TimeSpan Second_End = new TimeSpan(22, 59, 59);
TimeSpan Third_Start = new TimeSpan(23, 0, 0);
TimeSpan Third_End = new TimeSpan(06, 59, 59);
TimeSpan Now = DateTime.Now.TimeOfDay;

private void button1_Click(object sender, EventArgs e)
{
    this.dateTimePicker1.Value = DateTime.Now;
    this.dateTimePicker2.Value = DateTime.Now;
    if (Now >= (First_Start) && Now <= (First_End))
    {
        comboBox1.Text = "1st";
    }
    if (Now >= (Second_Start) && Now <= (Second_End))
    {
        comboBox1.Text = "2nd";
    }
    if (Now >= (Third_Start) && Now <= (Third_End))
    {
        comboBox1.Text = "3rd";
    }
}
  • Related