Painting when Button gets klicked

S

Stefan Richter

I want to paint some bars, as soon as a button get klicked.
My Paint Method has this signature:
Private Sub PaintChart(ByVal sender As Object, ByVal e As PaintEventArgs,
ByVal insolvent As Decimal, ByVal days30 As Decimal, ByVal days60 As
Decimal, ByVal days90 As Decimal, ByVal current As Decimal)

Means it needs to get a PaintEventArgs to work.

My Button Click Method has this signature:
Private Sub view_something_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles view_something.Click

In this method, I want to call my PaintChart method, because only then I
know all local parameters.
(okay, one solution would be to make all parameters global, but I doubt that
that was any good...)

PaintChart(sender, PaintEventArgs , insolvent, days30, days60, days90,
current)

Now the problem is, when I add the PaintEventArgs Event to my Click method,
then the signature doesnt fit anymore.

Have I got to do my own Handler, or how can that be solved???
(And how do I make one, if necessary)



Thanks,



Stefan
 
K

Ken Tucker [MVP]

Hi,

Create a graphics object for what you want to draw on in the click
event. In your PaintGraph procedure pass a graphics object instead of a
painteventargs. Here is a simple example on how draw bars when a user
clicks on a button.

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button3.Click
Dim g As Graphics = PictureBox1.CreateGraphics

g.FillRectangle(Brushes.Red, 0, 0, 10, 50)
g.FillRectangle(Brushes.Blue, 10, 0, 10, 50)
End Sub

Ken
 
S

Stefan Richter

What is PictureBox1.CreateGraphics?

And, when I created this Graphic, how could I forward it to my method again?
Cause the paint method that reacts on the PaintEventArgs can't have extra
arguments,
because otherwise the necessary signature for this event doesn't fit
anymore.

Sorry, Iam really confused by this stuff!

Maybe I should show you, what I want to do:

OnClick {

CalculateCoordinates()
DrawBars(Coordinates)

}


CalculateCoordinates{
.........
.........
}

DrawBars{
.........
.........
}


Thx,

Stefan
 
A

Armin Zingler

Stefan Richter said:
What is PictureBox1.CreateGraphics?

And, when I created this Graphic, how could I forward it to my method
again? Cause the paint method that reacts on the PaintEventArgs can't
have extra arguments,
because otherwise the necessary signature for this event doesn't
fit anymore.

Write a procedure. The procedure does the painting. One argument of the
procedure is a Graphics object. Call the procedure in the paint event. Pass
e.graphics to the procedure. In your click handler, call the proecedure,
too. As you don't have a graphics object there, you have to create it by
calling Creategraphics. Similar to Ken's answer:

Dim g As Graphics

g = PictureBox1.CreateGraphics
try
YourPaintingProcedure(g)
finally
g.dispose
end try

sub YourPaintingProcedure(byval g as graphics)
'Paint on g here
end sub

Instead of calling the procedure in the click event, you can simply call
Picturebox1.invalidate. This invalidates the picturebox' content. See
http://msdn.microsoft.com/library/en-us/gdi/pantdraw_7zn2.asp
and the following topics.

--
Armin

How to quote and why:
http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html
 
K

Ken Tucker [MVP]

Hi,

The graphics object is the class the handles drawing on a form or
control. Try something like this.


Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button3.Click
Dim g As Graphics = PictureBox1.CreateGraphics
PaintChart(g, 0, 0, 0, 0, 0)
End Sub

Private Sub PaintChart(ByVal g As Graphics, ByVal insolvent As Decimal,
ByVal days30 As Decimal, ByVal days60 As Decimal, ByVal days90 As Decimal,
ByVal current As Decimal)
g.FillEllipse(Brushes.Red, 10, 10, 100, 100)
g.FillRectangle(Brushes.Red, 0, 0, 10, 50)
g.FillRectangle(Brushes.Blue, 10, 0, 10, 50)

End Sub

Ken
 
S

Stefan Richter

Sorry, but that doesn't work.
I guess it's because MyBase.Paint does not repaint the Object over and over
again.

Dim PictureBox1 As New System.Windows.Forms.PictureBox

I tried:

Private Sub view_crs_mth_btn_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles view_crs_mth_btn.Click

.......

....

Dim g As Graphics = PictureBox1.CreateGraphics

g.FillRectangle(Brushes.Red, 0, 0, 10, 50)

g.FillRectangle(Brushes.Blue, 10, 0, 10, 50)

..............

End Sub
 
S

Stefan Richter

Okay, so you want me to write a paint procedure.
-----------
Private Sub PaintChart(ByVal e As Graphics, ByVal value1 As Integer, ByVal
value2 As Integer )

e.FillRectangle(Brushes.White, 530, value1 , 35, 150)
e.FillRectangle(Brushes.Lime, 600, value2, 35, 75)

End Sub

-------------
Then you want me to write a paint event:
Private Sub PaintChart2(ByVal sender As Object, ByVal e As PaintEventArgs)
Handles MyBase.Paint

Dim g As Graphics = e.Graphics

PaintChart(g, 0, 0)

End Sub

------------

How can I know value1 and value2 in the paint event????

---------

Private Sub view_crs_mth_btn_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles view_crs_mth_btn.Click

Dim value1 As Integer

Dim value2 as Integer

.....

Dim g As Graphics

Dim PictureBox1 As New System.Windows.Forms.PictureBox

g = PictureBox1.CreateGraphics

PaintChart(g, value1, value2)

-----------
So value1 and value2 have to be global for the whole class then????
And if so, how can I hide the Graphics again or better how can I stop the
Paint
method to try to paint it again???

thx,

Stefan
 
S

Stefan Richter

Doesn't seem to work, as a method that handles MyBase.Paint is missing.

Cheers,

Stefan
 
A

Armin Zingler

Stefan Richter said:
Okay, so you want me to write a paint procedure.
-----------
Private Sub PaintChart(ByVal e As Graphics, ByVal value1 As Integer,
ByVal value2 As Integer )

e.FillRectangle(Brushes.White, 530, value1 , 35, 150)
e.FillRectangle(Brushes.Lime, 600, value2, 35, 75)

End Sub

-------------
Then you want me to write a paint event:
Private Sub PaintChart2(ByVal sender As Object, ByVal e As
PaintEventArgs) Handles MyBase.Paint

Dim g As Graphics = e.Graphics

PaintChart(g, 0, 0)

End Sub

------------

How can I know value1 and value2 in the paint event????

---------

Private Sub view_crs_mth_btn_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles view_crs_mth_btn.Click

Dim value1 As Integer

Dim value2 as Integer

....

Dim g As Graphics

Dim PictureBox1 As New System.Windows.Forms.PictureBox

g = PictureBox1.CreateGraphics

PaintChart(g, value1, value2)

-----------
So value1 and value2 have to be global for the whole class
then????
Yes

And if so, how can I hide the Graphics again or better how
can I stop the Paint
method to try to paint it again???

Why stop it? The Paint event is only raised if it is required.


In general, you must remember what you want to display in the control.
Whenever the data influencing the painting changes, or whenever Windows
wants you to repaint, you must paint again.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top