Pages

Saturday, November 5, 2011

DateTimePicker Essentials in Visual Basic.NET

DateTimePicker plays essential role in the system development; it is very useful if you need to input a date, time or even selecting a date range in your data base. DateTimePicker control in one way or another is comparable to MonthCalendar Control, these controls both allows you to select a date. 

Like any other controls in Visual Basic there are a lot of methods on how you can manipulate your DateTimePicker. Here are some of the essentials that you may need to know on how to use and control the DateTimePicker.

1.       You can find the DateTimePicker control under Common Controls in the Toolbox.
2.       By default a DateTimePicker Format is LONG. (Refer to the image below). You can change the format by selecting the Format in Properties Windows and change the value. There are 4 formats to choose from, LONG, SHORT, TIME and CUSTOM.


3.       DateTimePicker1 is the Default Name of the control.
4.       In order to change the display font, find CalendarFont in the Properties Window.
5.       The default value of the control is based on system date.
6.       To display the DateTimePicker value in a TextBox you can use the following codes.

a.       SHORT DATE STRING FORMAT
   TextBox1.Text = DateTimePicker1.Value.ToShortDateString

b.      LONG DATE STRING FORMAT
  TextBox1.Text = DateTimePicker1.Value.ToLongDateString

c.       SHORT TIME STRING FORMAT
  TextBox1.Text = DateTimePicker1.Value.ToShortTimeString

d.      LONG TIME STRING FORMAT
  TextBox1.Text = DateTimePicker1.Value.ToLongTimeString

7.       You can also add days, month, and year in your DateTimePicker Value. Below is a sample syntax on how you can add a days.

ADDING DAYS
SYNTAX:
 DateTimePicker.Value.AddDays(Value As Double)
SAMPLE CODE:
DateTimePicker1.Value = DateTimePicker1.Value.AddDays(10)

ADDING MONTHS
SYNTAX:
DateTimePicker.Value.AddDays(Monhts as Integer)
SAMPLE CODE:
DateTimePicker1.Value = DateTimePicker1.Value.AddMOnths(5)


ADDING YEARS
SYNTAX:
DateTimePicker.Value.AddDays(Value as Integer)
SAMPLE CODE:
DateTimePicker1.Value = DateTimePicker1.Value.AddYears(2)


These are the basics of DateTimePicker Control. Feel free to comment and ask more about DateTimePicker Control. Happy Coding.

2 comments:

  1. Is it possible to use a DateTimePicker control in a search engine, where the criteria for searching is the DateTimePicker? For instance, you have a DB where you have several info and you wish to call/display that stored info according to the date. Any enlightenment would be highly appreciated!

    ReplyDelete
    Replies
    1. Hi Steve,

      Yes you may use DateTimePicker control as the criteria for searching certain record or records in a database.

      Here are some syntax and samples on how to use it.

      *If you want to search only the specific date you can use something like this:

      SELECT * FROM Database WHERE Date = #" & DateTimePicker1.Value.ToShortDateString & "#

      *But if you want to search a range of date you can use two date time pickers in order to have 2 date parameters in order for you to use the BETWEEN query in your database, it goes something like this:

      SELECT * FROM Database WHERE Date BETWEEN #" & DateTimePicker1.Value.ToShortDateString & "# AND #" & DateTimePicker2.Value.ToShortDateString & "#

      As you can see two date timepickers are used to get the range of date values. Remember that DateTimePicker1 serves as the Starting Date and DateTimePicker2 as the End Date. Also Make sure that the date field you are trying to query is formatted with Date data type.

      I hope this will help you, for any question please feel free to reply again or send a message. Thanks

      *Follow Us in Facebook
      http://www.facebook.com/pages/Troubleshoot-It/295226970517643

      Delete