Click here to Skip to main content
15,905,323 members
Home / Discussions / Visual Basic
   

Visual Basic

 
GeneralRe: Volume control for vb6/vb.net Pin
Digital4049-Feb-08 18:40
Digital4049-Feb-08 18:40 
GeneralRe: Volume control for vb6/vb.net Pin
Digital40410-Feb-08 16:00
Digital40410-Feb-08 16:00 
Generalprinting strings Pin
Smithers-Jones6-Feb-08 5:59
Smithers-Jones6-Feb-08 5:59 
AnswerRe: printing strings Pin
Scubapro6-Feb-08 8:06
Scubapro6-Feb-08 8:06 
GeneralRe: printing strings Pin
Smithers-Jones6-Feb-08 9:01
Smithers-Jones6-Feb-08 9:01 
GeneralRe: printing strings Pin
Richard Blythe6-Feb-08 11:54
Richard Blythe6-Feb-08 11:54 
GeneralRe: printing strings Pin
Smithers-Jones6-Feb-08 23:36
Smithers-Jones6-Feb-08 23:36 
GeneralRe: printing strings [modified] Pin
Richard Blythe7-Feb-08 9:45
Richard Blythe7-Feb-08 9:45 
I see what your getting at now. I did some digging and found that an overload on MeasureString() will output the characters fitted, which should be just what you need!
I've create an example on how to use this feature. Originally I was going to write all of the code in a button handler but it got a little messy. I ended up writing a light wieght class called "StringGenerator" that you can customize. To try this sample, perform the following steps:

1. Create a new class called "StringGenerator" and insert the code below marked: // <<<< StringGenerator Code >>>>>>
1. create a blank form and set it to be the startup form
2. add a button with it's position set to: x=5,y=5
3. double click on the button to generate an event handler.
4. Insert the code snippet below:

'begin code snippet
Dim g As Graphics = Me.CreateGraphics()
Dim sf As StringFormat = New StringFormat()
Dim intY As Integer = 20
Dim originalText As String = "This is very boring text but I don't look at what the text says, I just look at what results the text will bring."

Dim stringG As New MyClasses.StringGenerator( _
originalText, g, Me.Font, New RectangleF(80, intY, 60, 50), sf)
stringG.DrawCurrentString()

While (stringG.HasMoreCharsToDraw)
intY += 60 'in this sample, 60 creates a 10 px padding
stringG.SetNewBounds(New RectangleF(80, intY, 60, 50))
stringG.DrawCurrentString()
End While
'cleanup
g.Dispose()
sf.Dispose()

'End code snippet


Note:This code is still very primitive but it will definitely get you started. If you step through the code, you won't see any output. Remove all the breakpoints to see the dynamically generated graphics. Let me know how it turns out!




' <<<< StringGenerator Code >>>>>>
'insert into -> EMPTY <- class file!

Namespace MyClasses

Public Class StringGenerator
Dim g As Graphics
Dim rectF As RectangleF
Dim sf As StringFormat
Dim fnt As Font
Dim chars() As Char
Dim startIndex As Int32
Dim charsFitted As Int32
Dim linesFilled As Int32
Dim strDisplayText As String
Dim strCurText As String
Dim blnHasMoreChars As Boolean
Public blnClipToNearestWord As Boolean = True

Public Sub New(ByVal text As String, ByRef graphics As Graphics, ByRef font As Font, ByRef rectStartingBounds As RectangleF, ByRef stringFormat As StringFormat)
chars = text.ToCharArray()
rectF = rectStartingBounds
strCurText = text
g = graphics
fnt = font
'set blnHasMoreChars to an initial value of true;
blnHasMoreChars = True
End Sub

Public Sub DrawCurrentString()
MeasureMyCharacters()

'you can write your own custom code here, such as
'allowing a different color input. Don't allow a
'different font because it will mess you measurements up!
g.DrawRectangle(Pens.Red, Rectangle.Round(rectF))
g.DrawString(strDisplayText, fnt, Brushes.Black, rectF, sf)
End Sub

