locked
VISUAL BASIC - Initialize the DateTimePicker to display a default date?

    Question

  • I'm trying figure out the VB code necessay to initialize the DateTimePicker object at program startup.

    I've got this subroutine, which creates the datetimepicker:

    Public

     

    Class

    Form1

     

        Private Sub DateTimePicker1_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)        Handles

         DateTimePicker1.ValueChanged

     

     

    End

    Sub

    And I want to do something like this, but this doesn't work for me:

     

    Private Sub

    Form_Load()

        DateTimePicker1.Value = (

    DateTime

    .Now.AddYears(-1))

     

    End Sub

     

     
    Friday, August 19, 2011 10:32 PM

Answers

  • Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
     DateTimePicker1.Value = DateTime.Now.AddYears(-1)
    End Sub
    
    
    It works for me, the DateTimePicker is initialised to one year ago. What happens when you try it? Do you get a compile error, or do you see the wrong date in the DateTimePicker.

    Friday, August 19, 2011 11:03 PM
  • Thanks!  That does work for me too.  It was the "Handles" part that I had wrong.  It wasn't throwing an error,

    but it just didn't do anything...

    The only thing now is that it highlights the month when I run it, for some reason.

    Any Ideas on how to get rid of that? 

    So, this is what I ended up with at the top of my code block, just after the Public Class statement:

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            DateTimePicker1.Value = DateTime.Now.AddYears(-1)
         
        End Sub

     

    Hi,

    If you have a TextBox or some other control on your Form as well then you can use something like:>>

    TextBox1.Select()

    so that nothing is highlighted on the DateTimePicker.  :-)

     

    One idea you could use ( if you do not want a TextBox to be seen is to Hide the TextBox too).  :)

     

    Add one TextBox and one DateTimepicker to your Form then try this please:>>

     

     

    Public Class Form1
    
    
    
     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    
    
    
     'Setting application to ENglish USA ( American ):>>
    
     My.Application.ChangeCulture("en-Us")
    
     My.Application.ChangeUICulture("en-Us")
    
     'Setting date to August 20th, 2010:>>
    
     Dim someDate As Date = Convert.ToDateTime("8/20/2010")
    
    
    
     DateTimePicker1.Value = someDate
    
    
    
     TextBox1.Hide()
    
     TextBox1.Select()
    
    
    
     End Sub
    
    
    
    End Class
    
    


     



    Regards,

    profile for John Anthony Oliver at Stack Overflow, Q&A for professional and enthusiast programmers

    Click this link to see the NEW way of how to insert a picture into a forum post.

    Installing VB6 on Windows 7

    App Hub for Windows Phone & XBOX 360 developers.


    Not the most elegant thing in the world, but the concept worked!  I added

    Label1.Select()  because I have some label's on the screen.  I didn't have to do

    a hide at all, because it's a label.  So, thanks!  that worked...

    Saturday, August 20, 2011 5:25 PM

