Home > Archive > Visual Basic > February 2005 > newbie - how to resize column MSFlexGrid
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
newbie - how to resize column MSFlexGrid
|
|
| Newbee Adam 2005-02-15, 3:55 pm |
| newbie - how to resize column MSFlexGrid
I do not see in properties, maybe I am blind, how to change column width. I
would like to set it to just resize automatically to widest field of the
column, of corse.
can I, may I!!!???
thanks
--
Adam S
| |
| Bob Butler 2005-02-15, 3:55 pm |
| "Newbee Adam" <NewbeeAdam@discussions.microsoft.com> wrote in message
news:DA5D651F-1D19-4031-BCD6-B5DC7CE731B4@microsoft.com
> newbie - how to resize column MSFlexGrid
>
>
> I do not see in properties, maybe I am blind, how to change column
> width.
msflexgrid1.ColWidth(c)=whatever
> I would like to set it to just resize automatically to widest
> field of the column, of corse.
You'd have to loop through the list or track the values as you load the grid
and set it
--
Reply to the group so all can participate
VB.Net: "Fool me once..."
| |
| Rick Rothstein 2005-02-15, 3:55 pm |
| > > newbie - how to resize column MSFlexGrid
>
> msflexgrid1.ColWidth(c)=whatever
>
>
> You'd have to loop through the list or track the values as you load
the grid
> and set it
Here is a non-looping answer I gave to a similar question that was asked
in a past posting...
Rick - MVP
The following subroutine should resize your columns to match the largest
width in a column (setting it to zero width if no cell in the column has
anything in it). However, there is a condition placed on this... you
must be using the SAME font and font properties for ALL the cells. (Cell
font properties can be set individually, and if you have done this for
any cell within the column, you will need to examine each cell in the
column for the one with the greatest width.) One additional note... the
value of 6 used to multiply by the Screen objects TwipsPerPixelX
property is empirically derived. It appears that all cells have an
unprintable left and right margin area of 3 pixels each, so we must add
this amount to the width of text returned by the form's TextWidth
function (as long as the width is not zero). Also note that I have
hard-coded the FlexGrid's name inside the subroutine at the With
statement. If you require more flexibility than this, you can pass the
FlexGrid (As Control or As Object) into the Sub as an argument and use
that reference in the With statement.
Private Sub ResizeColumn(ColNumber As Long)
Dim CurrentRow As Long
Dim CurrentCol As Long
Dim ColText As String
Dim Fnt As Object
With MSFlexGrid1
CurrentRow = .Row
CurrentCol = .Col
.Row = 0
.Col = ColNumber
.RowSel = .Rows - 1
.ColSel = ColNumber
ColText = .Clip
Set Fnt = .Parent.Font
Set .Parent.Font = .Font
.ColWidth(ColNumber) = .Parent.TextWidth(ColText)
If .ColWidth(ColNumber) > 0 Then
.ColWidth(ColNumber) = .ColWidth(ColNumber) + _
6 * Screen.TwipsPerPixelX
End If
Set .Parent.Font = Fnt
.Row = CurrentRow
.Col = CurrentCol
End With
End Sub
| |
| Bob Butler 2005-02-15, 3:55 pm |
| "Rick Rothstein" <rickNOSPAMnews@NOSPAMcomcast.net> wrote in message
news:e3fDHG4EFHA.3824@TK2MSFTNGP10.phx.gbl
> Here is a non-looping answer I gave to a similar question that was
> asked in a past posting...
Nice; I hadn't ever looked at the .Clip property.
--
Reply to the group so all can participate
VB.Net: "Fool me once..."
| |
| Newbee Adam 2005-02-15, 3:55 pm |
| i put it in my program and it runs with no bus but it does not do anything.
Is the a property value I need to set so this works???
thanks, (I am SO excited to get this working! ;-)
"Rick Rothstein" wrote:
> the grid
>
> Here is a non-looping answer I gave to a similar question that was asked
> in a past posting...
>
> Rick - MVP
>
> The following subroutine should resize your columns to match the largest
> width in a column (setting it to zero width if no cell in the column has
> anything in it). However, there is a condition placed on this... you
> must be using the SAME font and font properties for ALL the cells. (Cell
> font properties can be set individually, and if you have done this for
> any cell within the column, you will need to examine each cell in the
> column for the one with the greatest width.) One additional note... the
> value of 6 used to multiply by the Screen objects TwipsPerPixelX
> property is empirically derived. It appears that all cells have an
> unprintable left and right margin area of 3 pixels each, so we must add
> this amount to the width of text returned by the form's TextWidth
> function (as long as the width is not zero). Also note that I have
> hard-coded the FlexGrid's name inside the subroutine at the With
> statement. If you require more flexibility than this, you can pass the
> FlexGrid (As Control or As Object) into the Sub as an argument and use
> that reference in the With statement.
>
> Private Sub ResizeColumn(ColNumber As Long)
> Dim CurrentRow As Long
> Dim CurrentCol As Long
> Dim ColText As String
> Dim Fnt As Object
> With MSFlexGrid1
> CurrentRow = .Row
> CurrentCol = .Col
> .Row = 0
> .Col = ColNumber
> .RowSel = .Rows - 1
> .ColSel = ColNumber
> ColText = .Clip
> Set Fnt = .Parent.Font
> Set .Parent.Font = .Font
> .ColWidth(ColNumber) = .Parent.TextWidth(ColText)
> If .ColWidth(ColNumber) > 0 Then
> .ColWidth(ColNumber) = .ColWidth(ColNumber) + _
> 6 * Screen.TwipsPerPixelX
> End If
> Set .Parent.Font = Fnt
> .Row = CurrentRow
> .Col = CurrentCol
> End With
> End Sub
>
>
| |
| Rick Rothstein 2005-02-15, 3:55 pm |
| > i put it in my program and it runs with no bus but it does not do
anything.
> Is the a property value I need to set so this works???
I presume you are calling the Sub and passing the column number into it
as an argument, right? If so, then the probable problem is that you
renamed your grid to something other than MSFlexGrid1 (which is the
default assigned by VB). That is not a problem, you just have to adjust
the Sub for that difference.
[color=darkred]
To make this adjustment, change the MSFlexGrid1 grid name above to the
name you gave your grid inside your program.
On a different note, it has been pointed out that you have multi-posted
your question to other newsgroups beside this one. Please read, take
note of, and follow the advice from this previous posting by Jeff
Johnson...
"You have posted this question individually to
multiple groups. This is called Multiposting
and it's BAD. Replies made in one group will
not be visible in the other groups, which
may cause multiple people to respond to your
question with the same answer because they
didn't know someone else had already done it.
This is a waste of time.
If you MUST post your message to multiple
groups, post a single message and select all
the groups (or type their names manually,
separated by commas) in which you want it to
be seen. This is called Crossposting and when
used properly it is GOOD."
Rick - MVP
| |
| Newbee Adam 2005-02-16, 3:56 pm |
| I did all you said but to no avail. But atleast with you help I was able to
change the colwidth so everything isvisible. I will play with you code for
auto resize again later becuase auto resize is my preference.
THANKS!!!
"Rick Rothstein" wrote:
> anything.
>
> I presume you are calling the Sub and passing the column number into it
> as an argument, right? If so, then the probable problem is that you
> renamed your grid to something other than MSFlexGrid1 (which is the
> default assigned by VB). That is not a problem, you just have to adjust
> the Sub for that difference.
>
>
> To make this adjustment, change the MSFlexGrid1 grid name above to the
> name you gave your grid inside your program.
>
> On a different note, it has been pointed out that you have multi-posted
> your question to other newsgroups beside this one. Please read, take
> note of, and follow the advice from this previous posting by Jeff
> Johnson...
>
> "You have posted this question individually to
> multiple groups. This is called Multiposting
> and it's BAD. Replies made in one group will
> not be visible in the other groups, which
> may cause multiple people to respond to your
> question with the same answer because they
> didn't know someone else had already done it.
> This is a waste of time.
>
> If you MUST post your message to multiple
> groups, post a single message and select all
> the groups (or type their names manually,
> separated by commas) in which you want it to
> be seen. This is called Crossposting and when
> used properly it is GOOD."
>
> Rick - MVP
>
>
| |
| Rick Rothstein 2005-02-16, 3:56 pm |
| > I did all you said but to no avail. But atleast with you help I was
able to
> change the colwidth so everything isvisible. I will play with you code
for
> auto resize again later becuase auto resize is my preference.
> THANKS!!!
The subroutine should work exactly as written (except for the name
change we just discussed)... I did test it before I posted it the first
time. When you say it doesn't work for you... can you explain that in
more detail? Is the program crashing? If yes, what error message are you
getting and what line is it stopping on? Are you getting it to resize to
something, just not what you expect? If so, describe that please. We
would like to help you out, but you need to supply us with more details
as to what is happening. Remember, we can't see your computer... we can
only use the information you tell us to construct our answers.
Rick - MVP
|
|
|
|
|