468,491 Members | 2,048 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,491 developers. It's quick & easy.

Painting when Button gets klicked

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
Nov 20 '05 #1
8 3098
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
-------------------
"Stefan Richter" <sp**@spammenot.com> wrote in message
news:c7***********@otis.netspace.net.au...
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

Nov 20 '05 #2
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
Nov 20 '05 #3
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
---------------------------
"Stefan Richter" <sp**@spammenot.com> wrote in message
news:c7***********@otis.netspace.net.au...
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

Nov 20 '05 #4
"Stefan Richter" <sp**@spammenot.com> schrieb
What is PictureBox1.CreateGraphics?
It is a call of the CreateGraphics method of the Picturebox. Press <F1> for
more.
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...tdraw_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

Nov 20 '05 #5
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
Nov 20 '05 #6
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
Nov 20 '05 #7
Doesn't seem to work, as a method that handles MyBase.Paint is missing.

Cheers,

Stefan
Nov 20 '05 #8
"Stefan Richter" <sp**@spammenot.com> schrieb
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.
--
Armin

Nov 20 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Bill K | last post: by
3 posts views Thread by gieforce | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.