Hi,


I have some basic knowledge in programming however it is not enough to solve the problem I faced currently. So I would like to ask for the pros for guild and help.

I have attached the print screen of my program's GUI

1)From the GUI, the combobox next to Lamp 1 provide user with 3 options: On, Off, Set

If user choose "Set", user can configure the time interval
e.g. from 8:00 AM to 9.00AM

Else user unable to configure the time interval.


2) When user configured the time interval(e.g. 8:00AM to 9.00AM) and clicked "OK", I need to constantly check the time of my computer and compare with the time interval.

If my computer time >= 8:00AM && my computer time <= 9.00AM
status will be set to "ON"

else
status will be set to "OFF"


Problems:
1) How to constantly (each second)compare the time interval with my computer's current time

Hope to get some advice soon. Thanks :)

Recommended Answers

All 13 Replies

Thanks. I try to look at the link that u posted

Hi,

if i use the code

DateTime saveNow = DateTime.Now;

i could get the current date and time of my computer.

But then,
1)how to obtain the time from UI? because it is separated by different combo box.

Hi,

I wrote the following code

DateTime saveNow = saveNow.Hour;

but there is an error popoing up:
Cannot implicit convert type 'int' to 'System.DateTime'

Please advice.

saveNow.Hour is of type int, you cannot assign an int type to saveNow, which is of type DateTime.
Try out this console app:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            // Get current date and time
            DateTime dt = DateTime.Now;
            int hours = dt.Hour;
            int minutes = dt.Minute;
            Console.WriteLine("It is now {0} hours and {1} minutes.", hours, minutes);
            Console.ReadKey();
        }
    }
}

This is my full source code, hoping some one can point my mistake to me. Thanks

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Timers;


namespace WindowsFormsApplication1
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (comboBox1.Text == "Set")
            {
                comboBox2.Enabled = true;
                comboBox3.Enabled = true;
                comboBox4.Enabled = true;
                comboBox5.Enabled = true;
                comboBox6.Enabled = true;
                comboBox7.Enabled = true;
            }

            else
            {
                comboBox2.Enabled = false;
                comboBox3.Enabled = false;
                comboBox4.Enabled = false;
                comboBox5.Enabled = false;
                comboBox6.Enabled = false;
                comboBox7.Enabled = false;
            }

        }

        private void button1_Click(object sender, EventArgs e)
        {
            DateTime saveNow = saveNow.Hour;
           
        }


    }
}

Thanks. ddanbe. I will try out your sample code.

I still have problem with the code

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Timers;


namespace WindowsFormsApplication1
{
    public partial class Form2 : Form
    {
        public Form2()  
        {
            InitializeComponent();
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (comboBox1.Text == "Set")
            {
                comboBox2.Enabled = true;
                comboBox3.Enabled = true;
                comboBox4.Enabled = true;
                comboBox5.Enabled = true;
                comboBox6.Enabled = true;
                comboBox7.Enabled = true;
            }

            else
            {
                comboBox2.Enabled = false;
                comboBox3.Enabled = false;
                comboBox4.Enabled = false;
                comboBox5.Enabled = false;
                comboBox6.Enabled = false;
                comboBox7.Enabled = false;
            }

        }

        private void button1_Click(object sender, EventArgs e)
        {
            DateTime current = DateTime.Now;
            int current_hour = current.Hour;
            int start_hour   = comboBox2.Text;
            int end_hour     = comboBox5.Text;

            if (current_hour >= start_hour && current_hour <= end_hour)
            {
                textBox1.Text = "On";
            }

            else
                textBox1.Text = "Off";
            
        }
    }
}

the red colour code contributes to error saying that"cannot implicitly convert type "string" to "int".

The problem is I cannot convert text from combobox to int.
So how to fix this problem? Please give me some sample code or correct my code.

Thanks

Use the Parse or TryParse methods of the Int32 structure.

stitch the hour, minute by using Convert. Convert has many methods and ToInt32 is one of them.

Be sure you add exception handling or your Forms.Timer, when you incorporate it, may just stop.

Alteratively, Convert can be used twice to store a startTime and an end time to compare to DateTime.Now

Thanks ddanbe and Venjense. Problem solve with the following code

int start_hour   = int.Parse(comboBox2.Text);
            int end_hour     = int.Parse(comboBox5.Text);
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.