Click here to Skip to main content
14,422,327 members
Rate this:
Please Sign up or sign in to vote.
See more:
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 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;

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.
Updated 5-Feb-13 0:57am
Rate this:
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.
Rate this:
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 ...

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);

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

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