Question

I am bolding dates from within the DateChanged event of the MonthCalendar control in C-Sharp under Visual Studio 2010.

Changing months by clicking previous/next month causes a beep. Everything works, though.

How can I avoid /squelch the beep sound?

The problem is ultra simple to replicate.

  1. Create a new dummy WinForm project
  2. Add a MonthCalendar Control to the form
  3. Add a DateChanged event
  4. Inside the DateChanged event, add the following code:

    DateTime[] dteBolded = { new DateTime(2012, 9, 28, 0, 0, 0), new DateTime(2012, 9, 21, 0, 0, 0) };
    
    this.monthCalendar1.RemoveAllBoldedDates();
    this.monthCalendar1.BoldedDates = dteBolded;
    
  5. Run, press play inside VS2010.

  6. Click previous/next month. You can hear the beep.

How do I solve the problem?

Thanks in advance,

Sarah

Was it helpful?

Solution

The BoldedDates property is special. Support for it is turned on with a style flag (MCS_DAYSTATE) that is specified when the native Windows control is created. Your code changes the property which forces the native control window to be re-created. There is one obvious way to see that, you'll see it flickering pretty noticeably.

The beep is produced because it is now a new control that gets the input event. It's confused and beeps.

The workaround is to delay changing the property so the input handling is completed. Which you can elegantly do with the Control.BeginInvoke() method. Like this:

    private void monthCalendar1_DateChanged(object sender, DateRangeEventArgs e) {
        this.BeginInvoke(new Action(() => {
            DateTime[] dteBolded = { new DateTime(2012, 9, 28, 0, 0, 0), new DateTime(2012, 9, 21, 0, 0, 0) };

            this.monthCalendar1.RemoveAllBoldedDates();
            this.monthCalendar1.BoldedDates = dteBolded;
        }));
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top