-
Apr 23rd, 2018, 07:22 AM
#1
Thread Starter
New Member
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
-
Apr 23rd, 2018, 08:26 AM
#2
Re: Use enter TAB event to draw graphics
Did you put a breakpoint in that event to make sure that it is actually triggered?
-
Apr 23rd, 2018, 08:39 AM
#3
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.
-
Apr 23rd, 2018, 09:00 AM
#4
Thread Starter
New Member
Re: Use enter TAB event to draw graphics
Originally Posted by jmcilhinney
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?
-
Apr 23rd, 2018, 09:02 AM
#5
Thread Starter
New Member
Re: Use enter TAB event to draw graphics
Originally Posted by Arve K.
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.
-
Apr 23rd, 2018, 06:44 PM
#6
Re: Use enter TAB event to draw graphics
Originally Posted by MartynC
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.
-
Apr 26th, 2018, 08:11 AM
#7
Thread Starter
New Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|