I have a DateTimePicker and Masked Textbox ,Masked Textbox almost covering all of DateTimePicker ,just down arrow key to select the date into the Masked Textbox.

Question No1. What event should I put the current code?I want the selected date to be inside the Masked textbox after it is being selected.

 DateTimePicker4.Format = DateTimePickerFormat.Custom
        DateTimePicker4.CustomFormat = "dd/MM/yyyy" 
        MT.Text = DateTimePicker4.Text

Question No2.
How can the Null value from Masked Textbox save into the database and retrives the same from database?

My question is - Is using a custom format on the DateTimePicker out of the question?

Why are you using a MTB over the DTP?

You can set custom DTP formats like so:

DateTimePicker1.CustomFormat = "dd/MM/yyyy"

Or you can set it by using the GUI.

Something like this should work.

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Set the format and initialize masked textbox
        DateTimePicker4.Format = DateTimePickerFormat.Custom
        DateTimePicker4.CustomFormat = "dd/MM/yyyy"
        mt.Text = DateTimePicker4.Text
    End Sub

    Private Sub DateTimePicker1_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DateTimePicker4.ValueChanged
        'change masked textbox every time datepicker changes
        mt.Text = DateTimePicker4.Text
    End Sub 

To set the masked textbox to a null value you can use MT.Text = ""

Edited 3 Years Ago by tinstaafl

Begginnerdev I want to use MTB because I want an option to enter Null vlaue into the database and It will not be happening in DTP case ,but in MTB case it is possible.

You're welcome.

On a side note. Using a string variable will acheive what you want without using a control that is basically redundant.

     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Set the format and initialize the variable
        DateTimePicker4.Format = DateTimePickerFormat.Custom
        DateTimePicker4.CustomFormat = "dd/MM/yyyy"
        Dim DateString as String = DateTimePicker4.Text
    End Sub
    Private Sub DateTimePicker1_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DateTimePicker4.ValueChanged
        'change the variable every time datepicker changes
        DateString = DateTimePicker4.Text
    End Sub 

Error is

Insufficient security permissions to set the system date

On this line

DateString = DateTimePicker4.Text

oops put the declaration in the wrong place.
try this:

    Dim DateString as String     
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Set the format and initialize the variable
        DateTimePicker4.Format = DateTimePickerFormat.Custom
        DateTimePicker4.CustomFormat = "dd/MM/yyyy"
        DateString = DateTimePicker4.Text
    End Sub
    Private Sub DateTimePicker1_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DateTimePicker4.ValueChanged
        'change the variable every time datepicker changes
        DateString = DateTimePicker4.Text
    End Sub 

Edited 3 Years Ago by tinstaafl

Now I don't need MaskedTextbox for the Blank date to be entered into the database?
I can use DateString = "" for empty string to be Entered into the database ?

This article has been dead for over six months. Start a new discussion instead.