Sizechanged event question...

J

johnb41

I have a Picturebox that has an image displayed inside. Through code,
I was able to have the "whole image" display in it without distortion.
(Like a "Fit All" in most any image viewer program)

Now, when the Picturebox gets resized by the user, I want the image to
recalculate and display as "Fit All" again.

So I have code in my SizeChanged event of the picturebox. When the
"size changed", it does it's calculation, and displays properly.

It works, but there is a problem: If the user drags the Picturebox
window larger or smaller, the SizeChanged event fires many times per
second. This causes alot of sluggishness in the app when trying to
display the image.

Is there anyway that i can have the SizeChanged event fire ONCE when
i'm done resizing the picturebox?

(By the way, the picturebox gets resized indirectly. It's dock is set
to Fill. When the interface "Next" to it gets resized, the Picturebox
naturally will resize accordingly)

Thanks for all your help!

John
 
C

Cor Ligthert

John,

You can use in the event two static variables (or one static point) to see
everytime if it changed,

I hope this helps,

Cor
 
P

Peter Proost

Hi John,

which sizechanged event are you handling? Because I have written a bit of
code which does the same thing, and tried it in these 3 events and they all
only fire once, when I finish dragging the form border

Private Sub ptbImg_Resize(ByVal sender As Object, ByVal e As
System.EventArgs) Handles ptbImg.Resize
End Sub

Private Sub ptbImg_SizeChanged(ByVal sender As Object, ByVal e As
System.EventArgs) Handles _ ptbImg.SizeChanged
End Sub

Private Sub frmResize_SizeChanged(ByVal sender As Object, ByVal e As
System.EventArgs) Handles _ MyBase.SizeChanged

End Sub

greetz Peter
 
J

johnb41

Peter,

It's actually a Panel that I have the SizeChanged event attached to.
This is so the I get scrollbars when the image is larger than the
Panel.

If it's not any trouble, could you post some sample code that makes the
event fire only once? I just did a sample test on resizing a plain
form:

Private Sub Form1_SizeChanged(...) Handles MyBase.SizeChanged
MessageBox.Show("Size Changed!")
End Sub

The form won't even resize because the messagebox pops up immediately.
If I could get the messagebox to show up after finishing the resize,
that would be awesome. I could then adapt it to my real app.

Thanks!
John
 
J

johnb41

Thanks for the reply. You make it seem obvious! Could you describe
more of what you mean, as it's very unclear to me. Thanks!

John
 
P

Peter Proost

Hi John,

when you run you code:
Private Sub Form1_SizeChanged(...) Handles MyBase.SizeChanged
MessageBox.Show("Size Changed!")
End Sub

the first time you see the messagebox pop up is because the form is
initialized, but after that the event normal only fires if you let go the
form border, so at the end of the SizeChanged event.

hth
Greetz Peter
 
C

Cor Ligthert

"johnb41"
Thanks for the reply. You make it seem obvious! Could you describe
more of what you mean, as it's very unclear to me. Thanks!
\\\
Static OldSize As Size
If Not OldSize.Equals(MeControl.Size) Then
....procedure
End If
///
I hope this helps,

Cor
 
H

Herfried K. Wagner [MVP]

johnb41 said:
I have a Picturebox that has an image displayed inside. Through code,
I was able to have the "whole image" display in it without distortion.
(Like a "Fit All" in most any image viewer program)

Now, when the Picturebox gets resized by the user, I want the image to
recalculate and display as "Fit All" again.

So I have code in my SizeChanged event of the picturebox. When the
"size changed", it does it's calculation, and displays properly.

It works, but there is a problem: If the user drags the Picturebox
window larger or smaller, the SizeChanged event fires many times per
second. This causes alot of sluggishness in the app when trying to
display the image.

If you are experiencing a flicker, you could enable double buffering for the
picturebox control. To do so, create a class that inherits from
'System.Windows.Forms.PictureBox' and provides the constructor shown in the
listing below:

\\\
Public Sub New()
Me.SetStyle( _
ControlStyles.ResizeRedraw Or _
ControlStyles.DoubleBuffer Or _
ControlStyles.AllPaintingInWmPaint, _
True _
)
Me.UpdateStyles()
End Sub
///

Then you use your custom picturebox control instead of the standard Windows
Forms picturebox.
 
J

johnb41

I gotta leave and do family stuff on this Memorial Day, so i'll only
post this quick message:

For me, the SizeChanged event seems to keep firing again and again,
until I let go of the mouse. This is in my real app, not the sample
one w/ the messagebox. For example, the image constantly tries to
redraw its self until I let go of the mouse.

I'll have to do more testing, and then play w/ Cor and herfried's
suggestions...

John
 
J

johnb41

Thanks for the code. But it made no difference. Here is what I now
have:

Private Sub Pnl_Image_SizeChanged(...) Handles
Pnl_Image.SizeChanged
Static OldSize As Size
If Not OldSize.Equals(Pnl_Image.Size) Then
' call procedure to resize and display image
End If
End Sub

What am I missing?

John
 
C

Cor Ligthert

John,

I forgot it, however this is classic
Private Sub Pnl_Image_SizeChanged(...) Handles
Pnl_Image.SizeChanged
Static OldSize As Size
If Not OldSize.Equals(Pnl_Image.Size) Then
' call procedure to resize and display image
End If

OldSize = Pnl_Image.Size

I hope this helps,

:)

Cor
 
J

johnb41

Cor,

That didn't make a difference. My procedure fires again and again
until I let go of the mouse.

It's actually not "as" bad now that i'm at work on a much faster
computer. But it's still annoying.

Any more suggestions, or should I just give up?

Thanks,
John
 
C

Cor Ligthert

John,

Sorry, I was reading it not well I thought that the picture stayed the same,
of course does this not help.

The only thing that I can come up that can help you here is probably a
timer.
We people are not able to see some things that fast.

I never tried it, however you can probably do it in the same way as
mysample, and than there is not much to change.

Private Sub Pnl_Image_SizeChanged(...) Handles
Pnl_Image.SizeChanged
Static OldTime As Integer
If (Environment.TicksCount - OldTime) > 50 Then
' call procedure to resize and display image
End If
OldTime = Environments.TicksCount
End Sub

And than set that 50 to a proper value.

Maybe this helps,

I am curious.

Cor
 
J

johnb41

Cor,

Interesting idea, thanks!

But the only benefit it has is the image redraw happens a little less
frequently. And if I let go of the mouse at the wrong time, the image
doesn't redraw. I have to let go of the mouse immediately after it
redraws if I want the image displayed properly.

I played w/ the number value. A number too high refreshes too
infrequently, and often when I let go of the mouse, my subroutine
doesn't fire, and thus my image doesn't get resized/redrawn. A number
too low, and the effect is similar to the original code.

Well, I really appreciate all the help you've given me, Cor. But it
looks like this is pretty much impossible! :(

John
 
P

Peter Proost

John, have you looked at my example?

in my opinion it does what you want.

Greetz Peter
 
J

johnb41

Peter,

Yes, I have. For me, the SizeChanged event fires again and again as
the Panel gets resized, not just when I let go of the mouse. Am I
missing something? Did you write any special code so it works as you
say?

John
 
J

johnb41

Peter,

Yes, I have. For me, the SizeChanged event fires again and again as
the Panel gets resized, not just when I let go of the mouse. Am I
missing something? Did you write any special code so it works as you
say?

John
 
P

Peter Proost

Hi John, sorry for not responding faster, but after 5 o' clock (belgian
time), I haven't got access to the ng's for the moment (pc at home is broke)
The example I posted, runs very smooth at my computer, it only fires the
sizechanged event once, and that's when I release the form border after
resizing.
Which version of VS do you have? 2002, 2003? I have got 2003.

Greetz Peter
 
J

johnb41

I have VS 2003 also. So let me get this straight, if you simply put in
a messagebox inside a Sizechanged event (for a Form), the messagebox
does not appear until AFTER you let go of the mouse button? No extra
code required?

Amazing. I can't even resize a form because the messagbox pops up too
soon.

If anyone else is reading this, does this happen to you also?

If this is true, i'm surprised that the others that posted in this
thread didn't mention it. Cor really went out of his way to help w/ a
couple potential solutions. Now i'm more confused than ever! (but that
isn't new)

John
 
P

Peter Proost

I also find it very strange, if I put a msgbox in the sizechanged event of a
form, it first pops up before the form is even visible (that's just the form
initializing) and then when the form is loaded it only pops up AFTER I let
go of the mouse button, no extra code required.

For example the example I posted at my pc only fires the sizechanged event
after I let go of the mousebutton.

Greetz Peter
a messagebox inside a Sizechanged event (for a Form), the messagebox
does not appear until AFTER you let go of the mouse button? No extra
code required?
 

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