473,395 Members | 2,783 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Keypress navigation of an array of controls

I have a 2-d array of my own controls (derived from UserControl) on a panel in a Windows form. I would like the arrow keys to navigate in 2-dimensions, but the arrow keys get intercepted before I get to see them so I just get the default navigation (which goes through all of my controls as if they were in a single line. Can anyone tell me how to intercept the keystrokes and do my own handling? At the moment I'm overriding OnKeyPress on my own controls to try to catch the keypress, but the keypresses are not getting through. I've also tried adding
Expand|Select|Wrap|Line Numbers
  1. protected override bool IsInputChar(char charCode)
  2. {
  3.     if (charCode == (char)Keys.Up || charCode == (char)Keys.Down || charCode == (char)Keys.Left || charCode == (char)Keys.Right) return true;
  4.     else return base.IsInputChar(charCode);
  5. }
  6.  
both to my controls and to the form, but it makes no difference (that code isn't seeing the keypresses either). I can only think it's being caught at the level of the panel that my controls are on, but I can't see a direct way of adding an IsInputChar handler to it. Do I have to subclass the panel, is there another way of doing it, or am I missing the point altogether?
Feb 24 '11 #1
4 2291
GaryTexmo
1,501 Expert 1GB
Can you give us an idea of what your form might look like, and are the key presses intended to change the focus to the next control?

If the input is getting caught on the panel the controls are on, can you maybe handle it there?

Another way might be to try using a global keyboard hook. That will let you intercept the input at a lower level, so perhaps you can make use of that. Here's a code project link: http://www.codeproject.com/KB/cs/globalhook.aspx
Feb 24 '11 #2
It's a Japanese logic puzzle type of thing, so the game area is a rectangular array of cells in which various things happen. The game area is a panel, and the main form has other panels for status, buttons and so on. So I have an array of cells like:
1 2 3
4 5 6
7 8 9
(usually a lot bigger, and not numbered like that)
If I'm in cell 1, I want the down arrow to move me to cell 4, but at present it takes me to cell 2.

I'll have a look at the global keyboard hook, thanks.
Feb 24 '11 #3
GaryTexmo
1,501 Expert 1GB
I don't know the details of the user control you've got set up, but you might also try overriding the base class's KeyPressed event and not processing the base? I don't know if that would help... global keyboard hooks seem excessive for this application.

What's it doing right now with what have? Is it working and doing something else, or just not working at all?
Feb 24 '11 #4
What it was doing was the default navigation. For example, right or down navigated to the next control to the right, wrapping to the first control on the next row.

I've cracked it now. I had to handle my control's OnPreviewKeyDown event to set IsInputKey if it's a button I want, and handle my controls OnKeyDown (not OnKeyPress) event to do the actual handling, remembering to set the Handled flag.

Thanks.
Feb 24 '11 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: John MacIntyre | last post by:
Hi, I have a page with a series of child pages loaded into an iframe. When I move from page to page, I store an object containing the child's control data in a variable on the main page, then...
10
by: Tim Frawley | last post by:
I am attempting to detect a Shift+Tab in the KeyPress event for back navigation on a control that doesn't support this method. Does anyone have any ideas how to compare e.KeyChar to a ShiftTab? ...
3
by: MadMikey | last post by:
I've got some controls that are not available at design-time. How do I code events, such as Keypress, for these controls? Thanks in advance. Mikey
4
by: Broadbent | last post by:
I have just started using VB7 so I am new to all this. In VB6 I used to array controls, but I notice that there is no Index property in the VB7 controls. What's the answer please?
4
by: Tom | last post by:
I have a VB.NET user control that I wrote - this control has three or four other controls on it (textbox, combobox, datetime picker, etc). Now, whenever the control is on a form and the user enters...
15
by: Adam J. Schaff | last post by:
I have noticed that if a user closes a form via pressing return (either while the OK button has focus or if AcceptButton is set to OK for the form) then the "ENTER" keypress event fires ON THE...
4
by: polocar | last post by:
Hi, I would like to find a way in C# so that, when the user presses the "Esc" key, the form closes; the problem is that I have a lot of controls in the form. At the beginning I have tried with...
5
by: Nathan Sokalski | last post by:
My Web.config file contains the following section to register some of my UserControls: <pages> <controls> <add tagPrefix="NATE" tagName="Banner" src="~/Banner.ascx"/> <add tagPrefix="NATE"...
1
by: jungle38 | last post by:
Hi Guys. This is my first question regarding programming in a long time. See below a program that creates a form with a button array. The question is simple. After the AddHandler MessageBox.Show...
0
by: MAJIK123 | last post by:
I'm Now in VB 2005 and I need help to put in 200 label box without binding a row of and datagrid in VB6 datagrid.row = 1 for x = 0 to 199 datagrid.col = x label(x).caption =...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.