Desktop-as-a-Service Designed for Any Cloud ? Nutanix Frame

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







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


  • Improved column resize event

    Posted by Sam on 12/19/2013 07:36pm

    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)

  • AUTO rowheight

    Posted by spike8888 on 08/22/2005 03:33pm

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

  • C Language

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

    how to code a program to delete a file

    • C Language

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


  • Place the Control(Option button) in MSFlexgrid

    Posted by Legacy on 03/29/2002 08: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.

  • I need an exsample work in usercontrol

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

    Originally posted by: amer

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

  • You must have javascript enabled in order to post comments.

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

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date