Articles in this section
Category / Section

How to prevent the Calendar popup from hiding upon mouse selection of the "Today" or "None" button?

2 mins read

In the DateTimerPickerAdv, the MonthCalender, popup is hidden upon mouse button selection of the None and Today buttons. The popup visibility can be maintained by the following steps:

  1. Handling the BeforeCloseUp event.
  2. Using the DisplayCalendar function.

 

By using the BeforeCloseUp event handler

In the DataTimePickerAdv, the CalenderPopup is hidden on clicking Today and None buttons. It is possible to maintain the CalenderPopup visibility in this case by handling the BeforeCloseUp event and None and Today buttons’ MouseDown event.

The following code example demonstrates the same.

C#

//Initializes the variable.
private bool preventFromPopup = false;
// Raises the events.
this.dateTimePickerAdv1.PopupWindow.BeforeCloseUp += new CancelEventHandler(PopupWindow_BeforeCloseUp);
this.dateTimePickerAdv1.PopupWindow.Calendar.NoneButton.MouseDown += new MouseEventHandler(NoneButton_MouseDown);
this.dateTimePickerAdv1.PopupWindow.Calendar.TodayButton.MouseDown += new MouseEventHandler(TodayButton_MouseDown);
// Raises the event when the Mouse is pressed on the None Button.
void NoneButton_MouseDown(object sender, MouseEventArgs e)
{
    preventFromPopup = true;
}
// Raises the event when the Mouse pressed on the Today Button.
void TodayButton_MouseDown(object sender, MouseEventArgs e)
{
    preventFromPopup = true;
}
// Prevents the popup window from hiding.
void PopupWindow_BeforeCloseUp(object sender, CancelEventArgs e)
{
    if (preventFromPopup)
        e.Cancel = true;
}

 

VB

'Initializes the variable.
Private preventFromPopup As Boolean = False
'Raises the events.
AddHandler dateTimePickerAdv1.PopupWindow.BeforeCloseUp, AddressOf PopupWindow_BeforeCloseUp
AddHandler dateTimePickerAdv1.PopupWindow.Calendar.NoneButton.MouseDown, AddressOf NoneButton_MouseDown
AddHandler dateTimePickerAdv1.PopupWindow.Calendar.TodayButton.MouseDown, AddressOf TodayButton_MouseDown
'Raises the event when the Mouse pressed on the None Button.
Private Sub NoneButton_MouseDown(ByVal sender As Object, ByVal e As MouseEventArgs)
 preventFromPopup = True
End Sub
'Raises the event when the Mouse pressed on the Today Button.
Private Sub TodayButton_MouseDown(ByVal sender As Object, ByVal e As MouseEventArgs)
 preventFromPopup = True
End Sub
'Prevents the popup window from hiding.
Private Sub PopupWindow_BeforeCloseUp(ByVal sender As Object, ByVal e As CancelEventArgs)
 If preventFromPopup Then
  e.Cancel = True
 End If
End Sub

 

By using the DisplayCalendar method

The purpose of the DisplayCalendar function is to display the MonthCalendar popup programmatically. So, the Popup can be displayed by invoking the DisplayCalendar function in its None and Today button click events.

The following code example demonstrates the same.

C#

// Raises the event.
this.dateTimePickerAdv1.PopupWindow.Calendar.TodayButton.Click += new EventHandler(TodayButton_Click);
this.dateTimePickerAdv1.PopupWindow.Calendar.NoneButtonClick += new EventHandler(Calendar_NoneButtonClick);
//Shows the calendar by using the DisplayMethod.
void Calendar_NoneButtonClick(object sender, EventArgs e)
{
    this.dateTimePickerAdv1.DisplayCalendar();
}
void TodayButton_Click(object sender, EventArgs e)
{
    this.dateTimePickerAdv1.DisplayCalendar();
}

 

VB

'Raises the events.
AddHandler dateTimePickerAdv1.PopupWindow.Calendar.TodayButton.Click, AddressOf TodayButton_Click
AddHandler dateTimePickerAdv1.PopupWindow.Calendar.NoneButtonClick, AddressOf Calendar_NoneButtonClick
'Shows the calendar by using the DisplayMethod.
Private Sub Calendar_NoneButtonClick(ByVal sender As Object, ByVal e As EventArgs)
 Me.dateTimePickerAdv1.DisplayCalendar()
End Sub
Private Sub TodayButton_Click(ByVal sender As Object, ByVal e As EventArgs)
 Me.dateTimePickerAdv1.DisplayCalendar()
End Sub

 

Showing popup window

Figure 1: The popup window does not hide, when clicking on the Today or None button

Sample Links:

C#: DateTimePickerAdv_CalendarPopup_C#

VB: DateTimePickerAdv_CalendarPopup_VB

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please sign in to leave a comment
Access denied
Access denied