Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

MsFlexGrid Navigating with the arrow keys

Posted on 1998-11-30
8
Medium Priority
?
2,255 Views
Last Modified: 2013-12-25
I cannot trap te arrow keys in the keypress, keydown or the keyup events of the msflexgrid. I need to run some code every time the up or the down arrow key is pressed.
Any help will be greatly appreciated.
Asha.
0
Comment
Question by:Ashas
8 Comments
 
LVL 3

Expert Comment

by:traygreen
ID: 1488579
You can use the EnterCell and LeaveCell for the grid control
0
 
LVL 3

Expert Comment

by:myqlG
ID: 1488580
I have used the keydown,keypress events in the MSFlexGrid AND
the Axflex grid.  I have been unable to trap the arrow keys.
I am thinking the control itself intercepts the arrow keys and
uses them for navigation.  I have even used the keypreview=true
on the form and tested for it there.  No luck.
0
 
LVL 3

Expert Comment

by:traygreen
ID: 1488581
The KeyPreview poperty on the form does not seem to work nor the keypress/keydown event for the grid.  So you could track the row on entercell and if changed on the subsequent enter cell, then run your code.

The other option is to switch to the videosoft vsFlexArray version 3.  It is the same control but with added functionality.  It will trigger the keydown event on the arrow keys and then you can trigger your code based on the keycode equalling 40 (down arrow).
Try www.videosoft.com
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 3

Expert Comment

by:traygreen
ID: 1488582
The KeyPreview poperty on the form does not seem to work nor the keypress/keydown event for the grid.  So you could track the row on entercell and if changed on the subsequent enter cell, then run your code.

The other option is to switch to the videosoft vsFlexArray version 3.  It is the same control but with added functionality.  It will trigger the keydown event on the arrow keys and then you can trigger your code based on the keycode equalling 40 (down arrow).
Try www.videosoft.com
0
 

Author Comment

by:Ashas
ID: 1488583
The enter cell - leave cell event make the application very slow, especially if you have large amount of data to be filled in the grid. Please suggest another solution.
Asha

0
 
LVL 3

Expert Comment

by:traygreen
ID: 1488584
Any chance of switching to videosoft?
To the best of my knoweldge you can not trap the key events for the arrow/tab keys on the msflexgrid.  
It is possible w/ the newest videosoft grid.  You can either trap on the keydown for the grid or set the keypreview on the form to true and trap the keystrokes there.
If you go the the videosoft site you should be able to download a trail version.  Then you could cost justify easily when you see the improvements.  There should not be any major changes to your existing code as the grids are identical with the exception of the added functionality and the exposure of additional properties.
0
 
LVL 13

Accepted Solution

by:
Mirkwood earned 100 total points
ID: 1488585
Here is the answer
Subclass the msflexgrid control and trap WM_KEYDOWN

OWTO: Build a Windows Message Handler with AddressOf in VB5

Last reviewed: October 10, 1997
Article ID: Q170570 The information in this article applies to:

Microsoft Visual Basic Professional and Enterprise Editions for Windows, version 5.0 Microsoft Visual Studio 97







SUMMARY

Prior to the release of Visual Basic 5.0, many developers used third-party Windows message-handling tools or developed their own with tools such as Microsoft Visual C++. With the addition of the AddressOf function to Visual Basic 5.0, developers can now create their own Windows message-handling routines within their Visual Basic applications.

For example, when a user right-clicks on a textbox in Windows 95 or Windows NT 4.0, the operating system automatically displays a default context menu. This default behavior occurs before the Visual Basic application fires the MouseUp event. Without the use of a Windows message handler, there is no way to replace the default context menu with a custom built context menu.





MORE INFORMATION

WARNING: Using AddressOf may cause unpredictable results if you don't completely understand the concept of function callbacks. You must understand how the Basic portion of the callback works, and also the code of the DLL into which you are passing your function address. Debugging such interactions is difficult because the program runs in the same process as the development environment. In some cases, systematic debugging may not be possible. See details in the REFERENCES section of this article for more information.

The following sample shows how to build a Windows message handler to trap and discard the right-click message to replace the default context menu with a custom built one:



1.Create a new Standard EXE project. Form1 is created by default.

2.Use the Menu Editor to add two menu items to the form:





    - For the first item, set its Caption property to "My Popup," its Name
      property to "mnuPopup," and its Visible property to "False."







    - For the second item, set its Caption property to "My Context," its
      Name property to "mnuPopupContext," and use the arrow keys to indent
      the item to be a submenu of the first item.





3.Add two CommandButtons and a textbox to the form:





    - For the first CommandButton, set its Name property to "cmdHook" and
      its Caption property to "&Hook."







    - For the second CommandButton, set its Name property to "cmdUnHook"
      and its Caption property to "&UnHook."







    - For the textbox, set its Name property to "txtHook" and its Text
      property to "Right Click On Me!"





