Windows Develop Bookmark and Share   
 index > Windows Forms General > DateTimePicker value and text are not matching
 

DateTimePicker value and text are not matching

The value and text of my datetime picker is not matching. I am setting the value and I can see the new value in debug but Text property is set to current date and users see the Text.

This control is not bound to any date.

Here is the designer code,
private System.Windows.Forms.DateTimePicker myDatetimePicker;
this.myDatetimePicker = new System.Windows.Forms.DateTimePicker();

this.myDatetimePicker.CustomFormat = "MM/dd/yyyy";
this.myDatetimePicker.Format = System.Windows.Forms.DateTimePickerFormat.Custom;
this.myDatetimePicker.Location = new System.Drawing.Point(184, 160);
this.myDatetimePicker.Name = "myDatetimePicker";
this.myDatetimePicker.Size = new System.Drawing.Size(170, 21);
this.myDatetimePicker.TabIndex = 66;

and here is how I am setting the new value.

platformBiosDatetextBox.Value = newDateTime;

After setting new value, I can see that new value is updated but text property is still null. If I check the value in another event, I see that value is set to new value but Text property is set to current date.

I tried with other formats like short but that didn't help.

I tried to assign a string to Text property but that didn't help either.

Am I making any mistakes here?

Thanks


Sreekanth
Cheers  Tuesday, March 17, 2009 11:09 PM
The typical explanation for mysterious bugs like this is that you are looking at an instance of the form that is not visible. Most typically created when you use new Form1() when you really need to refer to the running instance of Form1. Add this code to verify if that's the case:

this.Show().

Hans Passant.
nobugz  Wednesday, March 18, 2009 12:30 AM
Thanks for the reply.

I am not doing this during form initialization or during form loading. I am assigning the value in one of the buttons click event.

I tried to this.Show() and myDatetimePicker.Show() after assigning value but I still see the problem.

Sreekanth
Cheers  Wednesday, March 18, 2009 4:55 AM

DateTimePicker bug?

http://social.msdn.microsoft.com/Forums/en-US/vbgeneral/thread/49e10d02-5af4-4b2c-ab78-f9328514ab79/
Thanks, A.m.a.L
A.m.a.L - aditi.com - Think Product  Wednesday, March 18, 2009 5:41 AM

Hi Cheers,

Which version of .NET framework did you use? I cannot reproduce the issue on .NET 3.5.

Actually when the form is load, the value of the DateTimePicker has been set to today and the Text property is also string of today. When I click the button to set Value property of DateTimePicker, the Text property changed to the new string according to the value at once.

Did I miss something to reproduce your problem?

Sincerely,
Kira Qian


Please mark the replies as answers if they help and unmark if they don't.
Kira Qian  Friday, March 20, 2009 3:36 AM

We are changing the issue type to “General Discussion�because you have not followed up with the necessary information. If you have more time to look at the issue and provide more information, please feel free to change the issue type back to “Question�by opening the Options list at the top of the post editor window, and changing the type. If the issue is resolved, we will appreciate it if you can share the solution so that the answer can be found and used by other community members having similar questions.


Please mark the replies as answers if they help and unmark if they don't.
Kira Qian  Tuesday, March 24, 2009 10:05 AM
I have encountered similar problem. It seems to be related to the Visibility of the DateTimePicker. If it is not visible, the Text property does not exists and is not updated. According to MS it is "by design".

I solved the problem by inheriting the DateTimePicker into new control with following code:

Partial Public Class MyDateTimePicker
  Inherits System.Windows.Forms.DateTimePicker 

Protected Overrides Sub OnVisibleChanged(ByVal e As System.EventArgs)
    MyBase.OnVisibleChanged(e)
    If Me.Visible AndAlso Me.Value <> DateTime.Parse(Me.Text) Then
      Select Case Me.Format
        Case DateTimePickerFormat.Custom
          Me.Text = Value.ToString(Me.CustomFormat)
        Case DateTimePickerFormat.Long
          Me.Text = Me.Value.ToLongDateString
        Case DateTimePickerFormat.Short
          Me.Text = Me.Value.ToShortDateString
        Case DateTimePickerFormat.Time
          Me.Text = Me.Value.ToShortTimeString
      End Select
    End If
  End Sub

End Class

OldĹ™ich DlouhĂ˝  Wednesday, September 09, 2009 11:11 AM

You can use google to search for other answers

Custom Search

More Threads

• Web browser list & setings...
• AutoScroll in Panel
• SelectedIndex EASY QUESTION....
• how to get the ContentType (MineType) of a file
• DataGridView Column Reordering via Drag n Drop
• Case Sensitive
• Adding a timer to a windows service created in vb.net
• tabbing across multiple forms
• Image merging.
• add jpeg images to picture box at run time - C#.net