שתף באמצעות


How to catch maximize, minimize, restore buttons click events in VB 2005?

Question

Saturday, July 19, 2008 6:07 PM

Hi,

I'm drawing lines (using graphics.drawlines) on a picture box inside a form and I need to resize the drawings when the form is resized. Doing this inside form.resize event doesn't work the way I wanted. I need to do the redrawing after resize is completed. So, I'm redrawing the lines inside form.resizeend and it works fine when the form is resized by a mouse drag. However, if form maximize button is used, it triggers resize event but not resizeend event. Neither does restore button. How can I capture the resizeend event when these buttons are used?

Or even better, is there a way to save the the drawn lines as bitmap (or something that I can resize) and resize it along with the picture box?

I can also upgrade to VB 2008. Does VB 2008 has the same problem?

Thanks,
EF   

All replies (9)

Monday, July 21, 2008 6:59 PM ✅Answered | 1 vote

Ah, OK, you are part of the way there.

First off, You don't need to use .CreateGraphics. If you look at the arguments that are passed to the paint function, you will have the graphics object made available to you. so instead of:

PictureBox1.CreateGraphics 

You will have:

e.Graphics 

Now second, the reason it's not redrawing on a 'shrinking' of the form is due to how the picturebox is invalidated. When the picturebox is resized (Resize event), you can 'invalidate' the whole picturebox:

PictureBox1.Invalidate 

This will force all the graphics to draw in the paint event. Give that a try and see if it works.


Stephen J Whiteley


Saturday, July 19, 2008 6:48 PM | 3 votes

In order to catch the resize, minimize and restore events of a form you could have a look at the SizeChanged event:

Private Sub Form1_SizeChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.SizeChanged  
        If Me.WindowState = FormWindowState.Minimized Then  
            MsgBox("The form has been minimized.")  
        End If  
    End Sub 

Hope it helps.

Best regards.

Ale N.


My Website --> http://www.alession.blogspot.com/ || Please, write any comment or suggestion.


Saturday, July 19, 2008 8:03 PM

Thanks for the reply Ale. Unfortunately, sizechanged also doesn't work for me. Topic subject is kind of misleading. My problem isn't catching maximize event but end of it. I need to somehow force resizeend event when maximize is completed.

EF

 


Sunday, July 20, 2008 7:31 PM

Calling the draw update under picturebox's paint event and adding a timer with interval set to 1 solved the problem, but I still don't understand why maximize button click triggers form_resize event but not formresizebegin and formresizeend.  

Private Sub GC1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles GC1.Paint

    GC1.clear()

    Timer1.Enabled = True

End Sub

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick

    GC1.drawData(mydata)

    Timer1.Enabled = False

End Sub


Monday, July 21, 2008 4:17 PM

You shouldn't need to do any of this: if you are performing your drawing (as you should be) in the paint event, this event will fire once the resize event is complete.


Stephen J Whiteley


Monday, July 21, 2008 6:45 PM

I thought so too. But it doesn't work properly. I have a very simple code to try this. There is a picturebox on a form and picturebox is anchored to the form on all 4 sides. What I want to do is draw a line across the picture box and have it resized along with the picturebox size.

 

Using the paint event as in the below code works if you increase the form size by dragging. If you decrease it, paint event is not triggered and line is drawn by using the previous size of the picture box. Same is true for maximize and restore button clicks.    

Public Class Form1

Private Sub PictureBox1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint

PictureBox1.CreateGraphics.Clear(PictureBox1.BackColor)

PictureBox1.CreateGraphics.DrawLine(Pens.Black, 0, 0, PictureBox1.Width, PictureBox1.Height)

End Sub

End Class


Monday, July 21, 2008 7:07 PM

Thanks a lot. It is working now.


Wednesday, April 6, 2011 6:01 AM

EagleForce Just wanted to add to your very useful post.

        use the e.graphics

 

Whithin the Picturebox1.Resize((((

                Picturebox1.Invalidate 'Cause a Java like re-paint! ala  forces resize redraw

 

Thanks for this very useful post

                                                       /  |^)


Tuesday, May 6, 2014 3:12 PM

Just a quick addition to this:

I followed Ale's advice but kept getting "InvalidOperationException was unhandled" when the form loaded.

I eventually worked out that the code on sizechanged event was checking the size of a control that the form hadn't displayed yet.

Adding a "If Me.Visible Then...." line of code to the beginning of the event fixed the problem.

Kristian

Kristian Benning Sharepoint Newbie (Slightly better at VBA and SQL databases)