This is a migrated thread and some comments may be shown as answers.

DateTimePicker - Prevent Nulls

24 Answers 331 Views
DateTimePicker
This is a migrated thread and some comments may be shown as answers.
Jason Parrish
Top achievements
Rank 1
Jason Parrish asked on 09 Dec 2010, 07:38 PM
How can I prevent the user from clearing out the textboxes in the DateTimePicker?  For example, I always an actual date displayed in the textbox at all times.  Right now, a user can hit the "Delete" key and clear it out, even if MinDate is set.  The value will return the last "good" date thought.

Thank you!
--Jason

24 Answers, 1 is accepted

Sort by
0
Richard Slade
Top achievements
Rank 2
answered on 10 Dec 2010, 11:19 AM
Hi Jason,

The very simplest way I can think of is to stop keyboard input alltogether on the textbox item of the RadDateTimePicker. I.e
Me.RadDateTimePicker1.DateTimePickerElement.TextBoxElement.TextBoxItem.Enabled = False

Let me know if that helps or if you need more information
Richard
0
Jason Parrish
Top achievements
Rank 1
answered on 10 Dec 2010, 04:04 PM
Thank you.  I had done that.  However, I have some users who prefer to type in the date and others that like to use the mouse and picker.

Since the masked textbox does not allow the user to clear it out (and due to the other bug from my other post), I have replaced the datetimepicker with it.
0
Stefan
Telerik team
answered on 15 Dec 2010, 03:24 PM
Hi Jason Parrish,

Thank you for writing.

Richard's approach is a good and simple way of achieving the desired behavior. Please let me know if it works for you and if it does not, please provide me with the full details of your goal.
 
All the best,
Stefan
the Telerik team
Get started with RadControls for WinForms with numerous videos and detailed documentation.
0
Surendar S
Top achievements
Rank 1
answered on 31 Dec 2010, 11:40 AM
Hi Stefan,
    Is it possible to have the user to key in the date in the datetimepicker and does not allow him to delete the same?( I mean other than disabling the textboxelement).

With Regards,
S Surendar.
0
Richard Slade
Top achievements
Rank 2
answered on 31 Dec 2010, 11:43 AM
Hello,

you can disable the textbox item as per the code above, but then the user would not be able to type anything in. You could disable it when they leave the RadDateTimePicker but this then won't allow the user to go back and correct any mistakes. If you can tell me a bit more about what you would like to do then I'll do my best to help
Richard
0
Surendar S
Top achievements
Rank 1
answered on 31 Dec 2010, 12:16 PM
Hi Richard,
    My requirement is 
1. I dont want the user to delete the content in the control
2. I dont want the control to be disabled.


with Regards,
S Surendar
0
Richard Slade
Top achievements
Rank 2
answered on 31 Dec 2010, 12:23 PM
Hello,

At what point do you not what them to no longer be able to type anything? When they leave the control? If they make a mistake whilst typing then they cannot correct the error. If you coud expand on your needs, I'll do my best to help
Richard
0
Surendar S
Top achievements
Rank 1
answered on 31 Dec 2010, 12:47 PM
Hi Richard,
    I want it to happen in the page load event itself. I can allow the user to type but dont want to delete the same.

With Regards,
S Surendar
0
Richard Slade
Top achievements
Rank 2
answered on 31 Dec 2010, 01:22 PM
Hi,

I'm sorry, but I still don't understand what you want to do. I understand that you want the user to type in a value. Are you simply trying to avoid a null value being returned?
Richard
0
Surendar S
Top achievements
Rank 1
answered on 31 Dec 2010, 01:32 PM
Yes richard.
0
Richard Slade
Top achievements
Rank 2
answered on 31 Dec 2010, 02:16 PM
Simply set the null text and null date so when you focus on another control it adds back in a date
Me.RadDateTimePicker1.NullDate = Now
Me.RadDateTimePicker1.NullText = Now.ToShortDateString
Richard
0
Surendar S
Top achievements
Rank 1
answered on 03 Jan 2011, 07:49 AM
Hi Richard,
    I think you are not getting my scenario. If it is my mistake sorry for that and please read below for the correct scenario.
SCENARIO
    I have RadDateTimePicker in my form to select the date.In addition to select the date using mouse, user can also type the date in the RadDateTimePicker TextBox.But i dont want to allow the user to delete the text in the RadDateTimePicker TextBox using keyboard DELETE  Button.
    I tried using null value check in the valuechanged event of RadDateTimePicker. As this event is fired before page load, i am unable to handle this situation.Because in the page initial time the text box value is always null and i set the current date to the control in the page load event only.

    I think now you clear my scenario.Please give a suggestion to resolve this scenario.


With Regards,
S Surendar.


0
Stefan
Telerik team
answered on 04 Jan 2011, 04:55 PM
Hello guys and Happy new year.

Thank you for the clarification, Surendar.

