Search code examples
vb.netdatetimepickersavestate

DateTimePicker VB.Net save value on close


I have 2 Forms.Form 1 is main and holds a button that should show msgbox with date that is selected on datetimepicker which is on Form 2. Date shown on msgbox should be in short format (dd.MM.yyyy.).

On program start datetimepicker should be reset to today and msgbox should show today date unless user selects another date on Form2. If user goes to From 2 and changes date Form 2 should save new value and msgbox should show it after button click on form1. How do i do this?

I made myDate parameter in settings of type "DATE" and i didn't set a value. on form1 load i have: my.Settings.myDate = Today

on Form2 load i have:

datetimepicker1.Value = my.Settings.myDate

on Form2 closing i have:

 my.Settings.myDate = datetimepicker1.Value

This sets date on picker correctly, but when i go to form2 and change value, then close form2 and reopen it it still shows date that i have chosen, but msgbox shows initial value.

Thank you


Solution

  • Change the line where you show the message box to:

    MsgBox(my.Settings.myDate)
    

    Or better yet, change it to the .NET way instead of the VB6 way:

    MessageBox.Show(my.Settings.myDate.ToShortDateString())
    

    The reason it's failing for you, I think, is because you are using the global Form2 reference to the form which may or may not be the same instance of the form that you are showing? It's hard to say without seeing more of your code.