By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,258 Members | 1,218 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,258 IT Pros & Developers. It's quick & easy.

Text drawing problems

P: n/a
Hi

I am trying to draw text in a panel. I want the width of the panel to
be fixed but the height to be determined by the number of lines the
text needs to cover in the panel. I also want to be able to specify
the linespacing for the text.

I have looked at the StringFormat class, MSDN stating:

"Encapsulates text layout information (such as alignment and line
spacing)..."

However there appears to be no property for setting the Line Spacing
of the text, and I cannot work out how to wrap it...

Private Sub DrawText(ByVal e As PaintEventArgs)

Dim Text As String
Dim I As Integer
For I = 1 To 20
Text &= "Sample Text"
If I < 20 Then Text &= " "
Next

Dim TextSize As PointF
Dim StringFormat As New StringFormat

e.Graphics.DrawString(Text, Font, New SolidBrush(Color.Black),
0, 0, StringFormat.GenericTypographic)

End Sub

Any help would be appreciated!

Cheers

Blu.
Nov 20 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Hi,

Private Sub Panel1_Paint(ByVal sender As Object, ByVal e As
System.Windows.Forms.PaintEventArgs) Handles Panel1.Paint

Dim strOut As String = "This is my really really long line of
text to print on the screen"
Dim sf As New StringFormat

e.Graphics.DrawString(strOut, Me.Font, Brushes.Black, _
RectangleF.op_Implicit(Panel1.ClientRectangle), sf)

End Sub

Ken
-----------------
"BluDog" <ne**@nospam.bludog.net> wrote in message
news:0g********************************@4ax.com:
Hi

I am trying to draw text in a panel. I want the width of the panel to
be fixed but the height to be determined by the number of lines the
text needs to cover in the panel. I also want to be able to specify
the linespacing for the text.

I have looked at the StringFormat class, MSDN stating:

"Encapsulates text layout information (such as alignment and line
spacing)..."

However there appears to be no property for setting the Line Spacing
of the text, and I cannot work out how to wrap it...

Private Sub DrawText(ByVal e As PaintEventArgs)

Dim Text As String
Dim I As Integer
For I = 1 To 20
Text &= "Sample Text"
If I < 20 Then Text &= " "
Next

Dim TextSize As PointF
Dim StringFormat As New StringFormat

e.Graphics.DrawString(Text, Font, New SolidBrush(Color.Black),
0, 0, StringFormat.GenericTypographic)

End Sub

Any help would be appreciated!

Cheers

Blu.


--
Outgoing mail is certified Virus Free.
Checked by AVG Anti-Virus (http://www.grisoft.com).
Version: 7.0.230 / Virus Database: 263.3.0 - Release Date: 6/12/2004
Nov 20 '05 #2

P: n/a
"Encapsulates text layout information (such as alignment and line
spacing)..."

However there appears to be no property for setting the Line Spacing
of the text, and I cannot work out how to wrap it...
Private Sub Panel1_Paint(ByVal sender As Object, ByVal e As
System.Windows.Forms.PaintEventArgs) Handles Panel1.Paint

Dim strOut As String = "This is my really really long line of
text to print on the screen"
Dim sf As New StringFormat

e.Graphics.DrawString(strOut, Me.Font, Brushes.Black, _
RectangleF.op_Implicit(Panel1.ClientRectangle), sf)

End Sub


Ken, thanks for this, it is pretty much where I am, however it is not
a solution to the line spacing problem.

Cheers

Tom
Nov 20 '05 #3

P: n/a
u need this:
dim g as graphics
dim path as ne graphicpath
path.AddString()
set world transform
dim rectBound as rectanglaF= path.GetBound()
dim aptDist() as PointF= new point(0,0), .....}
g.transform= new Matrix(rectbound,aptDist))
then fill path
g.Fillapth(new solidbrush(colour,path)
or u can do path.AddString.

u can get help from msdn from microsoft's website.
regards

BluDog wrote:
Hi

I am trying to draw text in a panel. I want the width of the panel to
be fixed but the height to be determined by the number of lines the
text needs to cover in the panel. I also want to be able to specify
the linespacing for the text.

I have looked at the StringFormat class, MSDN stating:

"Encapsulates text layout information (such as alignment and line
spacing)..."

However there appears to be no property for setting the Line Spacing
of the text, and I cannot work out how to wrap it...

Private Sub DrawText(ByVal e As PaintEventArgs)

Dim Text As String
Dim I As Integer
For I = 1 To 20
Text &= "Sample Text"
If I < 20 Then Text &= " "
Next

Dim TextSize As PointF
Dim StringFormat As New StringFormat

e.Graphics.DrawString(Text, Font, New SolidBrush(Color.Black),
0, 0, StringFormat.GenericTypographic)

End Sub

Any help would be appreciated!

Cheers

Blu.


Nov 20 '05 #4

P: n/a
BluDog wrote:
However there appears to be no property for setting the Line Spacing
of the text, and I cannot work out how to wrap it... e.Graphics.DrawString(Text, Font, New SolidBrush(Color.Black),
0, 0, StringFormat.GenericTypographic)


dim ls as integer = LineHeight ' define the value as you wish

dim Lines() as string = split(Text,vbcrlf)

dim Y as integer = StartTop ' define the value as you wish

for i as integer = 0 to ubound(Lines)

e.Graphics.DrawString(Lines(i), Font, New SolidBrush(Color.Black),
0,Y, StringFormat.GenericTypographic)

y+=LineHeight

next

Nov 20 '05 #5

P: n/a
Hilbert

Thanks for your suggestion, the problem is there are no Line Feeds in
the string necessarily, therefore you cannot split it up in that
way... however i have tweeked it to find sensible line breaks and wrap
it manually:

Private LineHeight As Integer = 21
Private Format As StringFormat = StringFormat.GenericTypographic
Private SampleFont As Font = Font

Private Sub DrawText(SampleText as string, e As PaintEventArgs)

'Get the size of the panel the text is to be printed into
Dim PanelSize As New SizeF(SamplePanel.Width,
SamplePanel.Height)

'Split the text into an array of string
Dim Words() As String = SampleText.Split(" ")

'Loop through lines
Dim LineCount As Integer
Dim Line, TestLine As String
For Word As Integer = 0 To Words.Length - 1
TestLine = (Line & " " & Words(Word)).Trim
If e.Graphics.MeasureString(TestLine, SampleFont, New
PointF(3, 0), Format).Width > PanelSize.Width Then
e.Graphics.DrawString(Line, SampleFont, Brushes.Black,
3, LineCount * LineHeight, Format)
Line = ""
LineCount += 1
Word -= 1
ElseIf Word = Words.Length - 1 Then
e.Graphics.DrawString(TestLine, SampleFont,
Brushes.Black, 3, LineCount * LineHeight, Format)
LineCount += 1
Else
Line = TestLine
End If
Next

End Sub
Cheers

Blu
Nov 20 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.