Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# WinForm
I have one date time picker in my project.
 
in that i validate the choose day always be monday.
 
for that i written a code that is working fine.my code as follows;
 

 private void dt_startweek_ValueChanged(object sender, EventArgs e)
        {
            //Validation Week Start day always be monday
            if (dt_startweek.Value.DayOfWeek != DayOfWeek.Monday)
            {
                MessageBox.Show("Please Select  MONDAY Only", "Monday Not Selected", MessageBoxButtons.OK, MessageBoxIcon.Information);
                //dt_startweek.Visible = true;
                return;
            }
        }  
 
user select any other day other than the MONDAY message will shows Please Select MONDAY Only.
 
when user press ok button.the message pop will close, ok,that is correct.
 
but calendar(datetimepciker also closed);
 
i don't want to visible the datetimepicker (calendar) gets closed when press ok button in the message.
 
how can i do using c#.
 
it is windows appplication.
Posted 5-Feb-13 0:55am
Edited 5-Feb-13 0:57am
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

In the handler, set
DialogResult = DialogResult.None;
This will cancel the OK action.
You probably want this in a Click handler for your OK button, if the validation fails.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

There are a couple of ways to do this ...
 
Nice and simple is to select the control and send it the down key ...
dateTimePicker1.Select();
SendKeys.Send("%{DOWN}");
but there is always the faint danger that the sendkeys may go elsewhere if you have a "clicky" user.
 
Safer way is to send the message directly to the control
using System.Runtime.InteropServices;
...
[DllImport("user32.dll", SetLastError = true)]
private static extern int SendMessage(IntPtr hWnd, uint Msg, int wParam, int lParam);
const int WM_SYSKEYDOWN = 0x0104;
at the top of your class and then after your messagebox
SendMessage(dateTimePicker1.Handle, WM_SYSKEYDOWN, (int)Keys.Down, 0);
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Advertise | Privacy | Mobile
Web04 | 2.8.1411022.1 | Last Updated 5 Feb 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100