[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
Re: [02/03] help me in this Form Events ordering . . .
The dodgy way is to do this in each derived class:
VB Code:
Private shown As Boolean = False
Private Sub Form1_Activated(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Activated
If Not Me.shown Then
Me.shown = True
'Execute your code here.
MessageBox.Show("The form is being shown for the first time.")
End If
End Sub
A more professional way is to do this in the base class:
VB Code:
Private _shown As Boolean = False
Public Event Shown(ByVal sender As Object, ByVal e As EventArgs)
Protected Overrides Sub OnActivated(ByVal e As EventArgs)
'Raise the Activated event through the base class.
MyBase.OnActivated(e)
If Not Me._shown Then
'Raise the Shown event.
Me.OnShown(EventArgs.Empty)
End If
End Sub
Protected Overridable Sub OnShown(ByVal e As EventArgs)
Me._shown = True
'Raise the Shown event.
RaiseEvent Shown(Me, e)
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.
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:
Protected Overrides Sub OnLoad(ByVal e As EventArgs)
'Raise the Load event through the base class.
MyBase.OnLoad(e)
'Execute your code here.
MessageBox.Show("The Load event handlers have all been executed but the form hasn't yet been displayed.")
End Sub
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. :)
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
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.
Re: [02/03] help me in this Form Events ordering . . .