Question Is it possible to disable the PictureBox Paint event?

Thonord

Member
Joined
Oct 25, 2012
Messages
23
Programming Experience
10+
Or rather; control when it fires - or not - even if it wants to.
I have a PictureBox (Pb) on a panel on a form, loaded with an image, much larger than the Pb. (the reason for the panel is cuz it gives auto scrollbars).

90% of operation is done with the mouse over the Pb. I'm using move, down, up, left, right etc. etc. and I'm trapping form.keypress to draw stuff on the Pb. All works OK.

But - because I use keypress, I have to keep track of witch form control that has focus. I don't want text to appear in a random TextBox, and CRLF creates a lot of fun:)
To solve this, I set focus to the Pb when mousy enters the Pb, and focus to the "most probable" TextBox when I leave. Also works fine, But both focus-changes cause paint events.

Since the image is much larger than the Pb, if I "move" the image with the panel scrollbars, and then cross the boundary of the Pb, the image resets to the default position because of the paint event. (0,0 at Top,Left)

I really need to be able to move the mouse in and out of the Pb without the scrolled image resetting!

What I'm looking for is

If I don't want you to fire Then
qqqqqDon't fire
Else
qqqqqDo
Endif
 

IanRyder

Well-known member
Joined
Sep 9, 2012
Messages
1,130
Location
Healing, NE Lincs, UK
Programming Experience
10+
Hi,

You are on the wrong track with trying to handle the PictureBox paint event. What is happening here is that the Panel is raising it's own paint event since the AutoScroll is set to True. When you set the Focus back to the PictureBox within the Panel it then causes it's paint event to fire.

Unfortunately, as far as I can see, there is no way for the panel to remember it's current Scroll position so you need to save this position and then reset this position when the focus has been made to the PictureBox.

To demonstrate, try this:-

VB.NET:
Private Sub PictureBox1_MouseEnter(sender As Object, e As System.EventArgs) Handles PictureBox1.MouseEnter
  Dim CurrentPoint As Point = Panel1.AutoScrollPosition()
  PictureBox1.Focus()
  Panel1.AutoScrollPosition = New Point(Math.Abs(CurrentPoint.X), Math.Abs(CurrentPoint.Y))
End Sub

Hope that helps.

Cheers,

Ian
 

Thonord

Member
Joined
Oct 25, 2012
Messages
23
Programming Experience
10+
No doubt, the work-around works. I just need to find all "the stuff" I do that causes either a PanelPaint or a PicBoxpaint - but I shall prevail:)
Thanks again.
 
Top Bottom