Tracking Forums, Newsgroups, Maling Lists
Home Scripts Tutorials Tracker Forums
  HOME    TRACKER    Visual Basic

Calculating String Width

Given a particular font and point size how do I determine the width of a string?

The font is Courier in this case and the size is 10

View Complete Forum Thread with Replies

See Related Forum Messages: Follow the Links Below to View Complete Thread
Calculating Textbox And Label Width
I am trying to figure out how to calculate how wide a label or text box needs to be to hold a string of a certain number of characters. I realize it can be done manually as the form is built. I would prefer to be able to calculate the necessary width prior to building the form.

I can get the string length using "len"; however, I have no idea how to translate the number of characters to width of the control. It would appear I need to factor in the Font Name and Font Size. I realize some Fonts have varying widths for different characters. I assume I should use the width of the "M" in my calculations.

Any suggestions as to how the combination of Font Name, Font Size and number of characters can be translated UserForm measurements would be appreciated.

Width Of Text In No-fixed Width String
How can I find the wdith of a string of text in a non-fixed width font?

I am trying to format a comment box in Excel VBA, and I can't use a carraige return, so I am trying to set the width of the comment box and then fit my text into rows and columns.

Calculating String
I have a field that is set up as text which holds the calculation formula in my database.

Legend of what the constants are
TS = Total Food + Total Service
FS = Food Sales
SS = Service Sales
PF = Port Fee
TX = Taxes

Here is an example of what I mean. Calculation Field in the database might be set to.

TS + (FS * TX) + (SS * PF)

Now in my program the user definds that
TS = 1000
FS = 700
SS = 300
TX = 8%
PF = 6%

So the formula would end up looking like this
1000 + (700 * .08) + (300 * .06)

What I need to do is get the value of this into a variable set as double, but the formual is a string. Is there an easy way of doing this. Here is some vb code that kind of shows what I mean.

VB Code:
Dim s As Double    Dim sFormula As String     sFormula = "TS + (FS * TX) + (SS * PF)"    sFormula = Replace(sFormula, "TS", "1000")    sFormula = Replace(sFormula, "FS", "700")    sFormula = Replace(sFormula, "SS", "300")    sFormula = Replace(sFormula, "TX", ".08")    sFormula = Replace(sFormula, "PF", ".06")    'I want the result of the formula go into my S    'variable but this does not work.    S = Val(sFormula)

Calculating A String Formula
I have a formula string that is stored in a database. "((<PP> - 216.81) * .01) + 6)"

I perform a replace to complete the formula like so:

sFormula = replace(rst.fields("Formula").value,"<PP>", txtPP.text)

so now the formula reads: "((396-216.81) * .01) + 6)" but is still a string.

now how can I calculate this string at runtime?

any ideas would be appreciated.

