Results 1 to 7 of 7

Thread: Use enter TAB event to draw graphics

  1. #1

    Thread Starter
    New Member
    Join Date
    Sep 2015
    Posts
    10

    Use enter TAB event to draw graphics

    Hi,

    I have a tabControl that has a pictureBox on one of the tabs. I want to use the Enter TAB event to draw graphics but they do not appear.

    Using the below code:
    When I enter the tab drawGaphics is called but nothing appears
    When I click the button to initiate the drawing, drawGaphics is called and the graphics appears as expected.

    Code:
        Private Sub TAB_GRAPH_Enter(sender As Object, e As EventArgs) Handles TAB_GRAPH.Enter
            debug("Enter Graphics TAB")
            drawGraph()
        End Sub
    
    
        Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
            drawGraph()
        End Sub
    
        Private Sub drawGraph()
            debug("Enter drawGraph")
            Dim myGraphics As Graphics = PictureBox1.CreateGraphics
    
            ' myPen = New Pen(Brushes.Color, LineWidth)
            Dim myPen = New Pen(Brushes.Black, 1)
            Dim myRectangle As New Rectangle(10, 10, 300, 300)
    
            myGraphics.DrawRectangle(myPen, myRectangle)
            myGraphics.DrawLine(myPen, 60, 20, 200, 20)
    
        End Sub

  2. #2
    Fanatic Member Arve K.'s Avatar
    Join Date
    Sep 2008
    Location
    Kyrksæterøra, Norway
    Posts
    518

    Re: Use enter TAB event to draw graphics

    Did you put a breakpoint in that event to make sure that it is actually triggered?
    Arve K.

    Please mark your thread as resolved and add reputation to those who helped you solve your problem
    Disclaimer: I am not a professional programmer

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,350

    Re: Use enter TAB event to draw graphics

    NEVER call CreateGraphics. If you want to draw on a control then handle the Paint event of that control and use the Graphics object provided. If you ever want to change what's drawn on the control, call Invalidate on the control.

  4. #4

    Thread Starter
    New Member
    Join Date
    Sep 2015
    Posts
    10

    Re: Use enter TAB event to draw graphics

    Quote Originally Posted by jmcilhinney View Post
    NEVER call CreateGraphics. If you want to draw on a control then handle the Paint event of that control and use the Graphics object provided. If you ever want to change what's drawn on the control, call Invalidate on the control.
    I tried with the paint event but the code to draw the graphics was called many times; 15 times with a simple example.
    Is there a way to ensure the drawing code is called only one?

  5. #5

    Thread Starter
    New Member
    Join Date
    Sep 2015
    Posts
    10

    Re: Use enter TAB event to draw graphics

    Quote Originally Posted by Arve K. View Post
    Did you put a breakpoint in that event to make sure that it is actually triggered?
    Not a break point but a strinf to a debug textbox. "Enter Graphics TAB" + "Enter drawGraph" is displayed when the tab is entered and then "Enter drawGraph" is displayed when the button is clicked.

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,350

    Re: Use enter TAB event to draw graphics

    Quote Originally Posted by MartynC View Post
    I tried with the paint event but the code to draw the graphics was called many times; 15 times with a simple example.
    Is there a way to ensure the drawing code is called only one?
    You're trying to solve a problem that doesn't exist. There is no reason to try to ensure that the drawing code is called only once. Windows Forms is based on GDI+ and the way that works is that everything gets drawn every time. If the Paint event is raised 15 times then you need to your drawing code to be executed 15 times otherwise your drawing will be erased. That's why what you're drawing using CreateGraphics doesn't appear: it gets erased the next time there's a Paint event.

    Just do what you're supposed to do and every other .NET developer around the world does: draw in the Paint event handler.

  7. #7

    Thread Starter
    New Member
    Join Date
    Sep 2015
    Posts
    10

    Re: Use enter TAB event to draw graphics

    The previous attempt at using the paint event resulted in the drawn graphics being very sluggish and flickery (which is why I analysed how many times it was being drawn). I don't have the code any more so cannot must have had something wrong because the latest try works fine.

    Thanks everyone for your help.

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