msgbox not modal

Mike_etc

Member
Joined
Jun 7, 2020
Messages
7
Programming Experience
10+
I have been writing code in VB for 30+ years up thru VB6. I had not moved to VB.Net as I was close to retirement. I've been retired for more than 10 years now. For the fun of it I decided to improve myself by trying to move to VB.Net. Although I am comfortable with VB I am very much a newbe in VB.Net. I apologize if that shows in the simplicity of a problem I have run into.

I have been trying to re-write a program in VB.Net that works quite well in VB. My VB.Net program requires a text file that meets certain criteria to continue. In the form load event of my main form I use OpenFileDialog to allow the user to select a file. I use an IF Then_Else_End If structure to respond to the 2 dialog buttons. When Cancel is selected, I use MsgBox to offer the user the option of ending the program or selecting a file. If a file is selected I test that the file meets needed criteria. If the file fails the test I use MsgBox to offer the user the option of ending the program or selecting a new file. My problem is this, when I run the code and put in a breakpoint I can successfully step through all of the selection options with all of the message box dialogs properly firing. When I remove the breakpoint and run the code, none of the message boxes fire and the code seems to hang in a loop. I thought MsgBox was modal and the program should stop and wait for a user response when MsgBox is triggered. Any comments as to what may be happeninmg or any suggestions to how to fix the problem would be appreciated. I can provide my code but as I said I can successfully step through the code but I cannot run it automatically.
 
Last edited by a moderator:
Regarding what the OP is doing. Trying to display a dialog before the form is shown is a poor design model. If that where the case. Present your own dialog box as the first loaded form before loading your main form. That would actually be the correct thing to do, and with that result, you could then show your main form after you receive user input from your dialog.
 
I would not change the startup form (MainForm). If there is a precondition where it is pointless to start the application then Startup event can be cancelled, as explained in post 2.
Mind that there is a separation of application and the data the applications presents, it is most common for any application to start without data and after that let user load the data - either with menu system or for example automatically starting a dialog asking for data. Starting with a dialog, validation, and loading even before the main window is shown sounds very annoying to me. In addition the loading process could take some time and should most likely be done on a separate thread, I sometimes do this from Load event, but let the application initialize normally as quick as possible. Then data is presented when available and window is already shown, it makes the application appear much faster and responsive in startup.
 
Regarding what the OP is doing. Trying to display a dialog before the form is shown is a poor design model. If that where the case. Present your own dialog box as the first loaded form before loading your main form. That would actually be the correct thing to do, and with that result, you could then show your main form after you receive user input from your dialog.
It may be possible that you have also forgotten the fact that, in post #2, I specifically said that Load is the wrong choice in this particular case. I originally advised using Startup or Shown in this specific case. My comment about disagreeing with you was relating to NEVER using Load. I advocate for using the right tool for the job. Often times that is the Load event and sometimes it's not. If the OP were to handle Startup then they can still use the standard OpenFileDialog and not have waste time reinventing the wheel, and they can prevent the main form ever even being created if it turns out that they have no need to display it.
 
Back
Top