Re: Set Column Width for MSHFlexgrid
- From: "Rick Rothstein" <rickNOSPAMnews@xxxxxxxxxxxxxxxxx>
- Date: Fri, 22 Apr 2005 09:52:41 -0400
*** Repaired for top-down posting... see answer below ***
> > > I have an MSHFlexGrid, and i would like to set the width for one
> > > of the column to the size of the object(MSHFlexGrid).
> >
> > I'm not sure what you are trying to do here. Are you attempting to
make
> > one specified column be the same width at the "working" area of the
> > grid? In other words, are you trying to have a specific column fill
the
> > grid's display area completely? If so, what about if there are fixed
> > columns? Do you want the column to fill the non-fixed width of the
> > control?
>
> In my MSHflexgrid, i am using the method:
>
> set_colwidth (int, int)
> Let s say i have only one column and i want to set the column width to
the
> same width as the MSHFlexgrid itself
>
> in the second parameter, i have to add the col width i want but i
have some
> difficulty to set the column width because the measure are in pixel
>
> MyHSFlexgrid.width=640
> MyHSFlexgrid.set_colwidth(0,MyHSFlexgrid.width) => result: column is
smaller
>
> hope this is clear
No, it still isn't completely clear to me. You haven't defined if you
can have more than one column in your grid. Also, you didn't address the
FixedCol question. Anyway, I am feeling generous this morning, so I took
a stab at developing some code which I think will do what I think you
want. Copy the code below into the code window of the form on which your
FlexGrid is located. To use the Sub, simply pass it the column you want
to enlarge to the width of the grid. First, however, I need to explain a
couple of things. First, instead of adding extra code to test for, and
adjust around, the vertical scrollbar either being in the grid or not
(depending on how many rows and columns there are), I have chosen a
kludge type method to get around the problem. The GetClientRect obtains
the area of the grid that is not occupied by scrollbars. I simply expand
the specified column to the width of the grid and test this column width
against the client area after the expansion. If the column width is
greater than the now current client area, then a vertical scrollbar was
introduced and I run the adjustment calculation a second time to correct
for it. The Adjustment variable I introduced seems to be necessary to
make things look nice depending on whether the last column is selected
or not and what the BorderStyle is. Remember, this is a quickly
developed piece of code, so you should test it out to make sure I got
everything right. And one last thing... I don't do database programming,
so I don't use the Hierarchical FlexGrid (which your code samples
indicate you are using), so I haven't tested this code out against that
control. However, I have tested it against the normal FlexGrid and it
seems to work fine for that control. I am assuming it will also work
against your Hierarchical FlexGrid control too. If not, would you please
let me know. Finally, the name of the FlexGrid is hard-coded in the Sub
directly (it appears only once in the With statement), so you can change
it if needed.
Rick - MVP
Private Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type
Private Declare Function GetClientRect _
Lib "user32" _
(ByVal hWnd As Long, _
lpRect As RECT) As Long
Private Sub SetColumnToFullWidth(ColNumber As Long)
Dim X As Long
Dim Difference As Long
Dim Adjustment As Long
Dim GridAreaWidth As Double
Dim NewColumnWidth As Double
Dim FixedColWidth As Double
Dim GridArea As RECT
With MyHSFlexgrid
If ColNumber > .FixedCols And ColNumber < .Cols Then
For X = 0 To .FixedCols - 1
FixedColWidth = FixedColWidth + .ColWidth(X)
Next
Call GetClientRect(.hWnd, GridArea)
GridAreaWidth = GridArea.Right - GridArea.Left '- 1
NewColumnWidth = Int((GridAreaWidth - ScaleX(FixedColWidth, _
vbTwips, vbPixels)))
Adjustment = 1 - .BorderStyle
.ColWidth(ColNumber) = ScaleX(NewColumnWidth + _
Adjustment, vbPixels, vbTwips)
Call GetClientRect(.hWnd, GridArea)
If GridArea.Right - GridArea.Left - 1 < GridAreaWidth Then
GridAreaWidth = GridArea.Right - GridArea.Left - 1
NewColumnWidth = Int((GridAreaWidth - ScaleX(FixedColWidth, _
vbTwips, vbPixels)))
End If
.ColWidth(ColNumber) = ScaleX(NewColumnWidth + _
Adjustment, vbPixels, vbTwips)
.LeftCol = ColNumber
Else
' A bad column number was specified, handle error here
MsgBox "An invalid ColNumber argument was specified!", _
vbExclamation, "Invalid ColNumber Argument"
End If
End With
End Sub
.
- Follow-Ups:
- Re: Set Column Width for MSHFlexgrid
- From: oLiVieR CheNeSoN
- Re: Set Column Width for MSHFlexgrid
- From: Rick Rothstein
- Re: Set Column Width for MSHFlexgrid
- References:
- Set Column Width for MSHFlexgrid
- From: oLiVieR CheNeSoN
- Re: Set Column Width for MSHFlexgrid
- From: Rick Rothstein
- Re: Set Column Width for MSHFlexgrid
- From: oLiVieR CheNeSoN
- Set Column Width for MSHFlexgrid
- Prev by Date: VB Form vs VBA UserForm problems...
- Next by Date: Re: Set Column Width for MSHFlexgrid
- Previous by thread: Re: Set Column Width for MSHFlexgrid
- Next by thread: Re: Set Column Width for MSHFlexgrid
- Index(es):
Relevant Pages
|