PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197
VS 2010 [RESOLVED] Drawing Multiple Strings?-VBForums
Results 1 to 4 of 4

Thread: [RESOLVED] Drawing Multiple Strings?

  1. #1

    Thread Starter
    Addicted Member NinjaNic's Avatar
    Join Date
    Dec 2013
    Location
    Earth
    Posts
    230

    Resolved [RESOLVED] Drawing Multiple Strings?

    Hi again!
    Why is it that when I try to draw multiple strings in a list, it only draws one? How can I make it so that it draws all the strings at once? There must be a problem with the For Each loop. Thanks for the help in advance!

    vb Code:
    1. Public Class Form1
    2.  
    3.     'EACH RANDOMTEXT HAS 3 PROPERTIES: TEXT, LOCATIONX, AND LOCATIONY.
    4.     'I AM TRYING TO CREATE AN EFFECT TO SCATTER TEXT TO SLOWLY COVER THE SCREEN.
    5.  
    6.     Dim F As New Font("Arial", 16, FontStyle.Bold)
    7.     Dim RandomTexts As New List(Of RandomText)
    8.  
    9.     Private Sub Form1_Paint(sender As Object, e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
    10.         For Each RandomText In RandomTexts
    11.             e.Graphics.DrawString(RandomText.Text, F, Brushes.Black, New Point(RandomText.LocationX, RandomText.LocationY))
    12.         Next
    13.     End Sub
    14.  
    15.     Private Sub Timer2_Tick(sender As System.Object, e As System.EventArgs) Handles Timer2.Tick
    16.         RandomTexts.Add(New RandomText("", New Point(0, 0)))
    17.         'MsgBox(RandomTexts.Count)
    18.     End Sub
    19.  
    20.     Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles Timer1.Tick
    21.         NewText()
    22.         Me.Invalidate()
    23.     End Sub
    24.  
    25.     Private Sub NewText()
    26.         Dim R As New Random
    27.         Dim Alphabet As String = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890"
    28.         Dim N As Int32 = CInt(R.Next(3, 15))
    29.         Dim A As Int32 = CInt(R.Next(Alphabet.Length))
    30.         For Each RandomText In RandomTexts
    31.             RandomText.Text = ""
    32.             RandomText.LocationX = R.Next(-10, 390)
    33.             RandomText.LocationY = R.Next(-10, 390)
    34.             Do Until N <= 0
    35.                 N -= 1
    36.                 RandomText.Text += Alphabet(A)
    37.                 A = CInt(R.Next(Alphabet.Length))
    38.             Loop
    39.         Next
    40.  
    41.     End Sub
    42.  
    43. End Class

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    22,597

    Re: Drawing Multiple Strings?

    try this modification:

    Code:
    Private Sub NewText()
        'also for maximum randomizing move the Random declaration to form scope
        Dim R As New Random
        Dim Alphabet As String = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890"
        Dim N As Int32 = CInt(R.Next(3, 15))
        Dim A As Int32 = CInt(R.Next(Alphabet.Length))
        For Each RandomText In RandomTexts
            If RandomText.Text = "" Then
                RandomText.LocationX = R.Next(-10, 390)
                RandomText.LocationY = R.Next(-10, 390)
                Do Until N <= 0
                    N -= 1
                    RandomText.Text += Alphabet(A)
                    A = CInt(R.Next(Alphabet.Length))
                Loop
            End If
        Next
    End Sub

  3. #3

    Thread Starter
    Addicted Member NinjaNic's Avatar
    Join Date
    Dec 2013
    Location
    Earth
    Posts
    230

    Re: Drawing Multiple Strings?

    Thank you for the help!

  4. #4
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    22,597

    Re: [RESOLVED] Drawing Multiple Strings?

    one other tip...
    no need for CInt(). R.Next returns an integer

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width