DateTimePicker in C# windows Form

NazHim 201 Reputation points
2021-08-18T07:46:25.547+00:00

hi All,

i am using C# windows Application.

can possible to.? date field is disable. and only calendar field is enable.?
like image at below.

124221-datetimepicker-124319-02.png

best regards
NazHim

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,008 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Karen Payne MVP 35,436 Reputation points
    2021-08-18T09:57:03.667+00:00

    The easy way is to place a TextBox and Button on the form, show a child form with a month calendar control. In the child form, select a date and click the Select button which sends the selected date to an event which the calling form places in a TextBox. If when calling the child form without a DateTime the current date is used, if there is a DateTime it's used.

    Full source (calendar image is in the assets folder)

    124302-calendar1.png

    124179-calendar2.png

    Form1

    namespace WindowsFormsApp3  
    {  
    public partial class Form1 : Form  
    {  
        public Form1()  
        {  
            InitializeComponent();  
              
            Shown += OnShown;  
        }  
      
        private void OnShown(object sender, EventArgs e)  
        {  
            ActiveControl = ShowCalendarButton;  
        }  
      
        private void ShowCalendarButton_Click(object sender, EventArgs e)  
        {  
            CalendarForm calendarForm = null;  
              
            try  
            {  
                // decide to pass a date time or not  
                calendarForm = DateTime.TryParse(DateTimeValueTextBox.Text, out var currentDateTime) ?   
                    new CalendarForm(currentDateTime) :   
                    new CalendarForm();  
      
                // reposition to be next to calendar button  
                calendarForm.Location = new Point(Left + (Width - 100), Bottom - 80);  
                  
                calendarForm.DateTimeHandler += CalendarFormOnDateTimeHandler;  
                calendarForm.ShowDialog();  
      
            }  
            finally  
            {  
                calendarForm.DateTimeHandler -= CalendarFormOnDateTimeHandler;  
                calendarForm.Dispose();  
            }  
        }  
        // set date time  
        private void CalendarFormOnDateTimeHandler(DateTime sender)  
        {  
            DateTimeValueTextBox.Text = $"{sender.ToString("MM-dd-yyyy")}";  
        }  
    }  
    

    Calendar form

    public partial class CalendarForm : Form  
    {  
        public delegate void OnSelectDateTime(DateTime sender);  
        public event OnSelectDateTime DateTimeHandler;  
          
        public CalendarForm()  
        {  
            InitializeComponent();  
        }  
        public CalendarForm(DateTime sender)  
        {  
            InitializeComponent();  
            monthCalendar1.SetDate(sender);  
        }  
      
        private void SelectButton_Click(object sender, EventArgs e)  
        {  
            DateTimeHandler?.Invoke(monthCalendar1.SelectionRange.Start);  
            Close();  
        }  
    }  
    

    Cheap alternate

    Create a KeyDown event for a DateTimePicker as follows

    private void dateTimePicker1_KeyDown(object sender, KeyEventArgs e)  
    {  
        e.Handled = true;  
    }  
    
    0 comments No comments

  2. Sam of Simple Samples 5,546 Reputation points
    2021-08-18T20:48:11.393+00:00

    You can filter messages using the IMessageFilter Interface. If it is a key down or key up message then do not process it. For the calendar however the user might want to use the arrow keys so you can process the DropDown and CloseUp events and when the calendar is down, allow the keys to be processed. A complete sample is at Repos/Answers/DateDisabled at master · SimpleSamples/Repos.

    public partial class Form1 : Form, IMessageFilter  
    {  
        IntPtr dtphwnd = IntPtr.Zero;  
        bool CalendarDown = false;  
    
        public Form1()  
        {  
            InitializeComponent();  
            dtphwnd = dateTimePicker1.Handle;  
            Application.AddMessageFilter(this);  
        }  
    
        public bool PreFilterMessage(ref Message m)  
        {  
            const int WM_KEYDOWN = 0x0100;  
            const int WM_KEYUP = 0x0101;  
            if (CalendarDown)  
                return false;  
            // Does dateTimePicker1 exist?  
            if (dtphwnd == IntPtr.Zero)  
            {  
                return false;  
            }  
            // Is this message for the dateTimePicker1?  
            if (m.HWnd != dtphwnd)  
            {  
                return false;  
            }  
            // if key up or key down message then do not process it  
            if (m.Msg == WM_KEYDOWN || m.Msg == WM_KEYUP)  
                return true;    // stop it from being dispatched  
            // All other messages are processed  
            return false;  
        }  
    
        private void dateTimePicker1_CloseUp(object sender, EventArgs e)  
        {  
            CalendarDown = false;  
        }  
    
        private void dateTimePicker1_DropDown(object sender, EventArgs e)  
        {  
            CalendarDown = true;  
        }  
    
        private void Form1_FormClosed(object sender, FormClosedEventArgs e)  
        {  
            Application.RemoveMessageFilter(this);  
        }  
    }  
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.