Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Autosize Textbox / Label 1

Status
Not open for further replies.

Sylv4n

Technical User
Feb 27, 2002
83
GB
I have had a problem when trying to get my label (but same logic would work for a text box) to autosize dependant on what text is in there.

I have found a simpe(ish) solution

There are two text boxes - TextBoxA & TextBoxB
then you type into TextBoxA and press a button it autosizes TextBoxB

the logic is this:

When you press the button it creates a new form and puts a label(or text box) on that form, it then transferes the text over, works out the width then brings that width back and works out its relation to what the curren width is

The Code is:
Code:
Private Sub TransferButton_Click()

'Set the TextBox Height, Width and Top
Dim TextBoxHeight As Long
Dim TextBoxWidth As Long
Dim TextBoxTop As Long
TextBoxHeight = 223
TextBoxWidth = 2438
TextBoxTop = 2324

Me.TextBoxB.Height = TextBoxHeight
Me.TextBoxB.Width = TextBoxWidth
Me.TextBoxB.Top = TextBoxTop

'Sort Out Text Box A (Ensure that it does not contain Null infomration)
If IsNull(Me.TextBoxA.Value) Or Me.TextBoxA.Value = "" Then
    Me.TextBoxA.Value = "THIS IS A LONG STRING PLEASE TAKE NOTE OF THAT AND WATCH THE MAGIC OF AUTOSIZING!"
    MsgBox "You left TextBoxA blank Default Text Entered"
End If

'The Text to be put inot the caption
Dim strText As String
strText = Me.TextBoxA.Value

'Get the required ammount
Dim strRequired As String
strRequired = SizeNewControl(strText)

'Calculate the height
If strRequired <= TextBoxWidth Then
    'Set the line height to one
    Me.TextBoxB.Height = TextBoxHeight
Else
    'It Is bigger than one line!
    Dim LineX As Integer
    Dim GotTheRequirements As Boolean
    
    LineX = 2
    GotTheRequirements = False
    
    While GotTheRequirements = False
        
        If strRequired <= (TextBoxWidth * LineX) Then
            Me.TextBoxB.Height = TextBoxHeight * LineX
            GotTheRequirements = True
        Else
            LineX = LineX + 1
        End If
    
    Wend
End If

Me.TextBoxB.Caption = strText

End Sub

'------------------------------------------------------

Public Function SizeNewControl(strText As String) As String
    Dim frm As Form, ctl As Control

    ' Create new form.
    Set frm = CreateForm
    'Hide the form
    frm.Visible = False
    ' Create new command button.
    Set ctl = CreateControl(frm.Name, acLabel, , , , 0, 0)
    ' Set control's Caption property.
    ctl.Caption = strText
    ' Size control to fit caption.
    ctl.SizeToFit
    'Send the new width back
    SizeNewControl = ctl.Width
    'Close the form
    DoCmd.Close acForm, frm.Name, acSaveNo

End Function
Note the above will only work if you have a form with two text box 'TextBoxA' and 'TextBoxB' and a button 'TransferButton' and the form will have to be too big - to account for the tex box biend its biggest, it cant go off the form, this can be easaly be resized at the end by using the command:

Me.Section(0).Height = 'The Required height in Twips


ANY QUESTIONS??
 
Sylv4n,
Ouch ouch ouch!! Sorry, but for what you're trying to achieve, you're having to do a lot of 'work'.

Question: If the method works with text boxes, but not with labels, then why not just use a textbox on the form instead of a label where you require this capability. After all, an unbound textbox, with a transparent background which is locked and not enabled functions identically to a label control. Not as clever as your solution, but for us 'lazy ones', a lot more simple.

Still, full marks for enginuity; also a star from me for demonstrating good control and imagination in finding a workaround to the problem.

Regards,


Steve Lewy
Solutions Developer
steve@lewycomputing.com.au
(dont cut corners or you'll go round in circles)
 
but I would have the same problem with text boxes, the problem is I wonty know the height as I am using the same form which will open in many diffrent fassions to display diffrent dsata, yety it needs to be flexible enough to have new data added. I have one where it autosizes to have about 40 little boxes and if they were big the detail section woukld not cover the whole form, but on the other hand I have another wayt the form can open to display only 14-15 diffrent large boxces and there are at least 25 of these diffrent ways it can open at the moment and I need the functionallity to add more. the only problem with this solution is that it wont display:

&quot;A Magnet will have a big &quot;
&quot;Magnetic field I have no idea &quot;
&quot;Why &quot;

properly it cuts the Why out. I am currenyly working on that, although there will be only a few ituations such as this in my workplace they have a tendency to use large words :).

I should be able to achive my goal by playing arround with the text on the next form.

Can you see whay I use the code?
 
OK. I see what you mean; I misinterpreted your intitial post to mean that you didnt need to do all of this for text boxes. I still think that there would be an easier way than having to work on a form in design view, but since you've got this method working for you, then thats the way to go.

Typically if text data exceeds the size of the control, you then scroll or navigate down to see the rest of the text. You obviously cannot cater on a form for all of the data that you may wish to display (eg. a large memo field), so I would have to question your user interface design requirements here anyway. Thats possibly because I don't really understand your interface requirement.

Food for thought.
Good luck,
Steve Lewy
Solutions Developer
steve@lewycomputing.com.au
(dont cut corners or you'll go round in circles)
 
here is smaller and more refined version, and by the way I welcome and other ideas of how to compleate this but I has of yet using Access '97 have not found one.

CODE

Code:
Private Sub TransferButton_Click()

'Set the TextBox Height, Width and Top
Dim TextBoxHeight As Long
Dim TextBoxWidth As Long
Dim TextBoxTop As Long
TextBoxHeight = 223
TextBoxWidth = 2438
TextBoxTop = 2324

Me.TextBoxB.Width = TextBoxWidth
Me.TextBoxB.Top = TextBoxTop

'Sort Out Text Box A (Ensure that it does not contain Null infomration)
If IsNull(Me.TextBoxA.Value) Or Me.TextBoxA.Value = &quot;&quot; Then
    Me.TextBoxA.Value = &quot;THIS IS A LONG STRING PLEASE TAKE NOTE OF THAT AND WATCH THE MAGIC OF AUTOSIZING!&quot;
    MsgBox &quot;You left TextBoxA blank Default Text Entered&quot;
End If

Me.TextBoxB.Caption = Me.TextBoxA.Value
Me.TextBoxB.Height = SizeNewControl(Me.TextBoxA.Value, TextBoxHeight, TextBoxWidth)

End Sub

Public Function SizeNewControl(strText As String, TextBoxHeight As Long, TextBoxWidth As Long) As String
    Dim frm As Form, ctl As Control, LineX As Integer

    ' Create new form.
    Set frm = CreateForm
    'Hide the form
    frm.Visible = False
    ' Create new label.
    Set ctl = CreateControl(frm.Name, acLabel, , , , 0, 0, TextBoxWidth, (TextBoxHeight * 2))
    ' Set control's Caption property.
    ctl.Caption = strText
    ' Size control to fit caption.
    ctl.SizeToFit
    ' Send the new width back
    SizeNewControl = ctl.Height
    ' Close the form
    DoCmd.Close acForm, frm.Name, acSaveNo

End Function
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top