Calculating String Equations
I have tried looking through various forums trying to find out if anyone knows how to calculate a value for and equation contained in a string eg "1 + 2 + 3" to make equal to 6
One of the forums suggested Microsoft's Script Control and use it's 'Eval' function to calculate it. Unfortunately when I try to run my program it crashes. I'm using VB 5 Control Creation Edn.
as it does not seam to get as close in VB 4 Professional. (Which I have. Does anyone have any suggestions on how to either fix it or any alternative.

Calculating Length Of String

i have a problem with displaying data in a list box neatly.This is how it looks.

computer serial number
printer serial number
computer serial number

I somehow need to calculate the string first so the vairiable 'sp' can change according to the length of the string, which should make the spacing the same. Hope this makes sense. here is my code:

sp = 13
Do While Not rs.EOF

List1.AddItem "" & rs![category] & Space(sp) & rs![serial]



thanks in advance

Calculating String Sums
I would like to know if there is any function in VB by which you can 'parse' a string containing a sum, like "1+1", and get a number containing the result. JavaScript has the eval() function which you can use for this purpose, but I have not been able to find any such function in VB. Any help would be greatly appreciated!

Calculating From 1 String (Like Command Line)

Im wondering, is it possible to make visual basic calculate from 1 string?
Calculate("1 * 5 - 1") ' OR
Calculate("10 - 5 * 2")

Like that...

(Yes, that are simple examples, but i want to do more advanced stuff with it)


Gr. Ron

Calculating A File/string Checksum
I need a quick way of displaying a checksum of a file or string approximately 2MB in size.
I tried using a CRC but got errors because the file contains null characters.

Calculating String Lenght Taking Font Into Account?
Hi all

Any suggestions how can I calculate the length of some string cocncerning in what font it will be printed out? What I need to do is to see if a string fits in a table cell and if not then manually split it into smaller pieces and manually insert these pieces in cells below and not let word do it and extend one row into more rows.

I hope you all understand what I wanted to say.

Thanks for the help.


Width Of String

How can I find the width of a given string ?
Thanks !

String Width
Are there any function that return the width of a string in VB6? (not the no. of characters but width)

String Width ... How ? ...
Hi all

How can I know how many pixels there are in a string ?
Indeed, I need to fit a ComboBox width to the longest string of its items. I can get the string length but the problem is that all letters haven't the same width ... So, I would like its width ...
Thanks in advance.


Edited by - Rody on 8/1/2004 2:53:52 AM

String Width

i'm filling a flexgrid with strings and i would like to adjust the column width of my flexgrid so that the text fits in. Does anyone know how (how can i determine the width of a string).


String Width/height
I am looking for a good way to calculate a string's dimensions as if printed somewhere.
Think of a "stringwidth" and a "stringheight" function that would take as parameter one string and maybe a Font object.
Any suggestions?

Determining Width Of Text String???
How can I determine the actual width of a text string
(ie NON fixed-width fonts)???

Measuring The Graphical Width Of A String
I'm trying to develop a textbox that expands as the text in it wraps, but I am having difficulty figuring out a generic method for identifying the graphical width of a string without knowing the font that is being used.

I have read about the ClientDC.GetTextExtent method in C++. Is there something like that in VB? What properties should I be looking at?

I appreciate everyones' assistance.


Width Of String / Or Font In Pixels

Does anyone know how I would find out the width of an entire string, or the
width of a font in pixels at run time? I need this as I am producing a
program with various dynamic elements where this is necessary to size
certain components. There aren't any work arounds I can think of, so I
really need an answer to this before I can continue.....



Number To Fixed Width Right Aligned String?
Hi All
I want to know if there is any function in VB6 which is like STR function in SQL Server.
STR function takes three arguments viz. number to be formatted, width of resulting string and decimal places required.
I want such function in VB6 where if I supply number, total width and decimal places required, I will get a string right aligned within specified width. If number is smaller, it must get padded with spaces.
I hope VB6 must be having such a basic function.
Please help

How To Know A Width(pixel) Enough To String Display On A Form
Please tell me!
When the string was drew on a form. How many pixel it need to display?

How Calculate Width Required For Text String?
Hi all,

I need to calculate the width and height of a text.
This works perfect with using the printer object
(Printer.TextWidth and the like).

However, if there is no printer installed, this
strategy fails and I get a "printer error".

Could someone possibly name me an alternative method of
how to calculate text-width - or how to use the
printer-object if there is no printer(?)

Any help will be appreciated.

Best regards

How Can We Calculate The Width Of The Control From The Length Of A String
How can we calculate the width of the control from the length of a string. I can get the length of a string using len(strvar), now I need to reset the width of the control (label or command button) so that the strvar is completely displayed as the caption of the control.



Data Report - Paper Width Is Larger Than Printer Width
I am using the data report for my app which is running in several machines. I am getting an error "Paper width is larger than printer width" in only one machine. What may be the problem ?


Combo Box Dropdown List Width, Setting The Width *RESOLVED*
I found a way of changing the width of the dropdown list of a combo box by subclassing it. Is there any other way to do it? Can I subclass two seperate combo boxes in the same form or would it cause a problem (there seems to be when I tried it)?


Report Width Larger Than Paper Width Error In Datareport
I have to print a report (datareport), there is a predefined certificate on which I have to print my report

problem is, the certificate is a bit wide...and i am getting "report width larger than paper width error"

 i have dragged the scale (width) to 8.1 or 8.2, beyond that if i drag, i am getting the error

  i have set the datareport properties leftmargin, rightmargin, topmargin and bottommargin as 0.

  i have increased the reportwidth property value, still no use

  i want to increase the width of my report, (scale > 8.5), how to do it?


Error: Report Width Is Larger Than The Paper Width
i create few reports using Data Environment & Data Report. the reports r in A4 size (portrait), and some r in A4 size (landscape). when display the report which is portrait, it has no error. but when display the report which is landscape, an error says 'Report width is larger than the paper width'.

to display the landscape report, i must goto printer setting and set the paper orientation to landscape. then all my report can display.
BUT, the height of the portrait report is shorter. i mean the bottom of the portrait report will be blank (4 inch from bottom is blank)

i try to change the orientation before i open&display the report, but it doesnt work.
how to change the orientation, and the paper? pls help. Thanks.

Report Width Larger Than The Paper Width ERROR
Hi ,
I am using data reports for getting some reports from my application.While displaying or printing my reports, I get an error saying 'Report width is larger than the paper width.'
Now irrespective of the paper width settings of the printer I want that when I use the application at my client side, it should adjust the paper settings and printer settings so as to show and print the report.
Can anyone help me out on this....I have very little time at hand...and its ticking away...tick..tick...tick.....Help me

Thanks in advance.......

Report Width Larger Then Paper Width
i get the above error when i try to generate a report in vb. i am using the report designer, but i have checked the report and there is not label or text field or anything lapping over page - what could be the problem? any help would be greatly appreciated!

Report Width Is Larger Than The Paper Width ???
Hello all,

I just make an data report.
but when i try to run the project, there is error :

"Report Width is Larger than The Paper Width"

How to set the Paper Width ? Can i set it by code ? And How ?

I was try to setting my printer default on Lanscape paper, but still not working..

Please help..


Report Width Is Larger Than The Paper Width
i always get this error "Report Width Is Larger Than the Paper Width" when printing my data report. anyone knows how to solve this? thanks

MSFlexgrid - Set Column Width To Text Width..
How do I set the column width to be = to the amount of text

(the text length varies)

Report Width Is Larger Than The Paper Width
Dear vbguru out there,

I have encountered this error message "Report width is larger than the paper width" while exporting my form to data report in vb. Can anyone here help me in this? I have checked the microsoft support website and it said to download the latest service pack. I have done that but still getting the same error. Not sure why...Do I need to do anything after installing the lastest service pack? Urgently needed help here. Thanks in advance...

Report Width Is Larger Than The Paper Width
What to do when
"Report width is larger than the Paper width" error
is raised while exporting the Data Report to a .txt
or .html file.

Report Width Is Larger Than Paper Width
Report width is larger than paper width ...?

can somebody help me fix this program?? i got a presentation in a day
it keep giving error msg "REPORT WIDTH IS LARGER THAN PAPER WIDTH" when i click on booking details and cruise details

Set Form Width Based On Width Of Text
I have the following code:


For intIndex = 0 To lstText.ListCount - 1

strData = lstText.List(intIndex)

'Size report based on length of text
If TextWidth(strData) > lngWidth Then
lngWidth = TextWidth(strData)
End If

txtData.SelText = strData & vbCrLf

Next intIndex

Me.Width = lngWidth + (DATA_WIDTH_OFFSET * 2)

DATA_WIDTH_OFFSET is the length of the space between the side of the form and the side of the text. The Form_Resize automatically adjusts the width of the textbox based on the width of the form.

The problem is that it's not coming up with the correct size. The end result (the width of the form) ends up being about half the size it should be.

Is there a trick tp getting this to work properly?

Image To A String Im My Application?, And Tell The Width / Height Of An Image?

I wanna do the following in my application

Load a picture into a string,
Tell how wide / tall the image in a PIXELS it is, and if it is less then 150 px's each way load it into my pct box

is this easy or?

[Main problem for now is: i am using:

VB Code:
On Error GoTo ErrorSub:    Me.CommDia1.Filter = "Avatar Image Files (*.gif)|*.gif"    Me.CommDia1.InitDir = App.Path & "Avatars"    Me.CommDia1.ShowOpen    Dim aFileName As String, strAvatar As String    aFileName = Me.CommDia1.FileName    If Len(aFileName) > 0 Then        strAvatar = LoadPicture(aFileName)        imgAV(0).Picture = strAvatar    End IfExit SubErrorSub:    If Err.Number = "32755" Then        ' It's a Cancel Error, Just Ignore it        ' And Exit the SubRoutine        Exit Sub    ElseIf Err.Number = "76" Then        ' Avatar Directory Doesn't Exsist, Lets Make It        MkDir App.Path & "Avatars"        Err.Clear        Resume Next    Else        MsgBox "Error: " & Err.Description, vbOKOnly, "Error # " & Err.Number        Err.Clear    End If

And strAvatar isn't dimmed as the correct type,,, whats the correct type for the Picture?

Print {"report Width Longer Than Paper Width" Error With Deployed DataReport}
I use datareport to make report in my project when I setup this project and run it in my computer it works good but when I install it in other computer and I want to print this message appear :
report width longer than paper width.
please help me.

Frame Width = Label Width
How can I set the frame width the same as a label width ?
(the label is inside the frame)

Form Width Vs Flexgrid Width
Im facing some problems in the Form resizing along with the Flex grid size.
I made a procedure, which can display the records in a flex grid from any Select statement (SQL). No.of columns ,Colomname, rows, Cellwidth everything is in run time and those are working fine

But when SQL is bringing more columns then I have to increase the form width and the Flexgrid width to show the details. The form width should be increased based on the Flexgrid width. But once form width is not increasing more than 15000 . (My Resolution is 1024)

I understand it is depending on the Screen size and If form width is more than the screen then it will not increase further.
But is it possible to have a Scroll bar in a form to show the remaining part of the grid ?
How can solve this problem ?

Pls suggest an idea

Width Of Form Vs Width Of Picturebox
Hello all,

I know this might be simple but I cant figure it out!, I have a picturebox that is almost as large as it is the form, but in the Width property of the picturebox I have 631 and in the width property of the form I have 10008, how come? they are almost the same, I should have moved something since I have other project where the units match. Help pleas!

Data Report In Vb6|error:report Width Is Greater Than Page Width|what Can I Do?
i am building my report on vb6 data report. i have to make a wide report since i have so many fields to accomodate. but when i try to run the app. it says the report width is greater than the page width. how can i get over this situation.
i switched over to vb6 data report because..
( i tried making reports on crystal reports 8.5 but i can adjust so many fields on the single report even after changing the page setup to landscape instead of portrait.)
can't i make bigger reports? please help.

Data Report In Vb6|error:report Width Is Greater Than Page Width|what Can I Do?
i am building my report on vb6 data report. i have to make a wide report since i have so many fields to accomodate. but when i try to run the app. it says the report width is greater than the page width. how can i get over this situation.
i switched over to vb6 data report because..
( i tried making reports on crystal reports 8.5 but i can't adjust so many fields on the single report even after changing the page setup to landscape instead of portrait.)
can't i make bigger reports? please help.

How Does WebBrowser.Height And WebBrowse.Width Translate To Form.Height And Width?
I am using Visual Basic 6, on a Windows XP SP2 machine.

I have a website that users surf using my VB application.
This application has two Forms, Form1 and Form2.
Both of these Forms have a WebBrowser object embedded in each of them.

Now, let's say that a user is browsing my website using Form1.

Some of the links on this website use the javascript method to pop open a new window.

Code:'page.htm', 'myWindow', 'width=640,height=480');

In Form1, I am using the NewWindow2 method to trap for this event:


Private Sub WebBrowser1_NewWindow2( _
ByRef ppDisp As Object, _
ByRef Cancel As Boolean)

Form2.WebBrowser1.RegisterAsBrowser = True
Set ppDisp = Form2.WebBrowser1.Object

End Sub

I want Form2 to act as the "popup" window, and honor the sizes specified by the Javascript call.

From Form2, I can get the width and height values specified by the javascript call by either

1) Query the pDisp object from the BeforeNavigate2 method


Private Sub WebBrowser1_BeforeNavigate2( _
ByVal pDisp As Object, _
ByRef URL As Variant, _
ByRef flags As Variant, _
ByRef TargetFrameName As Variant, _
ByRef PostData As Variant, _
ByRef Headers As Variant, _
ByRef Cancel As Boolean)

Debug.Print("Width: " & pDisp.Width & ", Height: " & pDisp.Height)

End Sub

2) Get CX and CY values from the ClientToHostWindow method