4.Add the following code to the form:





      Private Sub cmdHook_Click()

          Hook
      End Sub

      Private Sub cmdUnHook_Click()
          UnHook
      End Sub

      Private Sub Form_Load()
          gHW = txtHook.hWnd
      End Sub

      Private Sub txtHook_MouseUp(Button As Integer, Shift As Integer, _
                                  X As Single, Y As Single)
          If Button = vbRightButton Then
              PopupMenu mnuPopup
          End If
      End Sub






5.Add a Module to the project.

6.Add the following code to the new Module:





    Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" _

           (ByVal lpPrevWndFunc As Long, _
            ByVal hWnd As Long, _
            ByVal Msg As Long, _
            ByVal wParam As Long, _
            ByVal lParam As Long) As Long

    Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" _
           (ByVal hWnd As Long, _
            ByVal nIndex As Long, _
            ByVal dwNewLong As Long) As Long

    Public Const GWL_WNDPROC = -4

    Public Const WM_RBUTTONUP = &H205

    Global lpPrevWndProc As Long
    Global gHW As Long

    Public Sub Hook()
        lpPrevWndProc = SetWindowLong(gHW, GWL_WNDPROC, _
                                     AddressOf WindowProc)
    End Sub

    Public Sub UnHook()
        Dim lngReturnValue As Long
        lngReturnValue = SetWindowLong(gHW, GWL_WNDPROC, lpPrevWndProc)
    End Sub

    Function WindowProc(ByVal hw As Long, _
                        ByVal uMsg As Long, _
                        ByVal wParam As Long, _
                        ByVal lParam As Long) As Long

        Select Case uMsg
            Case WM_RBUTTONUP
                Form1.PopupMenu Form1.mnuPopup
            Case Else
                WindowProc = CallWindowProc(lpPrevWndProc, hw, _
                                           uMsg, wParam, lParam)
        End Select
    End Function






7.Save the project and run it. Right-click on the textbox and notice that the default context menu appears before the custom menu. Click on the CommandButton marked "Hook" to enable the Windows message handler. Right-click on the textbox and note that the default context menu no longer appears. Be sure to click on the CommandButton marked "UnHook" before quitting the application. Always disable a custom Windows message handler before the application terminates. Clicking the End button while this sample is running with the Windows message handler still enabled will cause an IPF.







REFERENCES

This as an advanced topic for users experienced with using callbacks, typically C developers. If you are not familiar with the use of callbacks, then the following references may apply:

The Visual Basic 5.0 Programmers Guide to the Win32 API by Dan Appleman, ISBN 1-56276-446-2 published by Ziff-Davis

The Win32 SDK Online Help

(c) Microsoft Corporation 1997, All Rights Reserved. Contributions by David Sceppa, Microsoft Corporation
0
 
LVL 1

Expert Comment

by:Eklipse
ID: 1488586
This is an approach that you don't have to use dll calls.
Do you really need to get arrow keystrokes or simply you need something that 'emulates' the
keystrokes?

Let's imagine this situation (general):We're in a cell  sorrounded by other cells:
OOO
OXO
OOO
We're are in X, right? We define this position X like the origin (0,0) in a classic system coordinates
if you move the X respect origin,we have different values( # = last position):
OXO
O#O    (0,1)
OOO

OOO
X#O   (-1,0)
OOO

OOO
O#O  (-1,1) and so on.
OOX

Back in the grid we can do something like this using the keycodes of arrows keys:
the codes for the arrows keys are: left-37,right-39 ,up-38,down-40
          /\ 38

37<-  O   -> 39

          \/ 40
where the 'pivot' numbers are 38 (between 37 and 39) and 39 (between 38-40)

Using the EnterCell and LeaveCell ,it's easy to know where you moved to.
Move_x=Now_x-Last_x
Move_y=Now_y-Last_y
so you have the relative difference and you can guess where did you go,respect the origin.
Then if you use the Move_x and Move_y and  you add to the pivot numbers,you can obtain the keycodes.

Enough explanation,let's go to the examples:

Open a new project,and drop a textbox(text1)  and msflexgrid(msflexgrid1)
Put the following code:

Option Explicit
Private x%, y%

Private Sub MSFlexGrid1_EnterCell()
Dim vx%, vy%, KeyArrow%
vx = MSFlexGrid1.Col - x: vy = MSFlexGrid1.Row - y
KeyArrow = (38 + vx) * Abs(vx <> 0) + (39 + vy) * Abs(vy <> 0)
Select Case KeyArrow
     Case vbKeyLeft:     Text1.Text = "left"
     Case vbKeyUp:      Text1.Text = "up"
     Case vbKeyRight: Text1.Text = "right"
     Case vbKeyDown:   Text1.Text = "down"
     Case Else:     Text1.Text = "other"
End Select
End Sub

Private Sub MSFlexGrid1_LeaveCell()
x = MSFlexGrid1.Col: y = MSFlexGrid1.Row
End Sub


Yep ,it has a problem:if you're in the bounds of the grid,by example, you're on the right, the right arrow won't respond.
Pro:extremely shorter than previous dll-only way-solution

0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone 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

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

877 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