Checkbox & keydown [took some time but... RESOLVED!]
Is it possible to have a checkbox' keydown event fire when one of the arrow keys is pressed? Or at least is there any way around this?
Last edited by krtxmrtz; Sep 18th, 2006 at 10:53 AM.
Lottery is a tax on people who are bad at maths
If only mosquitoes sucked fat instead of blood...
To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)
I just thought I could use the validate event... Would that make any difference?
Lottery is a tax on people who are bad at maths
If only mosquitoes sucked fat instead of blood...
To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)
No I can't, there's no way to know the code of the pressed key. I've tried to enable keypreview for the form and trap the arrow keys in the form's keydown event but it doesn't work.
So how what can I do so that the arrow keys trigger some event when a checkbox has the focus?
Lottery is a tax on people who are bad at maths
If only mosquitoes sucked fat instead of blood...
To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)
The most difficult part of developing a program is understanding the problem.
The second most difficult part is deciding how you're going to solve the problem.
Actually writing the program (translating your solution into some computer language) is the easiest part.
Please indent your code and use [HIGHLIGHT="VB"] [/HIGHLIGHT] tags around it to make it easier to read. Please Help Us To Save Ana
I am not sure what the problem you are having is, you were given code to test for key events and the corresponding codes for those keys. Is there something that we are overlooking??
All right, I'll try to explain what I want to do. It's related to my previous thread (this one)
Please, take a moment to read my first post there and then try out the code attached to this post. If you click on the grid or use the enter or arrow keys, you'll see that the text in the target cell is selected (this I've done by means of a textbox), but in row 8 a checkbox appears. Click on any cell of row 8 and then try to navigate from there by means of the keyboard. The ener key works ok, but not so the arow keys. I'm trying to figure out the logic of it all...
Lottery is a tax on people who are bad at maths
If only mosquitoes sucked fat instead of blood...
To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)
The first thing I did was to add a form-level Watch on ActiveControl.Name, set to break when the value changed. I then made the following change
VB Code:
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
kee = KeyCode
[HL="#FFFF80"] If KeyCode = vbKeyRight Then
chk1_KeyDown KeyCode, 0
End If[/HL]
End Sub
And I was happy when it worked. I then got rid of the watch and it no longer worked! I'm guessing that since the only thing that really happened was that the code was no longer pausing between the changing of the active control that a DoEvents (or two) will fix the problem, but I haven't been able to get it right yet.
I'm not sure what this code's supposed to do... The idea is when the checkbox has the focus (i.e. when we are in row 8) then if I hit any of the arrow keys I want:
1. The checkbox to become invisible
2. The cell's text to be updated to 0 or 1 according to the checkbox value
3. To select the text of the corresponding neighbour cell (therefore to update the textbox and make it visible)
Lottery is a tax on people who are bad at maths
If only mosquitoes sucked fat instead of blood...
To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)
In testing this I found another problem. If you use the right arrow to "tab" in the checkbox row and you go from column 8 to column 9, that triggers the grid_Scroll sub which calls the setuptextbox sub which shows the original value of the grid cell in row 8, column 9.
In testing this I found another problem. If you use the right arrow to "tab" in the checkbox row and you go from column 8 to column 9, that triggers the grid_Scroll sub which calls the setuptextbox sub which shows the original value of the grid cell in row 8, column 9.
Yes, I thought I would deal with the scroll event after the current problem had been fixed.
Lottery is a tax on people who are bad at maths
If only mosquitoes sucked fat instead of blood...
To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)
Lottery is a tax on people who are bad at maths
If only mosquitoes sucked fat instead of blood...
To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)
as the arrow keys ( and tab) are system level keys, they are not returned in the keydow, keyup or keypress events of a form or any control on the form, so i am guessing you would have to use a system wide hook to catch them
only other option i can see is to catch the event they cause, like the getfocus of whatever is next
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case. Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
as the arrow keys ( and tab) are system level keys, they are not returned in the keydow, keyup or keypress events of a form or any control on the form...
I agree about the tab key but the arrow keys do trigger the keydown/keyup events. You can test this: just place a few common contols on a form, set breakpoints in the respective keydown events and then hit the arrow keys.
However they just don't respond in the demo project I'm working on, maybe because the checkbox is sitting on a grid or maybe because the code has some flaws.
Lottery is a tax on people who are bad at maths
If only mosquitoes sucked fat instead of blood...
To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)
I think the attached does what you want. I had to hook the windows messages for the chekbox to do it so while the msghook dll that I used to do that claims that there aren't the "normal" problems with subclassing, always make sure when testing to end the app normally otherwise VB might crash.
I think the attached does what you want. I had to hook the windows messages for the chekbox to do it so while the msghook dll that I used to do that claims that there aren't the "normal" problems with subclassing, always make sure when testing to end the app normally otherwise VB might crash.
Thanks Marty, I have yet to study the code and the readme file about the dll. One thing that doesn't work though is the right and left arrow keys when I'm in the checkbox. Could it be some minor fine tuning that I'm not aware of?
Lottery is a tax on people who are bad at maths
If only mosquitoes sucked fat instead of blood...
To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)
...One thing that doesn't work though is the right and left arrow keys when I'm in the checkbox. Could it be some minor fine tuning that I'm not aware of?
I don't understand. If, for example, I go to a cell in the check box row that is currently zero, and check it and then use either the left or right arrow, the previously zero cell will show one. Isn't that what you want?
I don't understand. If, for example, I go to a cell in the check box row that is currently zero, and check it and then use either the left or right arrow, the previously zero cell will show one. Isn't that what you want?
OK but when I leave the cell with the checkbox, I want to go up/down if I hit the up/down arrow keys, but the right/left arrow keys should take me to the right/left neighbouring cells, which don't: left & right arrow keys work exactly as the up & down arrows.
Lottery is a tax on people who are bad at maths
If only mosquitoes sucked fat instead of blood...
To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)
Don't click on row 8: click for example on '190' in row 7, now arrow down to take you to the 0 (non-checked checkbox) of row 8 and now type left or right arrow. It goes down or up rather than left or right.
Lottery is a tax on people who are bad at maths
If only mosquitoes sucked fat instead of blood...
To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)
I don't understand why the other doesn't work but try this.
VB Code:
Private Sub IMsgHookEvents_BeforeMessage(ByVal obj As MsgHookSvr.MsgHook, uMsg As Long, wParam As Long, lParam As Long, ReturnValue As Long, Cancel As Boolean)
' in this particular case, it isn't really necessary to test for
I don't understand why the other doesn't work but try this.
No, this one isn't working either. It behaves somewhat erratically. The right and left arrow keys now work correctly for the checkbox row, they move you to the next cell to the right or left, but the checkbox should now be visible on the current cell and isn't.
Also, if you navigate by means of the up or down arrow keys or the retunr key, sometimes you see strange things like moving one column to the left after typing return, moving one cell up after typing arrow down, etc. Also the background colour is not always updated.
Well, I have printed the dll guide and your code and will try to study it and see what can do about this.
Lottery is a tax on people who are bad at maths
If only mosquitoes sucked fat instead of blood...
To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)
Try this - the magic row is #4 and space will check the checkbox
It works, but the original project included a textbox to provide the selstart and sellength properties not available for the grid's cells. This makes it more difficult to fine tune it.
At the moment I have so much feedback from you guys that I've got to put on the thinking cap and sit for a while in front of the printed code.
Lottery is a tax on people who are bad at maths
If only mosquitoes sucked fat instead of blood...
To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)
Hey, that's great! Just one more detail's left to deal with, At the risk of messing around somewhat, I'd like the checkbox (and/or the cell it's on) to display a yellow background as well. Is that possible?
Lottery is a tax on people who are bad at maths
If only mosquitoes sucked fat instead of blood...
To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)
' In MSFlexGrid1_LeaveCell (under first If condition)
.CellBackColor = vbWhite
should do it
Yes it does...
I've just noticed what still's not working: when you hit return on the checkbox.
Lottery is a tax on people who are bad at maths
If only mosquitoes sucked fat instead of blood...
To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)
Private Sub MSFlexGrid1_KeyPress(KeyAscii As Integer)
If MSFlexGrid1.Row = CBRow Then
If KeyAscii = 32 Then
Check1.Value = Abs(Not CBool(Check1.Value))
ElseIf KeyAscii = 13 Then
MSFlexGrid1.Row = MSFlexGrid1.Row + 1
KeyAscii = 0
End If
End If
End Sub
plus you'll want to add in this one, which i forgot:
VB Code:
Private Sub Text1_KeyPress(KeyAscii As Integer)
If KeyAscii = 13 Then KeyAscii = 0
End Sub
Very good job, bushmobile, that is it! This evening's beer is on me... can you digitize that???
Lottery is a tax on people who are bad at maths
If only mosquitoes sucked fat instead of blood...
To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)
Depending on the total grid width (play around with the number of extra pixels) some problems appear with scrolling. It appears that you need 2 extra pixels so that the rightmost visible column is considered totally visible, but then another pixel may be necessary to prevent unwanted scrolling effects (try and see).
All in all I have the feeling that I'm on shaky ground and that the whole thing is a mess.
Lottery is a tax on people who are bad at maths
If only mosquitoes sucked fat instead of blood...
To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)