locked
Size Changed Event RRS feed

  • Question

  • I'm writing code, and i need to find out when a user minimises the window

    So i am using the Size Changed event (is there another more suitable event to achieve this ?)

    However, if i re-size my form in Design view, and then run the program
    The Size Changed event runs before the Form Load event - is there a way to stop this happening ?
    Also - why is this happening, the size hasn't changed after the program started, it was changed before the program was started, and how can a forms size change before it has even loaded anyway ?

    This is really frustrating, because i am forced to keep my form at 300x300
    Wednesday, March 28, 2007 5:18 PM

Answers

  • To explain the behaviour:   The designer only places code in when the defaults are changed.  When the form is built with the defaults the public properties arn't always used to mod the form.   For the changed event to fire the public size property has to be changed.

     

    When you open it the first time with the defaults the public size property isn't call...

     

    When you change the size the designer addes code to reset the window size....  the public size property is call and the event is fired.

     

     

    Wednesday, March 28, 2007 6:43 PM

All replies

  • Size change event with windows state check works just fine.......   The event fires because the size was zero now it is what ever....

     

    You can handle the changed event in two ways.    Mannully create the handle event after the form load event or create a boolaen varable that is changed after the load event to activate the changed event.

     

    The scecond choice isn't the best way but its easy to understand.....

     

    Code Snippet

    Private FormHasLoaded as boolean = false

     

    ****************   In load event    ****************

    mywork

    mywork

    I'm done

    FormHasLoaded = True

     

    **************  In The Changed Event **********

    if FormHasLoaded then

     

    Do some thing

     

    end if

    'Other wise leave me allow............ :)

     

    Wednesday, March 28, 2007 5:33 PM
  • This is odd, if you go in and make a new form
    First leave it as the default size

    setup the "size changed" and the "form load" routines and put in a line or something

    now put a break point on each of them, run the form, the form load routine runs
    Now change the form size, run it again, the "size changed" routine runs first, then the "form load"

    Why does it have this odd behaviour, it surely shouldn't be doing this ....

    also, i was just wondering if there was another event, like form_minimised or something that could be used instead of doing "size change" then check if its minimised, i guess not

    Thanks for your suggestion though, he had actually done that in his code that i was using, i just didnt understand why he was using a variable
    Wednesday, March 28, 2007 6:13 PM
  • To explain the behaviour:   The designer only places code in when the defaults are changed.  When the form is built with the defaults the public properties arn't always used to mod the form.   For the changed event to fire the public size property has to be changed.

     

    When you open it the first time with the defaults the public size property isn't call...

     

    When you change the size the designer addes code to reset the window size....  the public size property is call and the event is fired.

     

     

    Wednesday, March 28, 2007 6:43 PM
  • An alternative approach:

     

    Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)

    Const SIZE_MINIMIZED As Integer = 1

    Const WM_SIZE As Integer = 5

    If m.Msg = WM_SIZE AndAlso m.WParam.ToInt64 = SIZE_MINIMIZED Then

    Debug.WriteLine("Minimizing...")

    End If

    MyBase.WndProc(m)

    End Sub

    Wednesday, March 28, 2007 7:40 PM