Private Sub WebBrowser1_ClientToHostWindow(CX As Long, CY As Long)
Debug.Print("Width: " & cstr(CX) & ", Height: " & cstr(CY))
End Sub

QUESTION What is the mathematical formula to translate the pixel values into the correct Form2.Height and Form2.Width values?

For example, if I want my Form2 to be 640 x 480 px, what is the mathematical formula to translate that into something VB can use:

For 640px X 480px...

Form2.Height = ??? (What is the formula to get this?)
Form2. Width = ??? (What is the formula to get this?)

Thank you

How Change Width Of A Sub-form Depending On The Width Of The Parent-form?

Iím sitting here trying to get the forms in an Access database to look the way I want considering different Windows resolutions the users of the database may have.

What I want to do is to change the width of a sub-form depending on the current size of the form itís placed in. Is this possible to achieve in some way using VBA code?

I apologize if this may be a trivial question, but I havenít found an answer to it when searching the FAQs and the help included in MS Access itself.


Determine If Label Caption Width Is Greater Than Label Width
Is there anyway to add a check to deteremine if a caption of a label is longer than the label width, and if it is, omit the last few characters to make it fit and add "..."

Example: If "How are you doing?" doesn't fit in the label, it'll show "How are y..." instead.

Error In Data Report(VB6) "Report Width Larger Than Paper Width"
I am suppose to generate a Data report in VB6..
but i am geting an error like 'Report width larger than paper width'
my data report width is 10000
i have also tried it by reducing it to 8000
but still geeting the same error......
is it because of the few large datafields in the report....

