Raising ColumnResize event in MSFlexGrid (VB 5.0)

Raising ColumnResize event in MSFlexGrid (VB 5.0)

Knowledge of API programming is required.

While working with MSFlexGrid control the only possibility to enter data is to place another control over it. When user clicks on a cell of the grid, you should move the control just over the selected cell and give it a focus. But the main problem with such 'Flying Dutchman' is the following: when grid is resizing no event occurs, and your control may lose fit. So the task is: How do I catch or emulate the ColumnResize event?

The decision provided is based on the event sequence. In a few words: when user simply clicks over the grid certain events come in the certain order which differs from the order of event occurring when user changes the column width.

Download the project end compile it.

Look at the code placed within the form module. Here we are trying to replace the default event handler routine with our own function GridMessage which (with all necessary API declarations) is situated in the module. We are using the AddressOf operator to provide a function pointer, which is required by SetWindowLong API procedure.

Look at the function GridMessage. As you can see, the routine traps only 3 events, which are described by constants WM_LBUTTONDOWN, WM_LBUTTONUP and WM_ERASEBKGND. (All other windows messages passed through untouched right into the arms of the default message handler.) The first two are mouse events when you are resizing column you first press the mouse button and then release it. But to distinguish the resizing from simple mouse click we need to hire the third event, WM_ERASEBKGND. In general, the WM_ERASEBKGND message is sent when the window background must be erased (for example, when a window is resized). In conjunction with two previous events this allows us to discern the cases when user resizes column's width or row's height. This is explained by the following table (note that here we concentrate our attention only upon these three events, though many other kinds of events are also being sent by the application.)

What happens

User makes a mouse click

User resizes a column

Sequence of the windows events

WM_LBUTTONDOWN

WM_LBUTTONDOWN

WM_ERASEBKGND

WM_LBUTTONUP

WM_LBUTTONUP

WM_ERASEBKGND


Run the project and make sure that resize events are processed by GridMessage function. It prints a line in the debug window each time the resize occurs. Now you can write your own procedure and place its call instead of Debug.Print. The Demo project demonstrates how this feature is used to keep all grid columns always of equal size.


Side effects:

When running in debug mode, never finish the application with the End button on VB environment panel. This will cause the termination of VB.


Download Zipped Project Files (3k)

Screen Shot



Comments

  • Improved column resize event

    Posted by Sam on 12/19/2013 11:36am

    I found that the routine in the zip file wasn't reliable enough. The following changes worked better for me. I'm sorry that the code is not directly applicable to the code in the zip, but better than nothing. Private Declare Function GetTickCount Lib "kernel32" () As Long Private currentMouseColumn As Integer Private lastCalled As Long Case WM_LBUTTONDOWN setMouseCol hWnd 'sub that sets currentMouseColumn to the current mouse column Case WM_LBUTTONUP lastCalled = GetTickCount() Case WM_ERASEBKGND ' WM_ERASEBKGND happens right after WM_LBUTTONUP only on column resize If Abs(GetTickCount() - lastCalled)

    Reply
  • AUTO rowheight

    Posted by spike8888 on 08/22/2005 08:33am

    How to change rowheight of MSHFLEXGRID automatically depend on amount of text placed in flex cell?

    Reply
  • C Language

    Posted by sivapandim2000 on 07/09/2005 03:06pm

    how to code a program to delete a file

    • C Language

      Posted by sivapandim2000 on 07/09/2005 03:08pm

      sefwefwef

      Reply
    Reply
  • Place the Control(Option button) in MSFlexgrid

    Posted by Legacy on 03/29/2002 12:00am

    Originally posted by: P.L.N.Murthy

    How to place the control into MSFlexgrid. Presently I am placing on top of each cell in a particular column. I am writing the below code for that. 
    
    

    Opt(0).Visible = False

    For i = 1 To MSFlexGrid1.Rows - 1

    MSFlexGrid1.Row = i
    MSFlexGrid1.Col = 6

    Load Opt(i)
    Opt(i).Height = 190
    Opt(i).Width = 190
    Opt(i).Left = MSFlexGrid1.CellLeft + MSFlexGrid1.Left
    Opt(i).Top = MSFlexGrid1.CellTop + MSFlexGrid1.Top
    Opt(i).ZOrder (0)
    Opt(i).Visible = True

    Next i

    The problem of above code is , those controls are not stick to cells. If I am moving colums in flexgrid. The controls are not moving along with columns. Can you give me suggestion to do this. If you provide code for this, that will help me alot.

    Reply
  • I need an exsample work in usercontrol

    Posted by Legacy on 10/07/2001 12:00am

    Originally posted by: amer

    I need an exsample work in usercontrol and reasing event in resize mode

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • On-demand Event Event Date: September 10, 2014 Modern mobile applications connect systems-of-engagement (mobile apps) with systems-of-record (traditional IT) to deliver new and innovative business value. But the lifecycle for development of mobile apps is also new and different. Emerging trends in mobile development call for faster delivery of incremental features, coupled with feedback from the users of the app "in the wild." This loop of continuous delivery and continuous feedback is how the best mobile …

  • As mobile devices have pushed their way into the enterprise, they have brought cloud apps along with them. This app explosion means account passwords are multiplying, which exposes corporate data and leads to help desk calls from frustrated users. This paper will discover how IT can improve user productivity, gain visibility and control over SaaS and mobile apps, and stop password sprawl. Download this white paper to learn: How you can leverage your existing AD to manage app access. Key capabilities to …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds