Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
Solved

# Flex Greed

Posted on 2001-08-10
Medium Priority
536 Views
During loding  values, i want to resize a column of flex greed control. How can i do that nd what property i have to use.
0
Question by:sycale
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 4

LVL 3

Accepted Solution

andysalih earned 120 total points
ID: 6374728
this automaticaly resizes the flex grid

Public Sub AutosizeGridColumns(ByRef msFG As MSFlexGrid, ByVal MaxRowsToParse As Integer, ByVal MaxColWidth As Integer)
Dim I, J As Integer
Dim txtString As String
Dim intTempWidth, BiggestWidth As Integer
Dim intRows As Integer

With msFG

For I = 0 To .Cols - 1
' Loops through every column
.Col = I
' Set the active colunm
intRows = .Rows
' Set the number of rows
If intRows > MaxRowsToParse Then intRows = MaxRowsToParse
' If there are more rows of data, reset
' intRows to the MaxRowsToParse constant
'

intBiggestWidth = 0
' Reset some values to 0

For J = 0 To intRows - 1
' check up to MaxRowsToParse # of rows a
'     nd obtain
' the greatest width of the cell content
'     s

.Row = J

txtString = .Text
' The intPadding constant compensates fo
'     r text insets
' You can adjust this value above as des
'     ired.

If intTempWidth > intBiggestWidth Then intBiggestWidth = intTempWidth
' Reset intBiggestWidth to the intMaxCol
'     Width value if necessary
Next J
.ColWidth(I) = intBiggestWidth
Next I
' Now check to see if the columns aren't
'     as wide as the grid itself.
' If not, determine the difference and e
'     xpand each column proportionately
' to fill the grid
intTempWidth = 0

For I = 0 To .Cols - 1
intTempWidth = intTempWidth + .ColWidth(I)
' Add up the width of all the columns
Next I

If intTempWidth < msFG.Width Then
' Compate the width of the columns to th
'     e width of the grid control
' and if necessary expand the columns.
intTempWidth = Fix((msFG.Width - intTempWidth) / .Cols)
' Determine the amount od width expansio
'     n needed by each column

For I = 0 To .Cols - 1
.ColWidth(I) = .ColWidth(I) + intTempWidth
' add the necessary width to each column
'

Next I
End If
End With
End Sub

hope thats what you want

cheers
Andy
0

LVL 3

Expert Comment

ID: 6374730
just to explain what the code does above in more depth i forgot to include :-

Automatically resize the columns in any flex grid to give a nice, professional appearance. Public sub automatically resizes MS Flex Grid columns to match the width of the text, no matter the size of the grid or the number of columns. Reads first n number of rows of data, and adjusts column size to match the widest cell of text. Will even expand columns proportionately if they aren't wide enough to fill out the entire width of the grid. Configurable constraints allow you to designate 1) Any flex grid to resize 2) Maximum column width 3) the maximum number of rows in depth to look for the widest cell of text

cheers
Andy
0

LVL 3

Expert Comment

ID: 6374739
this is a little lengthy but see how you get on with it.

You must have MSflxgrd.ocx installed.

Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long
' Function to define the address of the
'     Message-Handling routine

Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
' Function to execute a function residin
'     g at a specific memory address

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
'Windows messages constants
Public Const WM_LBUTTONUP = &H202
Public Const WM_LBUTTONDOWN = &H201
Public Const WM_ERASEBKGND = &H14

You must have MSflxgrd.ocx installed.
Create a Form frmMain and place a MSFlexGrid control with name MSFlexGrid1 and a Module. The code is given below.
'
'Side Effects:Don't ever finish the appl
'     ication with the Stop button of VB Envir
'     onment. This will cause VBE to terminate
'     .
VERSION 5.00
Object = "{5E9E78A0-531B-11CF-91F6-C2863C385E30}#1.0#0"; "Msflxgrd.ocx"
Begin VB.Form frmMain
BorderStyle =3 'Fixed Dialog
Caption ="Resize the Grid !!!"
ClientHeight=4110
ClientLeft =4650
ClientTop=3750
ClientWidth =6735
MaxButton=0'False
MinButton=0'False
ScaleHeight =4110
ScaleWidth =6735
Begin MSFlexGridLib.MSFlexGrid MSFlexGrid1
Height =3015
Left=120
TabIndex=0
Top =960
Width=6495
_ExtentX=11456
_ExtentY=5318
_Version=65541
Rows=4
Cols=4
AllowUserResizing=1
End
Begin VB.Label Label2
Caption ="Try to resize the columns of MSFlexGrid. All the columns will be resized proportionally."
BeginProperty Font
Name="MS Sans Serif"
Size=9.75
Charset =204
Weight =400
Underline=0'False
Italic =0'False
Strikethrough=0'False
EndProperty

ForeColor=&H8000000D&
Height =615
Left=1320
TabIndex=1
Top =120
Width=3975
End
End
Attribute VB_Name = "frmMain"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option Explicit
' This constant is used to refer to the
'     Message Handling function in a given win

'     dow
Private Const GWL_WNDPROC = (-4)

'Save the address of the existing Messag
'     e Handler
g_lngDefaultHandler = GetWindowLong(Me.MSFlexGrid1.hwnd, GWL_WNDPROC)
'Define new message handler routine
End Sub

'Return the old handler back
Call SetWindowLong(Me.MSFlexGrid1.hwnd, GWL_WNDPROC, g_lngDefaultHandler)
End Sub

Public Sub ResizeGridProportional()
Dim SumWidthAs Long
Dim i As Integer

With MSFlexGrid1

For i = 1 To .Cols
SumWidth = SumWidth + .ColWidth(i - 1)
Next i

For i = 1 To .Cols
.ColWidth(i - 1) = SumWidth / .Cols
Next i
End With
End Sub

/* ******************** MODULE ***********************************/
Attribute VB_Name = "mHandlers"
'
Option Explicit
Public g_lngDefaultHandler As Long ' Original handler of the grid events
Private m_bLMousePressed As Boolean 'true if the left button is pressed
Private m_bLMouseClicked As Boolean 'true just after the click (i.e. just after the left button is released)
'API declarations ======================
'     ======================================
' Function to retrieve the address of th
'     e current Message-Handling routine

Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long
' Function to define the address of the
'     Message-Handling routine

Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
' Function to execute a function residin
'     g at a specific memory address

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
'Windows messages constants
Public Const WM_LBUTTONUP = &H202
Public Const WM_LBUTTONDOWN = &H201
Public Const WM_ERASEBKGND = &H14
'=======================================
'     =======================================
'this is our event handler

Public Function GridMessage(ByVal hwnd As Long, ByVal Msg As Long, ByVal wp As Long, ByVal lp As Long) As Long

If m_bLMousePressed And Msg = WM_LBUTTONUP Then
'button have been just released
m_bLMousePressed = False
m_bLMouseClicked = True
End If

If Not (m_bLMousePressed) And Msg = WM_LBUTTONDOWN Then
'button have been just pressed
m_bLMousePressed = True
m_bLMouseClicked = False
End If

If m_bLMouseClicked And (Msg = WM_ERASEBKGND) Then
'Only when resize happens this event may
'     occur after releasing the button !
'When user is making a simple click on g
'     rid,
'the WM_ERASEBKGND event occurs before W
'     M_LBUTTONUP,
'and therefore will not be handled there
'
frmMain.ResizeGridProportional
m_bLMouseClicked = False
End If
'call the default message handler
GridMessage = CallWindowProc(g_lngDefaultHandler, hwnd, Msg, wp, lp)
End Function

cheers
Andy
0

Author Comment

ID: 6374841
andysalih , you can do it with very short way. But thanks for trying to help me, i used couple your properties.
0

LVL 3

Expert Comment

ID: 6374895
im glad to have been of some help.

cheers
Andy

:o)

0

## Featured Post

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…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
###### Suggested Courses
Course of the Month11 days, 14 hours left to enroll