Public Sub SetNewBounds(ByVal rectFBounds As RectangleF)
rectF = rectFBounds
End Sub

Private Sub MeasureMyCharacters()
'This method takes 'strCurText' and measures it with the
'bounds supplied. MeasureString() returns a SizeF object
'but we are only interested in the output generated by the
' 'charsFitted' variable.
g.MeasureString(strCurText, fnt, rectF.Size, sf, charsFitted, linesFilled)

'If amount of chars fitted in rectF is less than
'the number of characters remaining to fit...
If (charsFitted < strCurText.Length - 1) Then
'do we want to clip to the nearest word?
If (blnClipToNearestWord) Then
'we will now loop backwards to find the first
'occurrence of a character separation: " "
For i As Integer = charsFitted + startIndex To 0 Step -1
If chars(i) = " " Then 'is the char a space?
'reduce the amount of characters fitted
'to the first word occurrence
charsFitted = (i - startIndex) + 1
i = -1 'terminate the loop
End If
Next
End If
blnHasMoreChars = True
Else
blnHasMoreChars = False
End If
'create the display contents
strDisplayText = New String(chars, startIndex, (charsFitted))
startIndex = (charsFitted + startIndex)
'assign strCurText the chars remaining to print
strCurText = New String(chars, startIndex, (chars.Length - startIndex))
End Sub

Public Function HasMoreCharsToDraw() As Boolean
HasMoreCharsToDraw = blnHasMoreChars
End Function

End Class
End Namespace

may your code be error free.
(okay, maybe two lines.)

modified on Thursday, February 7, 2008 5:20 PM

GeneralRe: printing strings Pin
Smithers-Jones13-Feb-08 9:36
Smithers-Jones13-Feb-08 9:36 
GeneralRe: printing strings Pin
Richard Blythe13-Feb-08 12:05
Richard Blythe13-Feb-08 12:05 
GeneralEncrypting files Pin
duo!@#6-Feb-08 4:05
duo!@#6-Feb-08 4:05 
GeneralRe: Encrypting files Pin
Justin Perez6-Feb-08 4:17
Justin Perez6-Feb-08 4:17 
GeneralRe: Encrypting files Pin
duo!@#6-Feb-08 4:49
duo!@#6-Feb-08 4:49 
GeneralRe: Encrypting files PinPopular
Justin Perez6-Feb-08 5:43
Justin Perez6-Feb-08 5:43 
GeneralRe: Encrypting files Pin
duo!@#6-Feb-08 20:29
duo!@#6-Feb-08 20:29 
GeneralRe: Encrypting files Pin
Guffa6-Feb-08 21:03
Guffa6-Feb-08 21:03 
GeneralRe: Encrypting files Pin
Christian Graus6-Feb-08 22:17
protectorChristian Graus6-Feb-08 22:17 
GeneralRe: Encrypting files Pin
Justin Perez7-Feb-08 2:42
Justin Perez7-Feb-08 2:42 
GeneralRe: Encrypting files Pin
Ri Qen-Sin7-Feb-08 7:06
Ri Qen-Sin7-Feb-08 7:06 
GeneralRe: Encrypting files Pin
Joe6-Feb-08 22:02
Joe6-Feb-08 22:02 
GeneralRe: Encrypting files Pin
Vasudevan Deepak Kumar6-Feb-08 22:05
Vasudevan Deepak Kumar6-Feb-08 22:05 
GeneralRe: Encrypting files Pin
NormDroid6-Feb-08 22:37
professionalNormDroid6-Feb-08 22:37 
GeneralURGENT !!! Pin
Christian Graus6-Feb-08 22:47
protectorChristian Graus6-Feb-08 22:47 
GeneralRe: URGENT !!! Pin
benjymous6-Feb-08 22:51
benjymous6-Feb-08 22:51 
GeneralRe: URGENT !!! Pin
Christian Graus6-Feb-08 23:19
protectorChristian Graus6-Feb-08 23:19 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.