How can I make a DateTimePicker display an empty string?

后端 未结 8 605
不知归路
不知归路 2020-11-28 08:12

I would like to be able to display a DateTimePicker that has a default value of nothing, i.e. no date.

For example, I have a start date dtTaskStar

相关标签:
8条回答
  • 2020-11-28 09:06

    The basic concept is the same told by others. But its easier to implement this way when you have multiple dateTimePicker.

    dateTimePicker1.Value = DateTime.Now;
    dateTimePicker1.ValueChanged += new System.EventHandler(this.Dtp_ValueChanged);
    dateTimePicker1.ShowCheckBox=true;
    dateTimePicker1.Checked=false;
    
    
    dateTimePicker2.Value = DateTime.Now;
    dateTimePicker2.ValueChanged += new System.EventHandler(this.Dtp_ValueChanged);
    dateTimePicker2.ShowCheckBox=true;
    dateTimePicker2.Checked=false;
    

    the value changed event function

            void Dtp_ValueChanged(object sender, EventArgs e)
            {
                if(((DateTimePicker)sender).ShowCheckBox==true)
                {
                    if(((DateTimePicker)sender).Checked==false)
                    {
                        ((DateTimePicker)sender).CustomFormat = " ";
                        ((DateTimePicker)sender).Format = DateTimePickerFormat.Custom;
                    }
                    else
                    {
                        ((DateTimePicker)sender).Format = DateTimePickerFormat.Short;
                    }
                }
                else
                {
                    ((DateTimePicker)sender).Format = DateTimePickerFormat.Short;
                }
            }
    

    When unchecked

    When checked

    0 讨论(0)
  • 2020-11-28 09:13

    In case anybody has an issue with setting datetimepicker control to blank during the form load event, and then show the current date as needed, here is an example:

    MAKE SURE THAT CustomFormat = " " has same number of spaces (at least one space) in both methods

    Private Sub setDateTimePickerBlank(ByVal dateTimePicker As DateTimePicker)    
    
        dateTimePicker.Visible = True
        dateTimePicker.Format = DateTimePickerFormat.Custom
        dateTimePicker.CustomFormat = "    " 
    End Sub
    
    
    Private Sub dateTimePicker_MouseHover(ByVal sender As Object, ByVal e As 
                        System.EventArgs) Handles dateTimePicker.MouseHover
    
        Dim dateTimePicker As DateTimePicker = CType(sender, DateTimePicker)
        If dateTimePicker.Text = "    " Then
            dateTimePicker.Text = Format(DateTime.Now, "MM/dd/yyyy")
        End If
    End Sub
    
    0 讨论(0)
提交回复
热议问题