Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

.ShowDialog

Status
Not open for further replies.

SiJP

Programmer
May 8, 2002
708
GB
I'm having a problem displaying a Modal form. Using the code below, the form I intend to open as modal seems to open then close immediately.

frmMain is created as follows:
Code:
Public Shared oMainForm As New frmMain 'in class module

Sub Main(ByVal cmdArgs() As String)
    oMainForm.Show()
    Application.Run()
End Sub

frmMain has a menu item that has code as follows, to open frmDialog:

Code:
    Dim oForm As New frmDialog
    oForm.ShowDialog()

Why, using the above code, does frmDialog open the close immediately?

Thanks,

------------------------
Hit any User to continue
 
Try application.doevents instead of application.run


Sweep
...if it works dont mess with it
 
Cheers sweep. When I change Application.Run to Application.Doevents, the application shuts down... thus I don't even get the chance to invoke the frmDialog!

Any clue's as to why?

------------------------
Hit any User to continue
 
Thanks for the suggestion Rick, unfortunately this is not what I need.

The main form (frmMain) is opened as a modal form by the entry point to the application (Sub Main). The second, modal dialog form is only opened when a certain business logic is hit (e.g. a user enters the first part of a name that is not found as a result of a lookup...).

Thus I will not open this form from the Sub Main.

Mastakilla - again, thanks for the contribution. Commenting out the Application.Run line will mean the applciation will terminate after running any code in the Sub Main.

Am I invoking ShowDialog in the wrong way? Is it acting by design?

Thanks.



------------------------
Hit any User to continue
 
Im not sure the first form show is needed.
My main form opens as yours does, and the first thing it does is to pop up a modal login form as such

Code:
 Private Sub frmMain_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load

'Get any Command Line Parameters!
Dim cmdParameters As String = Microsoft.VisualBasic.Command
Me.WindowState = FormWindowState.Maximized
Application.DoEvents()

'Login form
Dim f As New frmLogin
If f.ShowDialog = DialogResult.No Then Exit Sub
If Not CurrentUser.bUserAllowed Then
      MsgBox("Access not Permitted - User Revoked, Contact IT")
      Exit Sub
End If

End Sub


Sweep
...if it works dont mess with it
 
Hi Sweep.

I'm not sure what is meant by not needing the first form.show. If I dont have that there, how to I open the main form!?

I tried a bit of code in terms of:

Dim f As New frmDialog
Dim lret As Int32 = f.ShowDialog

I get the same result. The frm closes down.
I also tried putting Application.DoEvents after attempting to open the frmDialog - to no avail.

Is this anoying or what!?

------------------------
Hit any User to continue
 
I have frmMain as my startup object.
How are you opening your Main Form?

Sweep
...if it works dont mess with it
 
As per the code in the first post:

Code:
Public Shared oMainForm As New frmMain 'in class module
Sub Main(ByVal cmdArgs() As String)
    oMainForm.Show()
    Application.Run()
End Sub

:)

------------------------
Hit any User to continue
 
What else are you doing in this class module that you couldnt move to the the main forms load event?
The load event in my main form does lots more than I posted, I just cut it out for clarity.

In your form main, try adding a new custom method called zShow and in there simply add 2 lines..

Me.show
application.doevents

then instead of calling oMainForm.Show, call oMainForm.zShow.






Sweep
...if it works dont mess with it
 
Like you, I have a lot more that goes on under the surface. This is what typically happens when my application starts:

a) check the cmd line parameters. If none are specified, then open the main form. If a 'silent' switch is specified, do not open any forms.

b) If the main form is to be opened, firstly open a splash screen, load all the object data from registry and sql server's, close the splash screen then open the main form.

I'll see if I can test on a blank new app.. see what is different!

------------------------
Hit any User to continue
 
Thanks for the suggestion Rick, unfortunately this is not what I need.

The main form (frmMain) is opened as a modal form by the entry point to the application (Sub Main). The second, modal dialog form is only opened when a certain business logic is hit (e.g. a user enters the first part of a name that is not found as a result of a lookup...).

Thus I will not open this form from the Sub Main.

I'm failing to see the problem. Open the MainForm modally from the sub main, that will halt the progress of the code in Sub Main until frmMain is closed. On frmMain when the user hits the lookup button, open the frmDialog modally.

And for the record, I rank DoEvents right up next to data binding from the DB to the GUI.

-Rick

VB.Net Forum forum796 forum855 ASP.NET Forum
[monkey]I believe in killer coding ninja monkeys.[monkey]
 
Hi Rick,

ThatRickGuy said:
Open the MainForm modally from the sub main, that will halt the progress of the code in Sub Main until frmMain is closed. On frmMain when the user hits the lookup button, open the frmDialog modally.

