Click here to Skip to main content
15,881,248 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Hello!

I'm having a little problem: My pictureBox1_Paint-event does not fire, because of my use of a skinning-device, that obviously overrules all the paint-events.

It is not that big a problem, if only I could call this event manually. But how do I do that?
Is it possible at all?

The event uses the parameters "object sender, PaintEventArgs e", and I believe the secret to make things work is in the PaintEventArgs. Is there any way to make these by hand?

The code (example):

C#
public void pictureBox1_Paint(object sender, PaintEventArgs e)
{
    using (Pen pen = new Pen(Color.Red, 1))
    {
        e.Graphics.DrawRectangle(pen, 10, 10, 30, 20);
    }
}


Thanks in advance.

Sincerely/Best regards
Mads Aggerholm
Posted
Updated 2-Jul-13 22:51pm
v2

Although you could call a paint event manually, by constructing a PaintEvenArgs instance for yourself, and calling the method, it's a poor idea.
C#
PaintEventArgs pea = new PaintEventArgs(MyGraphics, MyClipRect);
pictureBox1_Paint(pictureBox1, pea);

A better solution would be to move the code out of the Event handler into a method, which you pass the Graphics Context from the Paint event, and then call that instead, supplying a Graphics object suitable for the painting (and remembering to Dispose it when the method returns)

However, if your skinning mechanism is not passing the Paint events on down you your controls, then you need to look at it and find out why - as it would disable all "clever" graphics effects and probably make many, many controls fail. Are you sure you are calling Invalidate on the correct controls to initiate the Paint in the first place?
 
Share this answer
 
Comments
Mads1967 5-Jul-13 2:53am    
Thanks! This one did the trick.
Doesn't your skinning component allow for you to exclude certain controls from the skinning process? That is normally the case...

I would go that way instead of try to manipulate the components by hand.

If not, then you could try to ask the maker of the skinning component if it would be possible for them to provide such an option.
 
Share this answer
 
v2
Comments
Mads1967 5-Jul-13 2:47am    
Well, it does. However, it can not be turned off regarding pictureboxes. I believe it's because the pictureboxes does not require skinning, at therefore thought not to need the necessity to be shut off.
The skinning tool just sits on all paint-events, including the pictureboxes (my theory).
Johnny J. 5-Jul-13 3:02am    
:-(
call like this from any other function...

C#
public void Button1_Click(object sender, EventArgs e)
{
pictureBox1_Paint(null,null);

}

public void pictureBox1_Paint(object sender, PaintEventArgs e)
{
    using (Pen pen = new Pen(Color.Red, 1))
    {
 Graphics g = pictureBox1.CreateGraphics();
        g.DrawRectangle(pen, 10, 10, 30, 20);
    }
}
 
Share this answer
 
v3
Comments
Mads1967 5-Jul-13 2:48am    
Thanks for the try, but "pictureBox1_Paint(null,null);" raises an error!
OriginalGriff 5-Jul-13 3:54am    
Reason for my vote of one: Never, ever construct a graphics object in the paint event handler! It is provided for you via the PaintEventArgs parameter, and subverting that destroys all sorts of things - like printing, print preview, and so forth.
And whenever you do construct a Graphics object is is vital that you Dispose of it - they are scarce resources which will run out well before the Garbage collector gets activated - at which point your application with crash with an "out of memory" exception
The solution inspired from OriginalGriff:

C#
public void pr_picturebox_refresh()
{
	Graphics l_gr_work = picturebox1.CreateGraphics();
	picturebox1.Refresh();
	Rectangle l_rt_work = new Rectangle(0, 0, picturebox1.Width, picturebox1.Height);
	PaintEventArgs l_pe_work = new PaintEventArgs(l_gr_work, l_rt_work);
	picturebox1_Paint_manual(picturebox1, l_pe_work);
}

private void picturebox1_Paint_manual(object sender, PaintEventArgs e)
{
	Rectangle l_rt_work = new Rectangle(10, 10, 50, 40);

	using (Pen pen = new Pen(Color.Red, 1))
	{
		e.Graphics.DrawRectangle(pen, l_rt_work);
	}
}


This works!

I just have to call pr_picturebox_refresh() from whereever the app would have fired "picturebox1_Paint" automatically - and that was expected.

Thank you everybody for your time.
 
Share this answer
 
v2
Comments
Sushil Mate 5-Jul-13 3:06am    
you can accept his solution as well or upvote. which might be helpful for others.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900