Check timing

I'm sorry to post this question as it might be dumb for everyone, but I am not getting the exact solution.

The question is this: I have a choice of time in my project, it appears after three text fields in the form, if no text is entered in the text field and is entered in submit, it gives a message (confirmation) that the data that needs to be entered. Similarly, if a date is not selected, it should continue.

What is the code for this, code that worked for other text fields and does not work for datetimepicker control:

if (dateInsert.Value.ToString() = string.Empty) { MessageBox.Show("Please select date!"); dateInsert.Focus(); return; } 
+4
source share
5 answers

Please correct the code and see if it works

  if (dateInsert.Value.ToString() == "") { MessageBox.Show("Please select date!"); dateInsert.Focus(); return; } 
+4
source

There is no direct solution for an empty DateTimePicker . The only way to reset DateTimePicker is to set CustomFormat and then set empty space as value.

 dateTimePicker1.Format = DateTimePickerFormat.Custom; dateTimePicker1.CustomFormat = " "; 

Even if you do, the value will be cleared in the control, but if you access the value of the property of the control in the code, it will return the current time. Therefore, your condition will always be false.

 //This is always false dateInsert.Value.ToString() = string.Empty 

Decision

Instead of using Value use Text in the condition.

 if(dateInsert.Text = " ") 
+2
source
 if (string.IsNullOrEmpty(dateInsert.Text) { MessageBox.Show("Please select date!"); dateInsert.Focus(); return; } 

hope this helps someone

+2
source

if you are using visual studio ..... use this code to check an empty text field

 <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" Display="None" ErrorMessage="Select Date" ControlToValidate="dateInsert" ValidationGroup="validation"> </asp:RequiredFieldValidator> 
0
source
 if(datepicker.Text == " ") messagebox.show("Please Select Date"); // this works 100 % 
-1
source

Source: https://habr.com/ru/post/1274614/


All Articles