Results 1 to 7 of 7

Thread: [02/03] help me in this Form Events ordering . . .

  1. #1

    Thread Starter
    Frenzied Member maged's Avatar
    Join Date
    Nov 2002
    Location
    Egypt
    Posts
    1,040

    [02/03] help me in this Form Events ordering . . .

    hi all ,

    i have a form that inherits from a base form .

    the Base Form tends to execute some code in its load events

    and also the Child Form tends to Execute different code in its load events

    now i need to put some code in the base form that will be executed after both Load events of the form , So where do i put it.

    now i am putting it in the visiblechanged event, but it does trigger when the forms does hide and show actions.

    you will probable ask why not putting it in the child form itself after the load segment. The answer will be that several forms inherits from the same base form. so i need to modify the base form itself in order to make the effect apply on all Child forms . . . .

    i hope i am clear in my question

    thx all in advance

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [02/03] help me in this Form Events ordering . . .

    The dodgy way is to do this in each derived class:
    VB Code:
    1. Private shown As Boolean = False
    2.  
    3. Private Sub Form1_Activated(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Activated
    4.     If Not Me.shown Then
    5.         Me.shown = True
    6.  
    7.         'Execute your code here.
    8.         MessageBox.Show("The form is being shown for the first time.")
    9.     End If
    10. End Sub
    A more professional way is to do this in the base class:
    VB Code:
    1. Private _shown As Boolean = False
    2.  
    3. Public Event Shown(ByVal sender As Object, ByVal e As EventArgs)
    4.  
    5. Protected Overrides Sub OnActivated(ByVal e As EventArgs)
    6.     'Raise the Activated event through the base class.
    7.     MyBase.OnActivated(e)
    8.  
    9.     If Not Me._shown Then
    10.         'Raise the Shown event.
    11.         Me.OnShown(EventArgs.Empty)
    12.     End If
    13. End Sub
    14.  
    15. Protected Overridable Sub OnShown(ByVal e As EventArgs)
    16.     Me._shown = True
    17.  
    18.     'Raise the Shown event.
    19.     RaiseEvent Shown(Me, e)
    20. End Sub
    Then each derived class can handle its own Shown event. This event will be raised after the form has become visible for the first time, just like the Form.Shown event in .NET 2.0.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [02/03] help me in this Form Events ordering . . .

    OK, I just re-read your first post. You say you want to put code in the base class to be executed after the Load event. Do you mean you want this to be executed after the form has actually been displayed or before? If you mean after then as I've done above is appropriate and you can put the code in the OnShown method. If you mean before then you should put the code in the base class's OnLoad method like this:
    VB Code:
    1. Protected Overrides Sub OnLoad(ByVal e As EventArgs)
    2.     'Raise the Load event through the base class.
    3.     MyBase.OnLoad(e)
    4.  
    5.     'Execute your code here.
    6.     MessageBox.Show("The Load event handlers have all been executed but the form hasn't yet been displayed.")
    7. End Sub
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4

    Thread Starter
    Frenzied Member maged's Avatar
    Join Date
    Nov 2002
    Location
    Egypt
    Posts
    1,040

    Re: [02/03] help me in this Form Events ordering . . .

    dear jmcilhinney,

    thx for your perfect answer, i want this code executed before the form is being shown . Because this code should remember that last user settings for the Form . Selected Tabs , Combobox Last selecected index . Etc.

    the problem that i want to put the remeber settings code in the base class to make it affect all my application forms which inherits from this form.

    but i got hit by a problem,

    in the child inherited forms, Lists are being populated in its load event, so i need to remember last user selection after the combos and lists are being filled. that's why i need it to run after the child inherited form load event.

    becuase there is no point on setting last selected index of a combo into an empty combobx (before being populated ) and it raised exceptions.

    i am not sure if my answer is clear but thanks for spending the time reading my posts.

  5. #5

    Thread Starter
    Frenzied Member maged's Avatar
    Join Date
    Nov 2002
    Location
    Egypt
    Posts
    1,040

    Re: [02/03] help me in this Form Events ordering . . .

    another question , does the activated event of the base form executes after the load event of the inhertied form ? ? ?


    what is the sequence of loading events between the two forms

    assuming that the base form is called : BaseForm

    and the inhertied form called : InheritedForm

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [02/03] help me in this Form Events ordering . . .

    You don't need me to answer that. Just call Console.WriteLine in each event handler for the base and derived classes and see for yourself.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    Frenzied Member maged's Avatar
    Join Date
    Nov 2002
    Location
    Egypt
    Posts
    1,040

    Re: [02/03] help me in this Form Events ordering . . .



    ok sorry

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width