コード例 #1
0
        /// <summary>
        /// Key has been pressed down.
        /// </summary>
        /// <param name="c">Reference to the source control instance.</param>
        /// <param name="e">A KeyEventArgs that contains the event data.</param>
        /// <exception cref="ArgumentNullException"></exception>
        public virtual void KeyDown(Control c, KeyEventArgs e)
        {
            Debug.Assert(c != null);
            Debug.Assert(e != null);

            // Validate incoming references
            if (c == null)
            {
                throw new ArgumentNullException(nameof(c));
            }

            if (e == null)
            {
                throw new ArgumentNullException(nameof(e));
            }

            switch (e.KeyCode)
            {
            case Keys.Enter:
            case Keys.Space:
                // Only interested in enabled items
                if (_menuRadioButton.ItemEnabled)
                {
                    PressMenuRadioButton(true);
                }

                break;

            case Keys.Tab:
                ViewManager.KeyTab(e.Shift);
                break;

            case Keys.Home:
                ViewManager.KeyHome();
                break;

            case Keys.End:
                ViewManager.KeyEnd();
                break;

            case Keys.Up:
                ViewManager.KeyUp();
                break;

            case Keys.Down:
                ViewManager.KeyDown();
                break;

            case Keys.Left:
                ViewManager.KeyLeft(true);
                break;

            case Keys.Right:
                ViewManager.KeyRight();
                break;
            }
        }
コード例 #2
0
        /// <summary>
        /// Key has been pressed down.
        /// </summary>
        /// <param name="c">Reference to the source control instance.</param>
        /// <param name="e">A KeyEventArgs that contains the event data.</param>
        /// <exception cref="ArgumentNullException"></exception>
        public virtual void KeyDown(Control c, KeyEventArgs e)
        {
            Debug.Assert(c != null);
            Debug.Assert(e != null);

            // Validate incoming references
            if (c == null)
            {
                throw new ArgumentNullException(nameof(c));
            }

            if (e == null)
            {
                throw new ArgumentNullException(nameof(e));
            }

            switch (e.KeyCode)
            {
            case Keys.Enter:
            case Keys.Space:
                // Only interested in enabled items
                if (_menuItem.ItemEnabled)
                {
                    // Do we press the item or show the sub menu?
                    if (!_menuItem.HasSubMenu)
                    {
                        PressMenuItem();
                    }
                    else
                    {
                        _menuItem.ShowSubMenu(true);
                    }
                }
                break;

            case Keys.Tab:
                ViewManager.KeyTab(e.Shift);
                break;

            case Keys.Home:
                ViewManager.KeyHome();
                break;

            case Keys.End:
                ViewManager.KeyEnd();
                break;

            case Keys.Up:
                ViewManager.KeyUp();
                break;

            case Keys.Down:
                ViewManager.KeyDown();
                break;

            case Keys.Left:
                // We wrap if are the first context menu shown, rather than a sub menu showing
                if (ViewManager.KeyLeft(!_menuItem.HasParentMenu))
                {
                    // User tried to fall off the left edge, so dismiss ourself and let the
                    // keyboard access take us back to the owning context menu instance
                    _menuItem.DisposeContextMenu();
                }
                break;

            case Keys.Right:
                // If enabled and with a sub menu, then show the sub menu
                if (_menuItem.ItemEnabled && _menuItem.HasSubMenu)
                {
                    _menuItem.ShowSubMenu(true);
                }
                else
                {
                    ViewManager.KeyRight();
                }

                break;
            }
        }
