I am making a small application that shows visually a mapping of equal sized rectangular chips on a wafer. I am doing this by drawing rectangles directly on an area of my main form. I now need to allow a user to give an apparent focus to a chip on this mapping, either with by clicking on it with the mouse or by navigating around the mapping with the arrow keys.
I have solved the mouse click focus, where I examine the x and y coordinates of the left mouse click event, decide if these coordinates coincide with the location of a chip, and then paint the selected chip with a different color and store the coordinates as the currently selected coordinates.
The problem I have now is navigating around the mapping with the arrow keys, where for example clicking on the "down arrow" will move the selected chip to the one below (if it exists). I can capture the KeyDown event and recognize whether the key pressed is an arrow key. The problem, however, is that the main form which I am drawing on, does not accept focus. I would like to use the arrow keys to navigate around the mapping IF the mapping image has the current focus and not any of the input field controls on the form. For example, the user should be able to click on the mapping, and this would remove the focus from all other controls on the form. but since the main form itself does not accept focus, I cannot do this.
How can I do this? I am relatively new to Visual C#.
Do I have to put some kind of focus-accepting control on my form, and draw my mapping on that instead of the main form? If so, what control would that be?
Thanks for any help!
stevePosted Jul 4, 2008, 9:32 AM
set the forms keypreview to true:
I already did that. That is not the issue here. The issue is giving focus to the main form.
stevePosted Jul 4, 2008, 9:31 AM
I did find a workaround: I placed a button control discretely in the corner and made it's style flat, its border and background the same as the form color. In other words, this is an invisible button.
I then examine the x and y coordinates of any mouse click on the form. If these coordinates are within the mapping image, I give this invisible button the focus, thus removing the focus from all other controls on the page.
This solution may not be the best or most correct, but it gives the user the impression that he/she has given the focus to the mapping image, since clicking on a chip in the mapping will also cause that chip to be given the focus color.
However, if anyone can show me the proper way to do this, please do!
Scott LyslePosted Jul 4, 2008, 9:30 AM