Results 1 to 12 of 12

Thread: [RESOLVED] Why does my screen blank ?

  1. #1

    Thread Starter
    Frenzied Member Poppa Mintin's Avatar
    Join Date
    Mar 2009
    Location
    Skunthorpe, North Lincolnshire, England.
    Posts
    1,912

    Resolved [RESOLVED] Why does my screen blank ?

    Hi,

    I have a tool which I programmed several years ago to produce a text file of coordinates to draw a regular polygon. The tool draws these Points around the horizontal centre of the form in order that the largest diameter can be achieved and outputs these Points to a text file.

    I am using this tool today as part of a special tool for a specific job, I call the polygon tool, read-in the (x,y) Points and then draw a line from the same origin Point on the screen to each Point given in the text file to form a 'star'.
    The difference is that in the new tool, the form's background colour and the pen colour are to be chosen before drawing.
    I had a bit of trouble detecting a miss-spelt or unknown colour, just converting the String to a Color doesn't automatically cause an error, it just gives 'Black', but found a way around that... I think !

    I run the tool, the 'star' is drawn, so far, so good, then the screen immediately goes blank... I can't find why.
    I have also found that the blank form, (blank except for the 'Exit' button, all other controls are 'Hidden') is not the colour given in the design page (Lavender) but the form's default colour 'Control'.

    Code:
        Private Sub MakeDrawing()
            Dim back, ink As Color, pen1 As Pen
            Dim gr As Graphics = CreateGraphics()
            Dim memo As Color = Me.BackColor
            Try
                ink = Color.FromName(TextBox2.Text)
                Me.BackColor = ink                     ' This is to detect colour error.
                back = Color.FromName(TextBox1.Text)
                Me.BackColor = back                    ' This is to detect colour error.
            Catch ex As Exception
                Me.BackColor = memo
                StartUp()
                Label3.Show()                          ' Tell the user that there's a colour error.
                Exit Sub
            End Try
            Me.BackColor = Nothing                     ' This is required else no drawing shown.
            gr.Clear(back)
            pen1 = New Pen(ink, 1)
            For i = 0 To cords.Count - 1
                gr.DrawLine(pen1, centre, cords(i))
            Next
        End Sub
    I must be doing something wrong, or not doing something I ought to be doing... But what ?


    Poppa


    PS.
    Typical text file:
    Radius : 200
    Centre : 432

    1: 561, 585 Green start
    2: 629, 467
    3: 605, 332
    4: 500, 244
    5: 364, 244
    6: 259, 332
    7: 235, 467
    8: 303, 585
    9: 432, 632 Red finish
    Pop.
    Last edited by Poppa Mintin; Sep 24th, 2020 at 11:00 AM.
    Along with the sunshine there has to be a little rain sometime.

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    105,568

    Re: Why does my screen blank ?

    NEVER call CreateGraphics. Draw on the Paint event of the relevant control.

  3. #3
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    105,568

    Re: Why does my screen blank ?

    Quote Originally Posted by Poppa Mintin View Post
    I had a bit of trouble detecting a miss-spelt or unknown colour, just converting the String to a Color doesn't automatically cause an error, it just gives 'Black'
    vb.net Code:
    1. Dim names = {"Red", "Rid"}
    2.  
    3. For Each s In names
    4.     If [Enum].IsDefined(GetType(KnownColor), s) Then
    5.         Console.WriteLine($"There is a known colour named '{s}'.")
    6.     Else
    7.         Console.WriteLine($"There is NOT a known colour named '{s}'.")
    8.     End If
    9. Next

  4. #4

    Thread Starter
    Frenzied Member Poppa Mintin's Avatar
    Join Date
    Mar 2009
    Location
    Skunthorpe, North Lincolnshire, England.
    Posts
    1,912

    Re: Why does my screen blank ?

    Thanks John,

    I've not addressed the colour problem yet, that's working for now.

    I've amended my method to what I think you mean. There is an improvement, the back colour remains as I set it, and the screen no longer blanks out, that's successful.

    The lines though are not so good. using a high number (360) of long lines, and watching very carefully, I can see that all the lines are drawn, and with only six lines, stepping through one at a time I can see the instructions being issued.
    Trouble is... When complete only the last line to be drawn remains visible.
    Code:
    '   Global variables.
            Dim back, ink As Color
            Dim centre, spot As Point
            Dim cords As New List(Of Point)
            Dim ok As Boolean
            Dim pen1 As Pen
    '   ________________________
    
       
        Private Sub MakeDrawing()
            Dim back, ink As Color
            Dim memo As Color = Me.BackColor
            Try
                ink = Color.FromName(TextBox2.Text)
                Me.BackColor = ink                     ' This is to detect colour error.
                back = Color.FromName(TextBox1.Text)
                Me.BackColor = back
            Catch ex As Exception
                Me.BackColor = memo
                StartUp()
                Label3.Show()                          ' Inform user there's a problem.
                Exit Sub
            End Try
            pen1 = New Pen(ink, 1)
            For i = 0 To cords.Count - 1
                spot = cords(i)
                ok = True
                Me.Refresh()
            Next
        End Sub
    
        Private Sub Me_Paint(sender As System.Object, e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
            If Not ok Then Exit Sub
            ok = False
            e.Graphics.DrawLine(pen1, centre, spot)
        End Sub

    Poppa
    Along with the sunshine there has to be a little rain sometime.

  5. #5
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    23,483

    Re: Why does my screen blank ?

    You're only passing one point at a time...

    Code:
    Private Sub MakeDrawing()
        Dim back, ink As Color
        Dim memo As Color = Me.BackColor
        Try
            ink = Color.FromName(TextBox2.Text)
            Me.BackColor = ink                     ' This is to detect colour error.
            back = Color.FromName(TextBox1.Text)
            Me.BackColor = back
        Catch ex As Exception
            Me.BackColor = memo
            StartUp()
            Label3.Show()                          ' Inform user there's a problem.
            Exit Sub
        End Try
        pen1 = New Pen(ink, 1)
        For i = 0 To cords.Count - 1
            spot = cords(i)
            ok = True
            Me.Refresh()
        Next
    End Sub
    
    Private Sub Me_Paint(sender As System.Object, e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
        'this is what happens
        'clear drawing area
        If Not ok Then Exit Sub
        ok = False
        'draw one line
        e.Graphics.DrawLine(pen1, centre, spot)
    End Sub

  6. #6
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    23,483

    Re: Why does my screen blank ?

    Why not use [graphics].DrawLines, and pass in the whole cords array?

  7. #7

    Thread Starter
    Frenzied Member Poppa Mintin's Avatar
    Join Date
    Mar 2009
    Location
    Skunthorpe, North Lincolnshire, England.
    Posts
    1,912

    Re: Why does my screen blank ?

    Quote Originally Posted by .paul. View Post
    Why not use [graphics].DrawLines, and pass in the whole cords array?
    Thanks .Paul, isn't that what John was so forceful in saying don't use it ?

    I've tried DrawLines but can't find how to do that without using CreateGraphics.

    Even so, the lines are being drawn, why are they not visible ? Surely drawing one line doesn't erase previously drawn lines.


    Poppa
    Along with the sunshine there has to be a little rain sometime.

  8. #8
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    23,483

    Re: Why does my screen blank ?

    Not CreateGraphics. In the Paint Event you’re using DrawLine. There is an alternative DrawLines, which draws multiple lines...

    They’re not visible because all you’re seeing is the last line. Every time you Refresh, the Paint Event is called, which erases all drawing on the surface (in this case your Form), then any drawing specified in the event is drawn

  9. #9
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    105,568

    Re: Why does my screen blank ?

    Quote Originally Posted by Poppa Mintin View Post
    I've tried DrawLines but can't find how to do that without using CreateGraphics.
    What's that got to do with it? CreateGraphics creates a Graphics object. The Paint event handler provides a Graphics object. Why can you do something with one of those but you can't do the same with the other?
    Quote Originally Posted by Poppa Mintin View Post
    the lines are being drawn, why are they not visible ? Surely drawing one line doesn't erase previously drawn lines.
    No, drawing a line doesn't erase previously drawn lines. Calling Refresh does though. When the Paint event is raised, the area of the control being repainted that was invalidated gets erased. When you call Refresh, you are invalidating the entire control, so everything you previously drew on the control gets erased.

    GDI+ drawing is easy but you need to understand the basic principles involved. The Paint event is raised often so it is never safe to draw on a control anywhere else. You need to declare one or more fields to store the data that represents your entire drawing. In the Paint event handler, you read that data and you use the Graphics object provided to draw what it represents. Any time the drawing needs to change, you modify the data stored in that field(s) and then call Invalidate. That will queue a Paint event so your drawing will be updated.

    It is preferable to call Invalidate with an argument that represents only the area of the control that has or may have changed. That will ensure that the minimum number of pixels get repainted. It is that repainting that is the slow part of the operation and causes flickering when lots of Paint events are raised. When you call Refresh, it calls Invalidate without an argument, so the entire control will be repainted, and then it calls Update, which forces the Paint event to be raised immediately rather than waiting until any currently executing event handler completes. I suggest that you follow the CodeBank link in my signature below and check out my Very Simple Drawing Program thread for an example.

  10. #10
    PowerPoster boops boops's Avatar
    Join Date
    Nov 2008
    Location
    Holland/France
    Posts
    3,132

    Re: Why does my screen blank ?

    Quote Originally Posted by .paul. View Post
    Why not use [graphics].DrawLines, and pass in the whole cords array?
    Graphics.DrawLines draws a polyline, i.e a series of lines joined end to end. Poppa apparently wants to draw separate lines radiating from a centre, so Graphics.DrawLine is needed. BB

  11. #11
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    23,483

    Re: Why does my screen blank ?

    Quote Originally Posted by boops boops View Post
    Graphics.DrawLines draws a polyline, i.e a series of lines joined end to end. Poppa apparently wants to draw separate lines radiating from a centre, so Graphics.DrawLine is needed. BB
    Even DrawLine, for multiple lines should be completed in one execution of the Paint Event...

    Code:
    Private Sub Me_Paint(sender As System.Object, e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
        For i = 0 To cords.Count - 1
            e.Graphics.DrawLine(pen1, centre, cords(i))
        Next
    End Sub

  12. #12

    Thread Starter
    Frenzied Member Poppa Mintin's Avatar
    Join Date
    Mar 2009
    Location
    Skunthorpe, North Lincolnshire, England.
    Posts
    1,912

    Re: Why does my screen blank ?

    Quote Originally Posted by .paul. View Post
    Even DrawLine, for multiple lines should be completed in one execution of the Paint Event...

    Code:
    Private Sub Me_Paint(sender As System.Object, e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
        For i = 0 To cords.Count - 1
            e.Graphics.DrawLine(pen1, centre, cords(i))
        Next
    End Sub
    Yes Paul, thank you, that's what I discovered works.

    I've finished up with this...

    Code:
        Private Sub MakeDrawing()
            Dim back, ink As Color
            Dim memo As Color = Me.BackColor
            Try
                ink = Color.FromName(TextBox2.Text)
                Me.BackColor = ink                     ' This is to detect colour error.
                pen1 = New Pen(ink, 1)
                back = Color.FromName(TextBox1.Text)
                Me.BackColor = back                    ' Also to detect colour error.
                PictureBox1.BackColor = back
            Catch ex As Exception
                Me.BackColor = memo
                StartUp()
                Label3.Show()
                Exit Sub
            End Try
            PictureBox1.Show()
        End Sub
    
        Private Sub DrawAll(sender As System.Object, e _
                    As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint
            For i = 0 To cords.Count - 1
                e.Graphics.DrawLine(pen1, centre, cords(i))
            Next
            ok = True
        End Sub
    I've left the question of colour detection for now, I'll make that a separate thread.

    Everything else works as I'd hoped, to reduce the problem with using the Form's Paint event I've added a PictureBox and drawn the lines in there. I've had to adjust the coordinates to account for the difference between the original centre location and the centre of the 'box of course, but that all adds to the fun.

    Making the form's background colour the same as that of the 'box gives the same effect as if the 'box wasn't even there ('box no border).

    Thanks for your help guys.

    I shall mark this thread as resolved.


    Poppa
    Along with the sunshine there has to be a little rain sometime.

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