Solved

Minimun text length of wrapped text

Posted on 2002-05-23
10
194 Views
Last Modified: 2010-05-02
I have written a method to autosize a datagrid but the problem is getting the minimun width of the caption header when the header has more than 1 line and when there are more words than lines.  If I just autosize by the data you may not see all the header.  If I the the length of the header I get the whole string which makes the column much too wide.


Ex caption = "Receive UnCor Rdg"
lines in the header = 2
the shortest possible length is 9 characters
1st line = Receive      
2nd line = UnCor Rdg


I want to find out the minimum width required to display the text.  This is just 1 example.  
0
Comment
Question by:jdesharnais
[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
  • Learn & ask questions
  • 5
  • 4
10 Comments
 
LVL 6

Expert Comment

by:pierrecampe
ID: 7030712
use a label (maybe visible=false)
autosize=true
wordwrap=true
put your text in this label and the label will scale to the smallest possible width
so now set that column.width  to label.width

or you could use the drawtext api
0
 
LVL 100

Expert Comment

by:mlmcc
ID: 7031150
learning
0
 
LVL 1

Author Comment

by:jdesharnais
ID: 7031954
pierrecampe
You have a good idea but the label control does not resize to the smallest possible.  It resizes to display all the caption.  It would also be hard to make it the right heigth for multiple lines.  

I would to use this sub to resize any grid.  I cuurently have 10 in my projects
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 6

Expert Comment

by:pierrecampe
ID: 7034428
ok i misinterpreted your question
however the answer is almost the same: use a label
if i understand right you want the text to be a certain hight ie 2 rowheights
if so just put your text i the label and step by step incraese int width until its height is the desired hight and then you'l have your minimum width
an example to show more clearly:
have a form with a label,autosize=true,wordwrap=true

Private Sub Command1_Click()
    wantedheight = 600
    Do Until Label1.Height < wantedheight
       Label1.Width = Label1.Width + Screen.TwipsPerPixelX
    Loop
End Sub

Private Sub Form_Load()
    Label1 = "anything allways yes no never"
End Sub

0
 
LVL 1

Author Comment

by:jdesharnais
ID: 7047696
pierrecampe I tried your idea but it is hard to determine the rigth height the label needs to be.
I asked the question because I thougth someone may have a sub written already.  I guess wrong.

I wrote my own routine
this breaks up a string into lines.  the calling function gets a array of lines.

Sub minwidth(txt As String, lines As Integer, ByRef a() As String)
Dim b() As String
Dim liLength As Long
Dim lilines As Integer
Dim x As Integer
ReDim b(lines - 1)
 a = Split(txt, " ")
    liLength = Round(((Len(txt) - lines) / lines) - 0.5)
  For x = 0 To UBound(a)
    If Len(a(x)) > liLength Then
      liLength = Len(a(x))
    End If
  Next x
  Do
Erase b
ReDim b(lines - 1)
lilines = 0
b(lilines) = a(0)

  For x = 1 To UBound(a)
    If Len(a(x)) + Len(b(lilines)) + 1 <= liLength Then '+1 for space
        b(lilines) = b(lilines) + " " + a(x)
    Else
    lilines = lilines + 1
      If lilines > lines - 1 Then
        Exit For
      Else
        liLength = liLength + 1
        b(lilines) = a(x)
     
      End If
    End If
  Next x
  Loop Until lilines <= lines - 1
End Sub


Not sure what to do when you answer your own question
0
 
LVL 6

Expert Comment

by:pierrecampe
ID: 7048384
jdesharnais
if this is supposed to be a joke it is one of EXTREME bad taste
>>it is hard to determine the rigth height the label needs to be
are you joking ?
if you need a header of 2 lines height just give a autosize label the same font as your grid and put a 2 word sentence in it and it WILL autosize to the correct height and then you WILL know the needed height
and then put your real text in it and make it wider until it is the right height
>>Not sure what to do when you answer your own question
well you did NOT answer your own question
the LEN function does NOT give you the lenght of text
all it does is give you the number of characters
But as it seems clear to me you dont want to give points just ask community support to reduce this questions points to 0 and move it to the paq

 
0
 
LVL 6

Expert Comment

by:pierrecampe
ID: 7048385
Oh yes i'm leaving today on a 7 day wilderness trip so i wont be able to see any comments for 7 day
0
 
LVL 1

Author Comment

by:jdesharnais
ID: 7051108
pierrecampe,
I do not mind giving you the answer, points is not an issue.  You did give me an idea with the label.  I increase the length of the line until it fits.  You are right I should of user the .textwidth method.  I do use the method in the calling routine.  Using the len function is not the most accurate but it is very close and when it doesn't fit you will not need loop many times to find the correct length.  with the .textwidth you may have to loop from several hundered to serveral thousand.  If you increase by larger multiples you are no further ahead.  

Hope you had a good wilderness trip.

0
 
LVL 6

Accepted Solution

by:
pierrecampe earned 200 total points
ID: 7065740
'thank you the trip was very tiring, but wonderfull
'i still have the feeling that we do not understand each other 100%
'so here is an example of what i mean:
'have a form with a label and a datagrid
'code:

Private Sub Form_Load()
    DataGrid1.HeadLines = 2
    DataGrid1.Splits(0).Columns(0).WrapText = True
       
    Label1.Appearance = 0 'flat
    Label1.AutoSize = True
    Label1.BorderStyle = 1 'fixed single
    Label1.WordWrap = True
    Label1.Visible = False
   
    a$ = "azezae WDFF ghhkjkjh tyuytuytut io at fghgff QJ Wjeryztere"
    DataGrid1.Columns(0).Caption = a$
    Label1.Caption = a$
    Do While Label1.Height > DataGrid1.RowHeight * 2
       Label1.Width = Label1.Width + Screen.TwipsPerPixelX
    Loop
    DataGrid1.Columns(0).Width = Label1.Width + TextWidth("i")
End Sub
0
 
LVL 1

Author Comment

by:jdesharnais
ID: 7093733
Sorry for not replying earlier, I have been away.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Determine Range to Select 5 56
using web browser with BING 40 136
Problem to With line 4 75
VB6 - Cannot type when doing an extract from MSHFlexgrid to Excel 6 38
The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question