How Insert Date and Time in windows Form? | My Coding!

Monday 16 March 2015

// // Leave a Comment

How Insert Date and Time in windows Form?


Hi friends this is our first program so we start with date and time.

The very first thing is that you have to take the new form, I was explained in last tutorial that how to take new form. It will look like below image.




After that follow the below steps
Step 1: Take the 2 Labels from Toolbox (Label is used to display anything in form).
Step 2: Rename the first label as  ”lbDate” which we use to display Date
             To rename Label go to Property Box right side of the window search for Design part.
In “Design” search Name type name of label.
You can change other properties like fonts, font color, font style etc. from property
Step 3:  Double click on form.

After double click code window will be open.  It will look like below image.


Write there below code. (Friend I want to suggest you that don’t copy and paste the code, please write it so that you canremember it for future).

lbDate.Text = Format(Now, "dd-M-yyyy")'To display date lbTime.Text = Format(Now, "hh:mm:sstt")‘To display time 
When we write the above code, date and time will be shown. But time will be static here it will show only time when the application will execute. To show continues clock we need to execute the code “lbTime.Text = Format(Now, "hh:mm:sstt")” every second. For this we need Timer tool.

For timer go to the design tab, then search for Component part, in component part there will be Timer, double click on it.


In above image you can see it. Just like the above image see right side of the window in property box, you can see the Enable property make it “True” and in Interval property make it “1000” .
You can also change the name of Timer in Name property.
After that double click on timer and copy paste the code 

lbTime.Text = Format(Now, "hh:mm:sstt")”.

After completing the above all steps, Run the program by clicking on Start button.


There are different formats for different date and time given below.
lbDate.Text = Format(Now, "dddd")         ‘To display the full name of day like “Monday”
lbDate.Text = Format(Now, "ddd")           ‘To display short day name like “Mon”
lbDate.Text = Format(Now, "MMMM")    ‘To display full name of month like “March”
lbDate.Text = Format(Now, "MMM")       ‘To display short name of month like “Mar”
lbDate.Text = Format(Now, "MM")            ‘To display month in no. format like “03”
lbTime.Text = Format(Now, "hh:mm:sstt")‘To display time in 12 hr format
lbTime.Text = Format(Now, "HH:mm:sstt")‘To display time in 24 hr format

Note: We can also display the Date and time on Textbox, masktext box or on any other text display component.

0 comments:

Post a Comment