Results 1 to 37 of 37

Thread: Checkbox & keydown [took some time but... RESOLVED!]

Hybrid View

  1. #1

    Thread Starter
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573

    Smile 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)

  2. #2

    Thread Starter
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573

    Re: Checkbox & keydown

    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)

  3. #3

    Thread Starter
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573

    Re: Checkbox & keydown

    Quote Originally Posted by krtxmrtz
    I just thought I could use the validate event.
    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)

  4. #4
    Fanatic Member kregg's Avatar
    Join Date
    Feb 2006
    Location
    UK
    Posts
    524

    Re: Checkbox & keydown [as yet unserolved]

    I believe you'll need some API code for that. I'll post back when I find something

    EDIT: http://www.vbforums.com/showthread.p...t=key+trap+api

    Try:
    VB Code:
    1. Private Sub CheckBox1_KeyDown(KeyCode As Integer, Shift As Integer)
    2.     If KeyCode = vbKeyUP or vbKeyDown or vbKeyLeft or vbKeyRight Then
    3.         'call your save function here
    4.     End If
    5. End Sub

    Just edited Shuja Ali's post for what I've gathered from your post. It might not exactly work, but it should be something similar to that i hope...
    Last edited by kregg; Sep 14th, 2006 at 08:22 AM.

  5. #5
    PowerPoster Pasvorto's Avatar
    Join Date
    Oct 2002
    Location
    Minnesota, USA
    Posts
    2,951

    Re: Checkbox & keydown [as yet unserolved]

    Could you do a msgbox and find out the ascii value for those keys and then use the keypress event to test for them?

  6. #6
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,427

    Re: Checkbox & keydown [as yet unserolved]

    VB Code:
    1. Private Sub Form_Load()
    2.  
    3.     Me.KeyPreview = True
    4.    
    5. End Sub
    6. Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
    7.    
    8.     Select Case KeyCode
    9.         Case vbKeyLeft
    10.             MsgBox "left arrow"
    11.         Case vbKeyRight
    12.             MsgBox "right arror"
    13.     End Select
    14.  
    15. End Sub

  7. #7
    PowerPoster
    Join Date
    Feb 2006
    Location
    East of NYC, USA
    Posts
    5,691

    Re: Checkbox & keydown [as yet unserolved]

    vbKeyLeft = 37
    vbKeyUp = 38
    vbKeyRight = 39
    vbKeyDown = 40
    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

  8. #8
    Fanatic Member
    Join Date
    Aug 2006
    Location
    In my head
    Posts
    913

    Re: Checkbox & keydown [as yet unserolved]

    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??

    Thanks,

    D

  9. #9

    Thread Starter
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573

    Re: Checkbox & keydown [as yet unserolved]

    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...
    Attached Files Attached Files
    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)

  10. #10
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,427

    Re: Checkbox & keydown [as yet unserolved]

    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:
    1. Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
    2.  
    3.     kee = KeyCode
    4.   [HL="#FFFF80"]  If KeyCode = vbKeyRight Then
    5.          chk1_KeyDown KeyCode, 0
    6.     End If[/HL]
    7. 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.

  11. #11
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,427

    Re: Checkbox & keydown [as yet unserolved]

    Okay forget about the previous code. Just do

    VB Code:
    1. Private Sub setupcheckbox()
    2.     With grid
    3.         grid.CellBackColor = vbYellow
    4.         chk1.Visible = True
    5.         chk1.Move .Left + .CellLeft + 0.5 * (.CellWidth - chk1.Width), .Top + .CellTop
    6.         chk1.Value = grid.TextMatrix(grid.Row, grid.Col)
    7. '        chk1.SetFocus [HL="#FFFF80"]Commented[/HL]
    8.         chk1.ZOrder
    9.         oldcol = .Col
    10.         oldrow = .Row
    11.     End With
    12. End Sub

  12. #12

    Thread Starter
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573

    Re: Checkbox & keydown [as yet unserolved]

    Quote Originally Posted by MartinLiss
    Okay forget about the previous code. Just do

    VB Code:
    1. Private Sub setupcheckbox()
    2.     With grid
    3.         grid.CellBackColor = vbYellow
    4.         chk1.Visible = True
    5.         chk1.Move .Left + .CellLeft + 0.5 * (.CellWidth - chk1.Width), .Top + .CellTop
    6.         chk1.Value = grid.TextMatrix(grid.Row, grid.Col)
    7. '        chk1.SetFocus [HL="#FFFF80"]Commented[/HL]
    8.         chk1.ZOrder
    9.         oldcol = .Col
    10.         oldrow = .Row
    11.     End With
    12. End Sub
    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)

  13. #13
    Frenzied Member trisuglow's Avatar
    Join Date
    Jan 2002
    Location
    Horsham, Sussex, UK
    Posts
    1,536

    Re: Checkbox & keydown [as yet unserolved]

    I understand the problem, but I'm not sure I know the solution.

    You could create a usercontrol that encapsulates a checkbox - I think you could make this work for you.

    Alternatively you can use the LostFocus event of the checkbox (but this doesn't let you know which key was pressed).
    This world is not my home. I'm just passing through.

  14. #14
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Checkbox & keydown [as yet unserolved]

    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

  15. #15

    Thread Starter
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573

    Re: Checkbox & keydown [as yet unserolved]

    Quote Originally Posted by westconn1
    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)

  16. #16
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,427

    Re: Checkbox & keydown [as yet unserolved]

    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.
    Attached Files Attached Files

  17. #17

    Thread Starter
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573

    Re: Checkbox & keydown [as yet unserolved]

    Quote Originally Posted by MartinLiss
    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)

  18. #18
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,427

    Re: Checkbox & keydown [as yet unserolved]

    Quote Originally Posted by krtxmrtz
    ...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?

  19. #19
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    Re: Checkbox & keydown [as yet unserolved]

    Try this - the magic row is #4 and space will check the checkbox
    VB Code:
    1. Private Sub Check1_Click()
    2.     MSFlexGrid1.SetFocus
    3. End Sub
    4.  
    5. Private Sub Form_Load()
    6.     Check1.Visible = False
    7. End Sub
    8.  
    9. Private Sub MSFlexGrid1_EnterCell()
    10.     With MSFlexGrid1
    11.         If .Row = 4 Then
    12.             Check1.Left = .Left + .ColPos(.Col) + (.ColWidth(.Col) - Check1.Width) \ 2
    13.             Check1.Top = .Top + .RowPos(.Row) + (.RowHeight(.Row) - Check1.Height) \ 2
    14.             Check1.Value = Val(.TextMatrix(.Row, .Col))
    15.             Check1.Visible = True
    16.         End If
    17.     End With
    18. End Sub
    19.  
    20. Private Sub MSFlexGrid1_KeyPress(KeyAscii As Integer)
    21.     If MSFlexGrid1.Row = 4 Then
    22.         If KeyAscii = 32 Then Check1.Value = Abs(Not CBool(Check1.Value))
    23.     End If
    24. End Sub
    25.  
    26. Private Sub MSFlexGrid1_LeaveCell()
    27.     With MSFlexGrid1
    28.         If .Row = 4 Then
    29.             .TextMatrix(.Row, .Col) = Check1.Value
    30.             Check1.Visible = False
    31.         End If
    32.     End With
    33. End Sub
    Edit: it also needs this Sub (attachment updated):
    VB Code:
    1. Private Sub MSFlexGrid1_Scroll()
    2.     With MSFlexGrid1
    3.         If Not (.RowIsVisible(.Row) And .ColIsVisible(.Col)) Then
    4.             Check1.Visible = False
    5.         Else[B]If .Row = 4 Then[/B]
    6.             Check1.Left = .Left + .ColPos(.Col) + (.ColWidth(.Col) - Check1.Width) \ 2
    7.             Check1.Top = .Top + .RowPos(.Row) + (.RowHeight(.Row) - Check1.Height) \ 2
    8.             Check1.Visible = True
    9.         End If
    10.     End With
    11. End Sub
    Edit2: another slight change to code (in bold above) - attachment not updated
    Attached Files Attached Files

  20. #20

    Thread Starter
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573

    Re: Checkbox & keydown [as yet unserolved]

    Quote Originally Posted by bushmobile
    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)

  21. #21
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    Re: Checkbox & keydown [as yet unserolved]

    Hey Presto! Textbox and Checkbox combined

    Attached Files Attached Files

  22. #22

    Thread Starter
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573

    Re: Checkbox & keydown [as yet unserolved]

    Quote Originally Posted by bushmobile
    Hey Presto! Textbox and Checkbox combined
    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)

  23. #23
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    Re: Checkbox & keydown [as yet unserolved]

    umm, adding
    VB Code:
    1. ' In SetUpCheckBox()
    2.         .CellBackColor = vbYellow
    3.  
    4. ' In MSFlexGrid1_LeaveCell (under first If condition)
    5.             .CellBackColor = vbWhite
    should do it

  24. #24

    Thread Starter
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573

    Re: Checkbox & keydown [as yet unserolved]

    Quote Originally Posted by bushmobile
    umm, adding
    VB Code:
    1. ' In SetUpCheckBox()
    2.         .CellBackColor = vbYellow
    3.  
    4. ' In MSFlexGrid1_LeaveCell (under first If condition)
    5.             .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)

  25. #25
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    Re: Checkbox & keydown [as yet unserolved]

    ok, change this sub
    VB Code:
    1. Private Sub MSFlexGrid1_KeyPress(KeyAscii As Integer)
    2.     If MSFlexGrid1.Row = CBRow Then
    3.         If KeyAscii = 32 Then
    4.             Check1.Value = Abs(Not CBool(Check1.Value))
    5.         ElseIf KeyAscii = 13 Then
    6.             MSFlexGrid1.Row = MSFlexGrid1.Row + 1
    7.             KeyAscii = 0
    8.         End If
    9.     End If
    10. End Sub
    plus you'll want to add in this one, which i forgot:
    VB Code:
    1. Private Sub Text1_KeyPress(KeyAscii As Integer)
    2.     If KeyAscii = 13 Then KeyAscii = 0
    3. End Sub

  26. #26

    Thread Starter
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573

    Re: Checkbox & keydown [as yet unserolved]

    Quote Originally Posted by bushmobile
    ok, change this sub
    VB Code:
    1. Private Sub MSFlexGrid1_KeyPress(KeyAscii As Integer)
    2.     If MSFlexGrid1.Row = CBRow Then
    3.         If KeyAscii = 32 Then
    4.             Check1.Value = Abs(Not CBool(Check1.Value))
    5.         ElseIf KeyAscii = 13 Then
    6.             MSFlexGrid1.Row = MSFlexGrid1.Row + 1
    7.             KeyAscii = 0
    8.         End If
    9.     End If
    10. End Sub
    plus you'll want to add in this one, which i forgot:
    VB Code:
    1. Private Sub Text1_KeyPress(KeyAscii As Integer)
    2.     If KeyAscii = 13 Then KeyAscii = 0
    3. End Sub
    Very good job, bushmobile, that is it! This evening's beer is on me... can you digitize that???
    Attached Images Attached Images  
    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)

  27. #27

  28. #28

    Thread Starter
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573

    Re: Checkbox & keydown [took some time but... RESOLVED!]

    Here's my current (final?) version. I've made a few minor changes (value of row 4) and fixed scrolling problems. I have set the grid width as:
    VB Code:
    1. tx = Screen.TwipsPerPixelX
    2. With Grid
    3.     'Sum of the widths of all the columns (all have the same
    4.     ' width) plus the widths of the left and 'right borders
    5.     '(times 2, found by trial and error)
    6.     .Width = .ColWidth(0) * nVisibleCols + 4 * .BorderStyle * tx
    7.     'And I have added 3 extra pixels (see below)
    8.     .Width = .Width + 3 * tx
    9. End With
    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.
    Attached Files Attached Files
    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)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width