-
Nov 16th, 2020, 09:16 AM
#1
Thread Starter
PowerPoster
[RESOLVED] Measure the length of a string in pixels.
Hi,
I have a problem measuring the length of a string in pixels.
Currently I'm using this code:
Code:
Private Function MeasureText(ByVal txt As String) As Integer
Dim wdth As Integer
Using gr As Graphics = TextBox1.CreateGraphics
wdth = gr.MeasureString(txt, TextBox1.Font).Width
End Using
Return wdth
End Function 'Measure length of 'txt' in pixels.
This is fine up to a point, sadly, if there's a vbTab in the measured string it will only count as one character and I get a false measurement.
I can't find way around this. I considered adding the length of a vbTab, (measure the difference between two strings, one with one vbTab and the other with two) but of course when a vbTab is used the new length of string is only up to the next tab position.
Poppa
Along with the sunshine there has to be a little rain sometime.
-
Nov 16th, 2020, 11:00 AM
#2
Re: Measure the length of a string in pixels.
Try replacing a tab with four spaces
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Nov 16th, 2020, 11:17 AM
#3
Thread Starter
PowerPoster
Re: Measure the length of a string in pixels.
Originally Posted by .paul.
Try replacing a tab with four spaces
Sorry, this is my fault, I should've explained what I'm trying to do.
I'm reading in a whole load of data strings consisting of four separate sections per string, these sections can be of any length, but I want to display and print 'em to a text file in four distinct columns.
Poppa
Along with the sunshine there has to be a little rain sometime.
-
Nov 16th, 2020, 11:25 AM
#4
Re: Measure the length of a string in pixels.
Originally Posted by Poppa Mintin
I want to display and print 'em to a text file in four distinct columns.
Text files don't support columns, well not with good results. If you wanted to put them in a ListBox, I can show you columns. As for the textfile, you'd be better off with csv
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Nov 16th, 2020, 11:27 AM
#5
Re: Measure the length of a string in pixels.
Here's a ListBox with three columns...
Code:
Imports System.Runtime.InteropServices
Public Class Form1
<DllImport("user32.dll")> _
Private Shared Function SendMessage( _
ByVal hWnd As IntPtr, _
ByVal wMsg As Integer, _
ByVal wParam As IntPtr, _
ByVal lParam As IntPtr) As Integer
End Function
Private Sub SetTabStops(ByVal listBox As ListBox, ByVal tabStops() As Integer)
Const LB_SETTABSTOPS As Integer = &H192
Dim pinnedValues As GCHandle
pinnedValues = GCHandle.Alloc(tabStops, GCHandleType.Pinned)
Dim ptr As IntPtr = pinnedValues.AddrOfPinnedObject()
SendMessage(listBox.Handle, LB_SETTABSTOPS, New IntPtr(tabStops.Length), ptr)
pinnedValues.Free()
listBox.Refresh()
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
ListBox1.Items.Add("Product" & vbTab & "Description" & vbTab & "Price")
ListBox1.Items.Add("BLT" & vbTab & "Sandwich with bacon, lettece and tomatoes" & vbTab & "7")
ListBox1.Items.Add("PZVES" & vbTab & "Pizza with tomatosauce, cheese and ham" & vbTab & "12")
ListBox1.Items.Add("BRGCH" & vbTab & "Cheese Burger" & vbTab & "9")
ListBox2.Items.AddRange(ListBox1.Items)
SetTabStops(ListBox2, New Integer() {70, 240})
End Sub
End Class
EDIT: Actually, it's two ListBoxes, to compare the differently displayed items
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Nov 16th, 2020, 11:45 AM
#6
Re: Measure the length of a string in pixels.
You can also do columns in a TextBox, with String.Format - Control spacing
https://docs.microsoft.com/en-us/dot...t?view=net-5.0
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Nov 16th, 2020, 12:02 PM
#7
Thread Starter
PowerPoster
Re: Measure the length of a string in pixels.
Originally Posted by .paul.
Text files don't support columns, well not with good results. If you wanted to put them in a ListBox, I can show you columns. As for the textfile, you'd be better off with csv
Thanks .Paul,
I know text files don't support columns, that's the problem, but I'm getting there, the first two columns are fine, and the fourth would be too if I could manage the sort out the third column. Just struggling with the tab.
I measure the width of each first element, then store those elements in a list of string each padded out with spaces to be the same length as each other, then add a tab so that the next element will be vertically aligned.
So far, I'm breaking down each element of a data string into it's elements as strings and trying to do as described above to each. Then I try to concatenate 'em all into one string to display and print.
Still struggling a bit, I was (am) hoping someone can suggest a way to measure a string that has a tab in it.
Poppa
Along with the sunshine there has to be a little rain sometime.
-
Nov 16th, 2020, 12:52 PM
#8
Re: Measure the length of a string in pixels.
reading the whole thread twice, i still dont get it.
do you want to print (to a printer) or do you want to display it on screen or do you want to write it to a text file?
you mentioned all these.
printing would prob. be the only reason why you should be interested in pixel width.
all others, you should prob. do with padding with spaces.
for diplay in controls you might want to try a fixedsize font like "lucida console"
-
Nov 16th, 2020, 07:34 PM
#9
Thread Starter
PowerPoster
Re: Measure the length of a string in pixels.
Originally Posted by digitalShaman
reading the whole thread twice, i still dont get it.
do you want to print (to a printer) or do you want to display it on screen or do you want to write it to a text file?
you mentioned all these.
printing would prob. be the only reason why you should be interested in pixel width.
all others, you should prob. do with padding with spaces.
for diplay in controls you might want to try a fixedsize font like "lucida console"
Hi Shaman, I want to be able to do all of the above.
I really don't like unisize fonts, although I know they can make some things easier.
In the utility that I'm writing I use the same font in my TextBox that I use in Notepad, displaying the text on screen allows me to see the progress and to check that everything is OK before I save it to a text file.
After the the text has been displayed, I have the option of producing a text file or not, choosing to save the file also displays the new text file on screen in Notepad and closes the app.
Poppa
PS, I have a solution.
Pop
Along with the sunshine there has to be a little rain sometime.
-
Nov 16th, 2020, 08:05 PM
#10
Thread Starter
PowerPoster
Re: Measure the length of a string in pixels.
Thanks for your help guys,
I have a working solution which produces a neat text file an example of which I've just deleted from this post because when it displays in here (in a Quote) all the formatting is removed
Taking a copy and paste into my Notepad (Comic Sans MS Regular at 14 point), the formatting is still removed.
So... for anyone who may be interested there is a sample on OneDrive: Here
To see it properly it should be viewed in the font above.
Poppa
Along with the sunshine there has to be a little rain sometime.
-
Nov 17th, 2020, 12:04 PM
#11
Re: [RESOLVED] Measure the length of a string in pixels.
Get the width of a vbTab... then multiply it by the number of times vbTab appears int the string.
-tg
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|