please tell me the solution...i needit urgently


Text Width/Height = Window Width/Height
can somebody pls show me how to make this work so that when you have a text like this;

I dont understand u oooooooooo, cos yesterday someone told me that JOY'
ve finally parked into ur apartment and BLESSING' ve refuse to go, Although
COMFORT is heavily by your side, yet GRACE ve vowed never to let go of u,
still PATIENCE is strongly emboided in ur heart.Despite all this. HOPE is laying
waiting 4 u all these years, while SUCCESS ve vowed to be ur crown. Remain
Bless. Send this with love to all those you love or hate including me, if only I am loved, He cares.

OR something smaller

I dont understand u oooooooooo, cos yesterday someone told me that JOY'
ve finally parked into ur apartment and BLESSING' ve refuse to go.

you'd be able to make the Label1 that is going to output that text the same width and height as the text and also the window's width and height as Label1's.

Difference Between Image1.Picture.Width And Image1.width

I was wondering if someone could explain to me what the difference is
between the width propery of the picture property of an image and the width
property of the image itself.

I would have guessed that when the stretch property of an image was set to
false and an image was assigned to the picture property, that
Image1.Picture.Width and Image1.width would be the same. This is not the

I need to know with what factor an image is stretched when stretched = true,
and I thought I could use these two different width properties, thinking
that Image1.width is the stretched width and Image1.Picture.width is the
actual width of the image.

Can someone help me figure this out?

Thanks in advance,

Copyright © 2005-08, All rights reserved