You can prevent the value from being deleted when the Delete button is pressed by subscribing to the ValueChanging event of RadDateTimePickerElement. You can subscribe to this event in the form constructor or in the form Load event:
public Form1()
        {
            InitializeComponent();
            radDateTimePicker1.DateTimePickerElement.ValueChanging += new Telerik.WinControls.UI.ValueChangingEventHandler(DateTimePickerElement_ValueChanging);
        }
 
This event is fired always when a value change occurs. You can simply check if the NewValue that is provided by the ValueChangingEventArgs equals the NullDate of RadDateTimePicker. If this is true, it means that the deletion is in process. In this case you can cancel the operation using the same arguments.

I hope this helps. Let me know if you need further assistance. 
 
Regards,
Stefan
the Telerik team
Q3’10 SP1 of RadControls for WinForms is available for download; also available is the Q1'11 Roadmap for Telerik Windows Forms controls.
0
Jason Parrish
Top achievements
Rank 1
answered on 04 Jan 2011, 05:04 PM
Can I suggest that the datetimepicker utilize the masked textbox or at least implement the same functionality?   The masked textbox does not allow the user to clear text out with the delete key.

Just my 2 cents.... :)
0
Surendar S
Top achievements
Rank 1
answered on 05 Jan 2011, 01:40 PM
HI,
    I have also noticed one more scenario that when i delete the date, it is not allowing me to type anything in the text area of the RadDateTimePicker.
0
Richard Slade
Top achievements
Rank 2
answered on 05 Jan 2011, 01:45 PM
Hello,

As far as I know this was a known issue that is now fixed in the latest version. Which version of the controls are you using? If an older one, I'd suggest upgrading to the latest Q3 2010 SP1 version
Richard
0
Surendar S
Top achievements
Rank 1
answered on 06 Jan 2011, 07:34 AM
HI Stefan,
    What you said in your post is right. Is working for me. I need a clarification. How should i check nullable value in the New value. I get the value as ""1/1/0001 12:00:00 AM" " when i convert it to string. I have set my MinDate as "1/1/2010".
If e.NewValue.ToString = "1/1/0001 12:00:00 AM" Then
              e.Cancel = True
End If
Is the above code what i have did is the right approach?

Hi Richard,
    I am using Q2 2010 version of telerik controls. At this moment i could block the user from deleting the date. So i dont have any problem in that.


With Regards,
S Surendar
0
Richard Slade
Top achievements
Rank 2
answered on 06 Jan 2011, 12:54 PM
Hi Surendar,

As the date could be presented in different formats, it would be best to compare e.NewValue with your mindate as dates rather than strings. See Date Compare on MSDN for more info.
Hope that helps
Richard
0
Stefan
Telerik team
answered on 07 Jan 2011, 03:38 PM
Hello Surendar, 

Thank you for writing back.

You can check if the NewValue equals the NullDate by using the following code snippet. This will let you know that Delete button is pressed:
void DateTimePickerElement_ValueChanging(object sender, Telerik.WinControls.UI.ValueChangingEventArgs e)
{
    if ((DateTime)e.NewValue == radDateTimePicker1.NullDate)
    {
  //do something
    }
}

As to your issue, where you can not type after the date was deleted I am confirming that this was a known issue in a previous release which we have already addressed in our latest Q3 2010 SP1 release.

I hope this helps.
 
Regards,
Stefan
the Telerik team
Q3’10 SP1 of RadControls for WinForms is available for download; also available is the Q1'11 Roadmap for Telerik Windows Forms controls.
0
sanjeev
Top achievements
Rank 1
answered on 22 Mar 2011, 07:34 AM
how to validate maskedEditText box for date.
I am using mask ##/##/####
and mask type is standard.

I want user to enter a valid date.
so please help me how to validate.

Thanks
Sanjeev
0
Richard Slade
Top achievements
Rank 2
answered on 22 Mar 2011, 10:35 AM
Hello Sanjeev,

You cna use the Validating and Validated events to perform the validation, though depending on your situation, you may want to instead look at using the RadDateTimePicker which uses a RadMaskedEditBox under the hood, but also includes the calendar that a user would need for picking a date.

Hope that helps
Richard
0
sanjeev
Top achievements
Rank 1
answered on 22 Mar 2011, 12:46 PM
Thanks Richard for your reply..
But in my situation i have to take mask type as standard and mask should be ##/##/####.I should not take dateTime picker or calender.so accordingly i have to do validation for date.If possible please try to give code also.
Do reply
Thanks
Sanjeev
0
Richard Slade
Top achievements
Rank 2
answered on 22 Mar 2011, 09:34 PM
Hello again,

Here is a very basic exmaple

