Results 1 to 4 of 4

Thread: "Erasing" a "randomly" colored line in VB?

  1. #1

    Thread Starter
    New Member
    Join Date
    Jun 2012
    Posts
    9

    "Erasing" a "randomly" colored line in VB?

    Hello all,

    I'm looking for a way of "erasing" a randomly colored line in vb.net. I don't actually think that there is a way to destroy the line, rather, I'm looking for a way to change the line color to the same color as the background, effectively "erasing" it. Also, I'm looking for a way to randomly generate the line colors, then change them to the background of the form as previously stated. The code for that is commented out.

    It's a pretty simple program in general. I have it randomly creating the lines (every 4 seconds) in a single color, just not randomly generating line color and changing them to the background color to be "erased"

    The buttons just speeds/slows the model.

    lines Code:
    1. Public Class Form1
    2.  
    3.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    4.         Me.BackColor = Color.Aquamarine
    5.     End Sub
    6.  
    7.     Private Sub btnFaster_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFaster.Click
    8.         tmrTimer.Interval = tmrTimer.Interval - 10
    9.     End Sub
    10.  
    11.     Private Sub btnSlower_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSlower.Click
    12.         tmrTimer.Interval = tmrTimer.Interval + 10
    13.     End Sub
    14.  
    15.     Private Sub tmrTimer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmrTimer.Tick
    16.         'Dim inicolor As New Random()
    17.         'Dim intR, intG, intB As Integer
    18.         'intR = inicolor.Next(0, 256) 'generates random rgb number
    19.         'intG = inicolor.Next(0, 256)
    20.         'intB = inicolor.Next(0, 256)
    21.         'Dim hexR, hexG, hexB As String
    22.         'hexR = intR.ToString("X").PadLeft(2, "0"c) 'converts it to hex
    23.         'hexG = intG.ToString("X").PadLeft(2, "0"c)
    24.         'hexB = intB.ToString("X").PadLeft(2, "0"c)
    25.         'Dim strColor As String
    26.         'strColor = "#" & hexR & hexG & hexB
    27.         'Dim var = Drawing.Color.FromArgb(intR, intG, intB)
    28.         'Me.BackColor = var
    29.         Dim formSurface As Graphics = Me.CreateGraphics 'creates surface
    30.         Dim line As Integer = (Int((5 - 1 + 1) * Rnd() + 1))
    31.         Dim pen As New Pen(Color.Green, line)
    32.         Dim p1horizontal As Integer = (Int((300 - 0 + 1) * Rnd() + 0)) 'generates 4 random line coordinates
    33.         Dim p1vertical As Integer = (Int((300 - 0 + 1) * Rnd() + 0))
    34.         Dim p2horizontal As Integer = (Int((300 - 0 + 1) * Rnd() + 0))
    35.         Dim p2vertical As Integer = (Int((300 - 0 + 1) * Rnd() + 0))
    36.         formSurface.DrawLine(pen, p1horizontal, p1vertical, p2horizontal, p2vertical)
    37.         Media.SystemSounds.Beep.Play()
    38.         formSurface.DrawLine(pen, p1horizontal, p1vertical, p2horizontal, p2vertical)
    39.  
    40.  
    41.     End Sub
    42. End Class

    Thanks!

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: "Erasing" a "randomly" colored line in VB?

    You're going about this in very much the wrong way. You basically never call CreateGraphics. Anything you draw using GDI+ you draw on the Paint event of the control. Every time the control is repainted, which happens when the form is minimised and restored, has another form dragged over it and on many other occasions, all the drawing gets erased. That's why you have to draw it again.

    To learn how to handle this sort of thing properly, follow the CodeBank link in my signature and check out my Simple Drawing thread. You just have to use the appropriate member variables to store the data that describes what you want to draw and add logic to the Paint event handler to turn that data into a drawing. If you want to change what is drawn, e.g. erase it, you simply modify the data ass appropriate and then force a Paint event to be raised.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    New Member
    Join Date
    Jun 2012
    Posts
    9

    Re: "Erasing" a "randomly" colored line in VB?

    I'm sorry, I can only use these specifications with creategraphics... it's dumb, i realize now but it's for school and I've gotta do what they tell me to do.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: "Erasing" a "randomly" colored line in VB?

    Like I said, any GDI+ drawing gets erased every time the control you draw on gets repainted. If you want to erase your line you simply call Refresh on the control you drew on.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

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
  •  



Click Here to Expand Forum to Full Width