paint graphics on panel with menuitem1.click

PG74

New member
Joined
Aug 6, 2004
Messages
2
Programming Experience
Beginner
paint graphics on panel with menuitem1.click

[font=verdana, arial, helvetica]I want to paint some lines and fonts on a panel, but I want it to paint when I click on a menuitem, have tried the code below, but it paints when I open the form. I dont want it to paint untill I call the sub with my MenuItem3.click.

Private Sub Panel1_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Panel1.Paint
'here is the code for lines and fonts
end sub

Private Sub MenuItem3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem3.Click
Panel1_Paint() ' calling the sub to paint the panel
End Sub

have heard you can use a sub "panel1.refresh()" or something like that, but I dont know how it works.
someone have an example code or can explain for me?? please!

thanks!
[/font]
 
The paint event happens anytime the control is redrawn, when the graphics have become invalid. This initially happens when the control is loaded. It also happens when the graphics become invalid, like when they are moved off the screen then back on.

If you want to paint only after the MenuItem is clicked, use a boolean variable with an If Then statment. Define the variable at class scope (bDraw for example). In the MenuItem's click event handler set the variable to true. In the paint event handler, use an If Then statement:

VB.NET:
Private Sub Panel1_Paint(ByVal sender As System.Object, _
  ByVal e As System.Windows.Forms.PaintEventArgs) Handles Panel1.Paint
    If bDraw Then
        'here is the code for lines and fonts
    Else
        e.Graphics.Clear(Panel1.BackColor)
    End if
End Sub

The line e.Graphics.Clear(Panel1.BackColor) will remove the lines and fonts if they have been previously drawn, otherwise they will remain until the graphics become invalid and a paint message is issued for the panel.

In the MenuItem's click event handler, in addition to setting the variable to True you may need to call the Invalidate or Refresh method of the panel to get the panel to redraw:

VB.NET:
Private Sub MenuItem3_Click(ByVal sender As System.Object, _
  ByVal e As System.EventArgs) Handles MenuItem3.Click
    bDraw = True
    Panel1.Refresh()
End Sub

Notice that this: Panel1_Paint() will cause an error because the required parameters are not included.
 
Back
Top