none
PageUp and PageDown in DataGridView not going to FormKeyDown RRS feed

  • Вопрос

  • Hello all. I have a TabControl with DataGridViews on some of the TabPages. Users want to hit PageUp and PageDown keys to change tab indexes no matter where the focus is. This is implemented in Form_KeyDown() with Keypreview=true. It works if the focus is on a TextBox.

    But if the focus is on a DataGridView, PageUp and PageDown skip Form_KeyDown() and go straight to DataGridView and change the grid row indicator. This is not what the users want.

    I tried to use a.Handled=true and send the same key to the hosting form. but they even skip DataGridView_KeyDown().

    Any idea ? Thanks.

Все ответы

  • It seems that keys cannot be intercepted in certain circumstances, then try an alternative. Add an interface and handle the Load event:

    public partial class MyForm : Form, IMessageFilter

    {

       private void MyForm_Load( object sender, EventArgs e )

       {

          Application.AddMessageFilter( this );

       }

       public bool PreFilterMessage( ref Message m )

       {

          const int WM_KEYDOWN = 0x0100;

          switch( m.Msg )

          {

             case WM_KEYDOWN:

                switch( (Keys)m.WParam )

                {

                case Keys.PageDown:

                   // Change Tab

                   // . . .

                   return true;

                case Keys.PageUp:

                   // Change Tab

                   // . . .

                   return true;

                 }

             break;

          }

          return false;

       }

    }

    Probably you should also identify the context. For example, maybe you should also call dataGrid.EndEdit.

  • Hi AlanChong,

    Did you solve your problem? Please let me know if you need further assistance.

    Best Regards,

    Xingyu Zhao


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Модератор
  • Accelerator Tables - Win32 apps is the Windows API solution for things like that but as far as I know Microsoft does not support that for .Net. You could use DllImport to implement it and I found an article showing how to do it and posted it in a reply a few days ago but I do not know where it is. Are you still looking? Do you want to try keyboard accelerators? If you are not going to try it then I won't bother looking for the article.


    Sam Hobbs
    SimpleSamples.Info


  • Hello Viorel. Let me think about it. There is nowhere to call EndEdit() because a PageUp or PageDown does not cause a FormKeyDown or DataGridVeiw_KeyDown event. 
  • Hello Xingyu. I have not solved the problem. This is a program rewritten form a Delphi program. This is not a problem with a Delphi form. I cannot always press PageUp and PageDown to change TabSheet in Delphi programs. Users do not want to user the mouse too often. They want to use mostly the keyboard. Imagine when a user types a few TextBoxes and then moves his hand to the mouse and clicks another tabsheet. Again he type some TextBoxes. Textbox -> textbox -> textbox -> mouse -> textbox -> mouse -> textbox.... Users get mad.
  • Hello. I also implement a F8 jumping. Users press the F8 Key and the focus is transferred to different controls, including DataGridViews and TextBoxes, in a certain order. Users can press the F8 key and let the focus to a textbox. Then he can press PageUp or PageDown to change TabSheets. I think I just leave it for now.
  • Hi AlanChong,

    Thanks for your feedback.

    In order to analyze your problem and make a test, I need more information.

    >>Users want to hit PageUp and PageDown keys to change tab indexes no matter where the focus is. This is implemented in Form_KeyDown() with Keypreview=true. 

    Could you provide some related code here? It will help us to reproduce your problem.

    We are waiting for your update.

    Best Regards,

    Xingyu Zhao


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Модератор
  • Users want to hit PageUp and PageDown keys to change tab indexes no matter where the focus is.

    Do Ctrl+Tab and Ctrl+Shift+Tab work? If so then there is no need to have anything in your code.


    Sam Hobbs
    SimpleSamples.Info