DateTimePicker and MCM_SETDAYSTATE

  • Thread starter Thread starter Peter Larsen [CPH]
  • Start date Start date
P

Peter Larsen [CPH]

Hi,

I'm trying to make some of the days (in a DateTimePicker) to appear bold,
but i can't figure out how to make this work.

I use .Net 3,5, Winforms on Visual Studio 2008.

I have found some code that looks very similar to the following:

IntPtr mHandle = SendMessage(new HandleRef(dateTimePicker1,
dateTimePicker1.Handle), DTM_GETMONTHCAL, IntPtr.Zero, IntPtr.Zero);
uint[] days = new uint[3];
days[1] = 0xff;
int result = SendMessage(new HandleRef(dateTimePicker1, mHandle),
MCM_SETDAYSTATE, 3, days);

The result is zero which indicates an unsuccessfully operation.

My SendMessage looks like this (the last sendmessage):

private static extern int SendMessage(HandleRef hWnd,
int Msg,
int wParam,
uint[] lParam);

I am not sure if this is right.
I have also tried MarshalAs and other stuff, but i'm not sure what is wrong
and where to fix it.

Any idea what to do ?
Thank you in advance.

Best Regards
Peter
 
Hi Peter,

Thank you for using Microsoft Managed Newsgroup Service, I'm Zhi-Xin Ye,
it's my pleasure to work with you on this issue.

As I understand, you want to bold some dates in the MonthCalendar part of
the DateTimePicker.

To bold a particular date you need to send the underlying SysMonthCal32
control the MCM_SETDAYSTATE message where each bit represents a day of the
month. However, this message is really tough to handle in this scenario,
as you might have tried lots of P/Invoke approaches.

After doing some reseach on this issue, I find a managed way to bold the
dates which avoids the Win32 stuff. The trick is placing a MonthCalendar
inside a ToolStripControlHost, and show it right upon the DateTimePicker's
MonthCalendar. Through this approach you can using the
MonthCalendar.BoldedDates, MonthCalendar.MonthlyBoldedDates and
AnnuallyBoldedDates properties to bold whatever dates you like.

The following is a sample code for your information.

public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();

this.dateTimePicker1.DropDown += new
EventHandler(dateTimePicker1_DropDown);
this.dateTimePicker1.CloseUp += new
EventHandler(dateTimePicker1_CloseUp);
this.Load += new EventHandler(Form1_Load);
}

void Form1_Load(object sender, EventArgs e)
{
mc = new MonthCalendar();

//Specify the bolded dates here
mc.BoldedDates = new DateTime[] {
DateTime.Now.AddDays(1),DateTime.Now.AddDays(10) };

mc.DateChanged += new DateRangeEventHandler(mc_DateChanged);

popup = new ToolStripDropDown();
popup.Margin = Padding.Empty;
popup.Padding = Padding.Empty;
ToolStripControlHost host = new ToolStripControlHost(mc);
popup.Items.Add(host);
}

void mc_DateChanged(object sender, DateRangeEventArgs e)
{
this.dateTimePicker1.Value = mc.SelectionStart;
}

ToolStripDropDown popup;
MonthCalendar mc;

void dateTimePicker1_DropDown(object sender, EventArgs e)
{
mc.SelectionStart = this.dateTimePicker1.Value;
mc.SelectionEnd = this.dateTimePicker1.Value;
int y = this.dateTimePicker1.Height -
this.dateTimePicker1.Margin.Vertical;
this.popup.Show(this.dateTimePicker1, 0, y );
}

void dateTimePicker1_CloseUp(object sender, EventArgs e)
{
this.popup.Hide();
}
}


Please try my suggestion and let me know whether it makes sense to you. I
look forward to your reply. Have a nice day!

Best Regards,
Zhi-Xin Ye
Microsoft Managed Newsgroup Support Team

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.

Note: MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 2 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions. Issues of this
nature are best handled working with a dedicated Microsoft Support Engineer
by contacting Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/en-us/subscriptions/aa948874.aspx
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
Hi Zhi-Xin,

Thanks for your reply.
I would have loved to get to know how to use MCM-SETDAYSTATE, but your idea
about combining known controls are great.

I don't need the textbox part of the DateTimePicker, so i have changed it to
use a DropDownButton in a TooStrip control.
Like this :

{
mc2 = new MonthCalendar();
mc2.BoldedDates = new DateTime[]
{DateTime.Now.AddDays(1),DateTime.Now.AddDays(3) };
ToolStripControlHost host2 = new ToolStripControlHost(mc2);
ToolStripDropDown tsdd = new ToolStripDropDown();
toolStripDropDownButton1.DropDown = tsdd;
tsdd.Items.Add(host2);
mc2.DateChanged += new DateRangeEventHandler(mc2_DateChanged);
mc2.DateSelected += new DateRangeEventHandler(mc2_DateSelected);
}

void mc2_DateSelected(object sender, DateRangeEventArgs e)
{
toolStripDropDownButton1.DropDown.Close();
}

void mc2_DateChanged(object sender, DateRangeEventArgs e)
{
toolStripDropDownButton1.Text = mc2.SelectionStart.ToLongDateString();
}

Thanks for your help.

Best Regards
Peter
 
Back
Top