I don't believe this code gives the correct RichTextBox string size

D

**Developer**

I don't believe the code below gives the correct string size in inches.

Can any one tell me what is wrong?

I need to know the size of the string before I add it to the RichTextBox





Thanks





Public ReadOnly Property TxtMeasureString(ByVal str As String) As SizeF

Get

Dim Gr As Graphics = Me.CreateGraphics

Dim MeasureString As SizeF = Gr.MeasureString(str,
ControlRichTextBox1.SelectionFont())

TxtMeasureString = New SizeF(MeasureString.Width / Gr.DpiX,
MeasureString.Height / Gr.DpiY)

Gr.Dispose()

End Get

End Property
 
H

Herfried K. Wagner [MVP]

**Developer** said:
I don't believe the code below gives the correct string size in inches.

Can any one tell me what is wrong?

I need to know the size of the string before I add it to the RichTextBox

I think it's not as easy. The richtextbox control is drawn using GDI, while
'Graphics.MeasureString' measuers the string using GDI+. Consequently
'MeasureString' won't return the exact size of the string. I fear you will
have to dig into the GDI32-API to get exact results.
 
D

**Developer**

Thanks, I'll go searching

\
Herfried K. Wagner said:
I think it's not as easy. The richtextbox control is drawn using GDI,
while 'Graphics.MeasureString' measuers the string using GDI+.
Consequently 'MeasureString' won't return the exact size of the string. I
fear you will have to dig into the GDI32-API to get exact results.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top