Q: OnLoad

  • Thread starter Thread starter Geoff Jones
  • Start date Start date
G

Geoff Jones

Can anybody tell me the event I should use if I want a OpenFileDialog to
open as soon as a from has displayed? I tried putting it in the forms OnLoad
but this means that the form isn't fully displayed until the OpenFileDialog
is shutdown.

Many thanks in advance

Geoff
 
Geoff,

Try putting Me.Show before calling the openfiledialog's ShowDialog method.

Kerry Moorman
 
Geoff,
Have you considered using the Activate event?

Something like:

Protected Overrides Sub OnLoad(ByVal e As System.EventArgs)
MyBase.OnLoad(e)
AddHandler Me.Activated, AddressOf MainForm_InitialOpen
End Sub

Private Sub MainForm_InitialOpen(ByVal sender As Object, ByVal e As
System.EventArgs)
RemoveHandler Me.Activated, AddressOf MainForm_InitialOpen
Dim dialog As New OpenFileDialog
dialog.ShowDialog(Me)
End Sub

The AddHandler & RemoveHandler ensure that MainForm_InitialOpen is only
called once.

If I had a number of forms that need the "InitialOpen" event I would
consider putting the above code in a base class that raises an InitialOpen
event, when the first Activated event is called...

My other thought was the Application.Idle event, that you also only allow to
be called once.

Hope this helps
Jay

| Can anybody tell me the event I should use if I want a OpenFileDialog to
| open as soon as a from has displayed? I tried putting it in the forms
OnLoad
| but this means that the form isn't fully displayed until the
OpenFileDialog
| is shutdown.
|
| Many thanks in advance
|
| Geoff
|
|
 
Geoff Jones said:
Can anybody tell me the event I should use if I want a OpenFileDialog to
open as soon as a from has displayed? I tried putting it in the forms
OnLoad but this means that the form isn't fully displayed until the
OpenFileDialog is shutdown.

\\\
Private Sub Form1_Activated( _
ByVal sender As Object, _
ByVal e As EventArgs _
) Handles MyBase.Activated
Static IsActivated As Boolean
If Not IsActivated Then
IsActivated = True
Application.DoEvents() ' ...
Me.OpenFileDialog1.ShowDialog()
End If
End Sub
///

In .NET 2.0 Windows Forms forms have a 'Shown' event.
 
Herfried,

Although it is more work, do I like the method Jay shows more.

I am in doubt what I will show next time, when this question is asked.
The way you show it is understandable for everybody, Jays is in my opinion
better.

:-)

Cor
 
Herfried,
Why the DoEvents? I didn't notice that we needed it, hence I left it out. Am
I missing something?

Its nice to hear about the .NET 2.0 Form.Shown event. I missed it when I was
looking thru the "what's new"...

Jay

| > Can anybody tell me the event I should use if I want a OpenFileDialog to
| > open as soon as a from has displayed? I tried putting it in the forms
| > OnLoad but this means that the form isn't fully displayed until the
| > OpenFileDialog is shutdown.
|
| \\\
| Private Sub Form1_Activated( _
| ByVal sender As Object, _
| ByVal e As EventArgs _
| ) Handles MyBase.Activated
| Static IsActivated As Boolean
| If Not IsActivated Then
| IsActivated = True
| Application.DoEvents() ' ...
| Me.OpenFileDialog1.ShowDialog()
| End If
| End Sub
| ///
|
| In .NET 2.0 Windows Forms forms have a 'Shown' event.
|
| --
| M S Herfried K. Wagner
| M V P <URL:http://dotnet.mvps.org/>
| V B <URL:http://classicvb.org/petition/>
|
 
Cor,
FWIW: Herfried's example is almost how my example started out, I had a
Private field instead of a Static variable. Then I thought it would be just
as easy to use AddHandler & RemoveHandler to control the event being called
only once. Ergo the version I posted.

I think its good Herfried showed the alternate method (a field instead of
Add/Remove handler)...

Jay

| Herfried,
|
| Although it is more work, do I like the method Jay shows more.
|
| I am in doubt what I will show next time, when this question is asked.
| The way you show it is understandable for everybody, Jays is in my opinion
| better.
|
| :-)
|
| Cor
|
|
 
Jay,

Jay B. Harlow said:
Why the DoEvents? I didn't notice that we needed it, hence I left it out.
Am
I missing something?

No, you are not missing something. The 'DoEvents' can be removed.
 
Back
Top