Articles in this section
Category / Section

How to use shift key to select multiple dates in WinForms MonthCalendarAdv?

5 mins read

Select multiple dates

To achieve multiple selection using Shift key in WinForms MonthCalendarAdv  instead of control key, you need to access the GridControl from the MonthCalendarAdv control. Then you need to handle GridCellClick event and add selected cells in Selections list of the grid. Please refer to the following code snippet.

C#

public void SetUpMonthCalendar(MonthCalendarAdv ctrlMonthAdv)
{
   ctrlMonthAdv.AllowMultipleSelection = false;
   foreach (Control ctl1 in ctrlMonthAdv.Controls)
   {
      GridControl _crlGrid = ctl1 as GridControl;
      if (_crlGrid != null)
      {
         _crlGrid.Focus();
         _crlGrid.MouseDown += new MouseEventHandler(GridMouseDownForSelection);
         _crlGrid.PrepareChangeSelection += new GridPrepareChangeSelectionEventHandler(_crlGrid_PrepareChangeSelection);
         _crlGrid.PrepareClearSelection += new EventHandler(_crlGrid_PrepareClearSelection);
         _crlGrid.AllowSelection = GridSelectionFlags.AlphaBlend | GridSelectionFlags.Shift;
         _crlGrid.MouseMove += new MouseEventHandler(_crlGrid_MouseMove);
         _crlGrid.CellClick += new GridCellClickEventHandler(_crlGrid_CellClick);
         break;
      }
   }
}
//Grid CellClick event
void _crlGrid_CellClick(object sender, GridCellClickEventArgs e)
{
   GridControl _crlGrid = sender as GridControl;
   if (Control.ModifierKeys == Keys.Shift)
   {
      GridRangeInfo range = _crlGrid.CurrentCell.RangeInfo;
      if (IsFirstClick)
      {
         range = GridRangeInfo.Cell(FirstRowIndex, FirstColIndex);
         IsFirstClick = false;
      }
      int row, col;
      if (_crlGrid.PointToRowCol(e.MouseEventArgs.Location, out row, out col))
      {
          //Adding selected cells in Grid selection list
          _crlGrid.Model.Selections.Add(GridRangeInfo.Cells(range.Top, range.Left, row, col));
       }
       _crlGrid.Refresh();
   }
}

VB

Public Sub SetUpMonthCalendar(ByVal ctrlMonthAdv As Syncfusion.Windows.Forms.Tools.MonthCalendarAdv)
    ctrlMonthAdv.AllowMultipleSelection = False
    For Each ctl1 As Control In ctrlMonthAdv.Controls
       Dim _crlGrid As Syncfusion.Windows.Forms.Grid.GridControl = CType(IIf(TypeOf ctl1 Is Syncfusion.Windows.Forms.Grid.GridControl, ctl1, Nothing),  Syncfusion.Windows.Forms.Grid.GridControl)
        If Not _crlGrid Is Nothing Then
           _crlGrid.Focus()
           _crlGrid.AllowSelection = Syncfusion.Windows.Forms.Grid.GridSelectionFlags.AlphaBlend Or Syncfusion.Windows.Forms.Grid.GridSelectionFlags.Shift
           AddHandler _crlGrid.MouseMove, AddressOf _crlGrid_MouseMove
           AddHandler _crlGrid.CellClick, AddressOf _crlGrid_CellClick
           Exit For
        End If
    Next ctl1
End Sub
‘Grid CellClick event
Private Sub _crlGrid_CellClick(ByVal sender As Object, ByVal e As Syncfusion.Windows.Forms.Grid.GridCellClickEventArgs)
     Dim _crlGrid As Syncfusion.Windows.Forms.Grid.GridControl = CType(IIf(TypeOf sender Is Syncfusion.Windows.Forms.Grid.GridControl, sender, Nothing), Syncfusion.Windows.Forms.Grid.GridControl)
     If Control.ModifierKeys = Keys.Shift Then
        Dim range As Syncfusion.Windows.Forms.Grid.GridRangeInfo = _crlGrid.CurrentCell.RangeInfo
        If IsFirstClick Then
          range = Syncfusion.Windows.Forms.Grid.GridRangeInfo.Cell(FirstRowIndex, FirstColIndex)
          IsFirstClick = False
        End If
        Dim row, col As Integer
        If _crlGrid.PointToRowCol(e.MouseEventArgs.Location, row, col) Then
           ‘Adding selected cells in Grid selection list
          _crlGrid.Model.Selections.Add(Syncfusion.Windows.Forms.Grid.GridRangeInfo.Cells(range.Top, range.Left, row, col))
        End If
     _crlGrid.Refresh()
     End If
End Sub

 

Conclusion

I hope you enjoyed learning how to use shift key to select multiple dates in WinForms MonthCalendarAdv.

You can refer to WinForms Calendar feature tour page to know about its other groundbreaking feature representations.You can also explore our documentation, to understand how to create and manipulate data.

For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.

If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forumsDirect-Trac, or feedback portal. We are always happy to assist you!




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