Raising ColumnResize event in MSFlexGrid (VB 5.0) | CodeGuru

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. […]

Written By
CodeGuru Staff
CodeGuru Staff
Oct 13, 2003
2 minute read
CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More

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

CodeGuru Logo

CodeGuru covers topics related to Microsoft-related software development, mobile development, database management, and web application programming. In addition to tutorials and how-tos that teach programmers how to code in Microsoft-related languages and frameworks like C# and .Net, we also publish articles on software development tools, the latest in developer news, and advice for project managers. Cloud services such as Microsoft Azure and database options including SQL Server and MSSQL are also frequently covered.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.