Solved

Winform Button focus on Up/Down Arrow key

Posted on 2007-11-14
5
1,416 Views
Last Modified: 2013-12-17
In Winform  I have 2 buttons . When I use the UP/Down arrow key it chnages the focus from one button to other.I want to disable this button focus chnage when Up/Down arrow key is hit on the keyboard.

Instead I wanted to us for Datagrid. So I worte the following code for Scrolling the Grid.
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
                        
      switch (keyData)
      {
            case Keys.Down:
                 SelectNextGridRow();
                 break;
            case Keys.Up:
                   SelectNextGridRow();
                 break;
      }
      return base.ProcessCmdKey(ref msg, keyData);
}

The focus on buttons also keeps chnaging when I hit Up/Down Key. How do I disable this?

0
Comment
Question by:AdibRahman77
  • 2
  • 2
5 Comments
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 300 total points
ID: 20281883
This part:

    return base.ProcessCmdKey(ref msg, keyData);

causes the default action for the keys to be processed.

If you don't want something to happen for certain keys then you need to NOT call the above line of code for them...

0
 
LVL 10

Assisted Solution

by:Nash2334
Nash2334 earned 150 total points
ID: 20281939
Idle_Mind is correct, just move the return to the default action and you should be OK.

protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
                       
      switch (keyData)
      {
            case Keys.Down:
                 SelectNextGridRow();
            case Keys.Up:
                   SelectNextGridRow();
            default:
                   return base.ProcessCmdKey(ref msg, keyData);
      }
}
0
 
LVL 10

Expert Comment

by:Nash2334
ID: 20281944
Forgot the return statement for the up down keys...

protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
                       
      switch (keyData)
      {
            case Keys.Down:
                 SelectNextGridRow();
                 return true;
            case Keys.Up:
                   SelectNextGridRow();
                   return true;
            default:
                   return base.ProcessCmdKey(ref msg, keyData);
      }
}
0
 
LVL 3

Assisted Solution

by:Aurora27
Aurora27 earned 50 total points
ID: 20283268
yep..returning a base calls memeber fn. always overrides the everthing that was done before :)

CT.
0
 
LVL 3

Expert Comment

by:Aurora27
ID: 20285668
Thanks.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question