Opening the main form modally is something I hadn't considered, but I have just tested this and I still get the problem where frmDialog opens for a split second then closes.

(One thing I did notice, was that when I changed the Sub Main to start the m_ObjMainForm.ShowDialog(), and then I stepped throught the code, was that the code did not halt in anticipation of the form closing.. is that incorrect behaviour?)

Sorry if i haven't been clear - I do appreciate your interest and help with this.

(I'm missing the pun here, but whats up with DoEvents/ databinding from DB to GUI?.....!)

Cheers! :)

------------------------
Hit any User to continue
 
Code executes in a structured top-down method until another thread is launched. Using form.show will launch that form in a new thread, which means the initial thread will continue on. Using form.showmodal will launch that form in the same thread, so the initial code block that called the form.showmodal will halt until the form is closed. For example:

Code:
Public Sub Main
  dim f as new form
  f.show
  msgbox("All done!")
end sub

Code:
Public Sub Main
  dim f as new form
  f.showdialog
  msgbox ("all done!")
end sub

The first example will show the form and a message box will pop up, after clicking the pop up, the application will end. The second example will show the form, and when you close the form, the message box will show up.

If a form opens and closes right away, it means that it is going out of scope. One of the common causes of this is using form.show in Sub Main. since Form.Show doesn't stop the Sub Main code from executing, and Sub Main is the primary thread, when Sub Main completes, the application will end.

(I'm missing the pun here, but whats up with DoEvents/ databinding from DB to GUI?.....!)

Just some pet peeves of mine, I don't like seeing data from the database bound directly to the UI, and using DoEvents. DoEvents is a crutch of the poor design of VB6, unfortunatly some of the same threading issue still exist in .Net. There are ways arround it that are much more elegant and effective, but require extra work and a familiarity with threading.

-Rick

VB.Net Forum forum796 forum855 ASP.NET Forum
[monkey]I believe in killer coding ninja monkeys.[monkey]
 
I created a new project, and literally had minimal objects in it. Firstly I tried the simple Sub Main code from Rick.

This works as expected (i.e. Forms open and remain open).

Secondly, I tried to include a two new forms. One is opened (ShowDialog) from sub main, the second opened from an event fired on the first form (button click). The same behavior is displayed as my current issue (opens/closes).

I think this is majorily f*cked up - but I've got a work around.

Basically, with or without the sub main having form.showdialog code, the final 'open my fricking dialog form' code reads as follows:

Code:
        [COLOR=blue]Try[/color]
            [COLOR=blue]Dim[/color] f [COLOR=blue]As New[/color] frmTranslationTables
            f.ShowDialog() [COLOR=green]'Form opens then closes quickly.[/color]
            f.ShowDialog() [COLOR=green]'Bugger me, form now opens a second time, and remains open![/color]
        [COLOR=blue]Catch[/color] ex [COLOR=blue]As[/color] Exception
            MsgBox(ex.ToString)
        [COLOR=blue]End Try[/color]

Believe it or not, this works. Tres Weird, non?

------------------------
Hit any User to continue
 
What is the code in the constructor(new), on_load, visibility_changed, got_focus, resized, and other events that will fire?

Also, If you have
Code:
f.show
f.showdialog

It would likely behaive as your app is. But you posted that you are using .showdialog twice, I would recommend double checking that.

-Rick

VB.Net Forum forum796 forum855 ASP.NET Forum
[monkey]I believe in killer coding ninja monkeys.[monkey]
 
Still keeping tabs on this.. got a few other issues to sort out, but want to find a resolution, rather than a work around!

Thanks for your help all (esp. Rick)

....

------------------------
Hit any User to continue
 
SiJP --

You can't have your Sub Main call a form unless you use the .ShowDialog. I had this problem as well when I was upgrading a VB6 app to .net.

As a workaround, I created a new blank form for the project, and put all of the Sub Main contents after the InitializeComponent() part inside the #Region " Windows Form Designer generated code " section.

Once you do this, set this from as your startup object. Also be sure to place code within your actual main form to close this new form when it is shut down.

From there, you can set properties of this new form to be hidden at startup and not shown in the task bar. No one will ever know that you have an extra form in your application!

 
Hi ps2goat.

ps2goat said:
You can't have your Sub Main call a form unless you use the .ShowDialog

When you say 'call a form' I don't quite get you. In my proj I have a class that declares the variable 'oMainForm' as frmMain. I then use this in the Sub Main to show the form, as epr code at the start of this post: oMainForm.Show - this works.



------------------------
Hit any User to continue
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top