Form1.Designer.cs
namespace WindowsFormsApplication1
{
    partial class Form1
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;
  
        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }
  
        #region Windows Form Designer generated code
  
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.components = new System.ComponentModel.Container();
            this.radMaskedEditBox1 = new Telerik.WinControls.UI.RadMaskedEditBox();
            this.errorProvider1 = new System.Windows.Forms.ErrorProvider(this.components);
            this.radButton1 = new Telerik.WinControls.UI.RadButton();
            ((System.ComponentModel.ISupportInitialize)(this.radMaskedEditBox1)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.errorProvider1)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.radButton1)).BeginInit();
            this.SuspendLayout();
            // 
            // radMaskedEditBox1
            // 
            this.radMaskedEditBox1.Culture = null;
            this.radMaskedEditBox1.Location = new System.Drawing.Point(12, 22);
            this.radMaskedEditBox1.Mask = "d";
            this.radMaskedEditBox1.MaskType = Telerik.WinControls.UI.MaskType.DateTime;
            this.radMaskedEditBox1.Name = "radMaskedEditBox1";
            this.radMaskedEditBox1.Size = new System.Drawing.Size(100, 20);
            this.radMaskedEditBox1.TabIndex = 0;
            this.radMaskedEditBox1.TabStop = false;
            this.radMaskedEditBox1.Text = "22/03/2011";
            this.radMaskedEditBox1.Value = new System.DateTime(2011, 3, 22, 0, 0, 0, 0);
            // 
            // errorProvider1
            // 
            this.errorProvider1.ContainerControl = this;
            // 
            // radButton1
            // 
            this.radButton1.Location = new System.Drawing.Point(141, 22);
            this.radButton1.Name = "radButton1";
            this.radButton1.Size = new System.Drawing.Size(130, 19);
            this.radButton1.TabIndex = 1;
            this.radButton1.Text = "Submit";
            this.radButton1.Click += new System.EventHandler(this.radButton1_Click);
            // 
            // Form1
            // 
            this.AcceptButton = this.radButton1;
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(283, 67);
            this.Controls.Add(this.radButton1);
            this.Controls.Add(this.radMaskedEditBox1);
            this.Name = "Form1";
            this.Text = "Form1";
            this.Load += new System.EventHandler(this.Form1_Load);
            ((System.ComponentModel.ISupportInitialize)(this.radMaskedEditBox1)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.errorProvider1)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.radButton1)).EndInit();
            this.ResumeLayout(false);
            this.PerformLayout();
  
        }
  
        #endregion
  
        private Telerik.WinControls.UI.RadMaskedEditBox radMaskedEditBox1;
        private System.Windows.Forms.ErrorProvider errorProvider1;
        private Telerik.WinControls.UI.RadButton radButton1;
    }
}

Form1.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
  
namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
  
        private void Form1_Load(object sender, EventArgs e)
        {
            this.radMaskedEditBox1.Value = null;
            this.radMaskedEditBox1.MaskType = Telerik.WinControls.UI.MaskType.DateTime;
            this.radMaskedEditBox1.Mask = "d";
  
            this.radMaskedEditBox1.Validating += new CancelEventHandler(radMaskedEditBox1_Validating);
            this.radMaskedEditBox1.Validated += new EventHandler(radMaskedEditBox1_Validated);
        }
  
        void radMaskedEditBox1_Validating(object sender, CancelEventArgs e)
        {
            DateTime result;
            bool validDate = DateTime.TryParse(this.radMaskedEditBox1.Value.ToString(), out result);
            if (!validDate | DateTime.Compare(result, DateTime.Today) != 0)
            {
                // not valid
                this.errorProvider1.SetError(this.radMaskedEditBox1, "Must be today");
                e.Cancel = true;
            }
        }
  
        void radMaskedEditBox1_Validated(object sender, EventArgs e)
        {
            this.errorProvider1.SetError(this.radMaskedEditBox1, "");
        }
  
        private void radButton1_Click(object sender, EventArgs e)
        {
            if (this.ValidateChildren(ValidationConstraints.None))
            {
                MessageBox.Show("All Valid - Continue");
            }
        }
  
  
    }
}

Hope that helps
Richard
0
Stefan
Telerik team
answered on 24 Mar 2011, 12:58 PM
Hi sanjeev,

Thank you for writing.

Please refer to Richard's answer regarding your question. 

Additionally, I would like to kindly ask you to avoid posting questions regarding different subjects in the same thread. This thread concerns RadDateTimePicker and your question is regarding RadMaskedEditBox. It would be great if post question regarding the masked edit box either in a new thread or if you find a similar thread regarding your issue. This will allow the rest of the community to easily find the thread that they are looking for.

Thank you for the understanding.
 
Best wishes,
Stefan
the Telerik team
Tags
DateTimePicker
Asked by
Jason Parrish
Top achievements
Rank 1
Answers by
Richard Slade
Top achievements
Rank 2
Jason Parrish
Top achievements
Rank 1
Stefan
Telerik team
Surendar S
Top achievements
Rank 1
sanjeev
Top achievements
Rank 1
Share this question
or