All replies

  • sorry, I'm not at a computer with VS on it, but have you tried .Value.Date

    DateTimePicker1.Value.Date = ...
    

     

    Friday, August 19, 2011 10:51 PM
  • Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
     DateTimePicker1.Value = DateTime.Now.AddYears(-1)
    End Sub
    
    
    It works for me, the DateTimePicker is initialised to one year ago. What happens when you try it? Do you get a compile error, or do you see the wrong date in the DateTimePicker.

    Friday, August 19, 2011 11:03 PM
  • Thanks!  That does work for me too.  It was the "Handles" part that I had wrong.  It wasn't throwing an error,

    but it just didn't do anything...

    The only thing now is that it highlights the month when I run it, for some reason.  Any Ideas on how to get rid

    of that? 

    So, this is what I ended up with at the top of my code block, just after the Public Class statement:

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            DateTimePicker1.Value = DateTime.Now.AddYears(-1)
         
        End Sub

     

    Saturday, August 20, 2011 3:12 PM
  • what exactly are you trying to do?

    you are subtracting a year from the current date, but what do you want the datetimepicker to do at that point?

    it's hard for us to answer your question without knowing what you are trying to accomplish

    Saturday, August 20, 2011 4:07 PM
  • When I run the application with that code. I don't see any highlighting in the DateTimePicker until I click on it. If I click on the calendar control and select a date, the month will be highlighted. If I click on part of the date, (day, month or year), that part of the date is highlighted. I don't think there is any way to stop that (other than setting the Enabled property to False, but then you can't use it to pick a date).
    Saturday, August 20, 2011 4:07 PM
  • I think it might have something to do with this statement, which is down below the Form1_Load that you helped me

    with.

     Private Sub DateTimePicker1_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DateTimePicker1.ValueChanged

      End Sub

    I don't have to click on it.  When I run the program it highlights the month of August, but the year is initialized to 2010, which is

    what I want.  Also, I'm using VB express with .net 4.  Maybe that has something to do with the fact that you don't see the

    highlight?

    Saturday, August 20, 2011 4:25 PM
  • what exactly are you trying to do?

    you are subtracting a year from the current date, but what do you want the datetimepicker to do at that point?

    it's hard for us to answer your question without knowing what you are trying to accomplish


    I just want to programatically initialize the datetimepicker to a date that I specify.  And I think the other gentleman has

    put me on the right track.  Thanks!

    Saturday, August 20, 2011 4:26 PM
  • Thanks!  That does work for me too.  It was the "Handles" part that I had wrong.  It wasn't throwing an error,

    but it just didn't do anything...

    The only thing now is that it highlights the month when I run it, for some reason.

    Any Ideas on how to get rid of that? 

    So, this is what I ended up with at the top of my code block, just after the Public Class statement:

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            DateTimePicker1.Value = DateTime.Now.AddYears(-1)
         
        End Sub

     

    Hi,

    If you have a TextBox or some other control on your Form as well then you can use something like:>>

    TextBox1.Select()

    so that nothing is highlighted on the DateTimePicker.  :-)

     

    One idea you could use ( if you do not want a TextBox to be seen is to Hide the TextBox too).  :)

     

    Add one TextBox and one DateTimepicker to your Form then try this please:>>

     

     

    Public Class Form1
    
     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    
      'Setting application to ENglish USA ( American ):>>
      My.Application.ChangeCulture("en-Us")
      My.Application.ChangeUICulture("en-Us")
      'Setting date to August 20th, 2010:>>
      Dim someDate As Date = Convert.ToDateTime("8/20/2010")
    
      DateTimePicker1.Value = someDate
    
      TextBox1.Hide()
      TextBox1.Select()
    
     End Sub
    
    End Class
    


     



    Regards,

    profile for John Anthony Oliver at Stack Overflow, Q&A for professional and enthusiast programmers

    Click this link to see the NEW way of how to insert a picture into a forum post.

    Installing VB6 on Windows 7

    App Hub for Windows Phone & XBOX 360 developers.

    Saturday, August 20, 2011 4:40 PM
  • Thanks!  That does work for me too.  It was the "Handles" part that I had wrong.  It wasn't throwing an error,

    but it just didn't do anything...

    The only thing now is that it highlights the month when I run it, for some reason.

    Any Ideas on how to get rid of that? 

    So, this is what I ended up with at the top of my code block, just after the Public Class statement:

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            DateTimePicker1.Value = DateTime.Now.AddYears(-1)
         
        End Sub

     

    Hi,

    If you have a TextBox or some other control on your Form as well then you can use something like:>>

    TextBox1.Select()

    so that nothing is highlighted on the DateTimePicker.  :-)

     

    One idea you could use ( if you do not want a TextBox to be seen is to Hide the TextBox too).  :)

     

    Add one TextBox and one DateTimepicker to your Form then try this please:>>

     

     

    Public Class Form1
    
    
    
     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    
    
    
     'Setting application to ENglish USA ( American ):>>
    
     My.Application.ChangeCulture("en-Us")
    
     My.Application.ChangeUICulture("en-Us")
    
     'Setting date to August 20th, 2010:>>
    
     Dim someDate As Date = Convert.ToDateTime("8/20/2010")
    
    
    
     DateTimePicker1.Value = someDate
    
    
    
     TextBox1.Hide()
    
     TextBox1.Select()
    
    
    
     End Sub
    
    
    
    End Class
    
    


     



    Regards,

    profile for John Anthony Oliver at Stack Overflow, Q&A for professional and enthusiast programmers

    Click this link to see the NEW way of how to insert a picture into a forum post.

    Installing VB6 on Windows 7

    App Hub for Windows Phone & XBOX 360 developers.


    Not the most elegant thing in the world, but the concept worked!  I added

    Label1.Select()  because I have some label's on the screen.  I didn't have to do

    a hide at all, because it's a label.  So, thanks!  that worked...

    Saturday, August 20, 2011 5:25 PM
  • Not the most elegant thing in the world, but the concept worked!  I added
    Label1.Select()  because I have some label's on the screen.  I didn't have to do
     a hide at all, because it's a label.  So, thanks!  that worked...

    Hi,

    Can you mark one of the replies "as answer" even if it is your own post above in order to "close" the thread ( in a fashion ) please.

     



    Regards,

    profile for John Anthony Oliver at Stack Overflow, Q&A for professional and enthusiast programmers

    Click this link to see the NEW way of how to insert a picture into a forum post.

    Installing VB6 on Windows 7

    App Hub for Windows Phone & XBOX 360 developers.

    Saturday, August 20, 2011 6:12 PM
  • Done.  Thanks everyone for the help!
    Saturday, August 20, 2011 6:16 PM