コード例 #3
0
        /// <summary>
        /// Key has been pressed down.
        /// </summary>
        /// <param name="c">Reference to the source control instance.</param>
        /// <param name="e">A KeyEventArgs that contains the event data.</param>
        /// <exception cref="ArgumentNullException"></exception>
        public virtual void KeyDown(Control c, KeyEventArgs e)
        {
            Debug.Assert(c != null);
            Debug.Assert(e != null);

            // Validate incoming references
            if (c == null)
            {
                throw new ArgumentNullException(nameof(c));
            }

            if (e == null)
            {
                throw new ArgumentNullException(nameof(e));
            }

            if (ViewManager != null)
            {
                switch (e.KeyCode)
                {
                case Keys.Tab:
                    ViewManager.KeyTab(e.Shift);
                    return;
                }
            }

            // Get the current focus date
            DateTime focusDate  = _months.FocusDay ?? (_monthCalendar?.SelectionStart ?? _months.Calendar.SelectionStart);
            DateTime anchorDate = _months.AnchorDay ?? (_monthCalendar?.SelectionStart ?? _months.Calendar.SelectionStart);

            // Use keyboard to modify the new focus date
            switch (e.KeyCode)
            {
            case Keys.Left:
                focusDate = e.Control ? focusDate.AddMonths(-1) : focusDate.AddDays(-1);

                break;

            case Keys.Right:
                focusDate = e.Control ? focusDate.AddMonths(1) : focusDate.AddDays(1);

                break;

            case Keys.Up:
                focusDate = focusDate.AddDays(-7);
                break;

            case Keys.Down:
                focusDate = focusDate.AddDays(7);
                break;

            case Keys.Home:
                if (e.Control)
                {
                    focusDate = focusDate.AddMonths(-1);
                    focusDate = new DateTime(focusDate.Year, focusDate.Month, 1);
                }
                else
                {
                    focusDate = new DateTime(focusDate.Year, focusDate.Month, 1);
                }

                break;

            case Keys.End:
                if (e.Control)
                {
                    focusDate = focusDate.AddMonths(1);
                    focusDate = new DateTime(focusDate.Year, focusDate.Month, 1);
                    focusDate = focusDate.AddMonths(1).AddDays(-1);
                }
                else
                {
                    focusDate = new DateTime(focusDate.Year, focusDate.Month, 1);
                    focusDate = focusDate.AddMonths(1).AddDays(-1);
                }
                break;

            case Keys.PageUp:
                focusDate = e.Control ? focusDate.AddMonths(-1 * _months.Months) : focusDate.AddMonths(-1);

                break;

            case Keys.PageDown:
                focusDate = e.Control ? focusDate.AddMonths(1 * _months.Months) : focusDate.AddMonths(1);

                break;

            case Keys.Enter:
            case Keys.Space:
                if ((_monthCalendar != null) && _monthCalendar.AutoClose && (_months.Provider != null))
                {
                    // Is the menu capable of being closed?
                    if (_months.Provider.ProviderCanCloseMenu)
                    {
                        // Ask the original context menu definition, if we can close
                        CancelEventArgs cea = new CancelEventArgs();
                        _months.Provider.OnClosing(cea);

                        if (!cea.Cancel)
                        {
                            // Close the menu from display and pass in the item clicked as the reason
                            _months.Provider.OnClose(new CloseReasonEventArgs(ToolStripDropDownCloseReason.Keyboard));
                        }
                    }
                }
                break;
            }

            // If the max selection count is 1 then always treat the new selection as the new focus
            // day we have just calculated. If the shift key is not pressed then definitely treat as
            // a single day selection.
            if ((_months.Calendar.MaxSelectionCount == 1) || !e.Shift)
            {
                _months.AnchorDay = focusDate;
                _months.FocusDay  = focusDate;
                _months.Calendar.SetSelectionRange(focusDate, focusDate);

                if (ViewManager != null)
                {
                    _needPaint(this, new NeedLayoutEventArgs(true));
                }
            }
            else
            {
                DateTime startDate = _months.Calendar.SelectionStart;
                DateTime endDate   = _months.Calendar.SelectionEnd;

                if (focusDate < anchorDate)
                {
                    // Cannot extend selection beyond the max selection count
                    if ((anchorDate - focusDate).Days >= _months.Calendar.MaxSelectionCount)
                    {
                        focusDate = anchorDate.AddDays(-(_months.Calendar.MaxSelectionCount - 1));
                    }

                    startDate = focusDate;
                    endDate   = anchorDate;
                }
                else if (focusDate > anchorDate)
                {
                    // Cannot extend selection beyond the max selection count
                    if ((focusDate - anchorDate).Days >= _months.Calendar.MaxSelectionCount)
                    {
                        focusDate = anchorDate.AddDays(_months.Calendar.MaxSelectionCount - 1);
                    }

                    startDate = anchorDate;
                    endDate   = focusDate;
                }

                _months.AnchorDay = anchorDate;
                _months.FocusDay  = focusDate;
                _months.Calendar.SetSelectionRange(startDate, endDate);

                if (ViewManager != null)
                {
                    _needPaint(this, new NeedLayoutEventArgs(true));
                }
            }
        }