Question Form main and form settings - help

ColdIce

New member
Joined
Apr 29, 2011
Messages
1
Programming Experience
1-3
Hello!

I got two forms: frmMain and frmSettings.
Application startup is set to: frmMain.

I got the following code @ frmMain Load:
VB.NET:
       settings.DatabasePath = Application.StartupPath & "\"c & "application.db3"

        If (File.Exists(settings.DatabasePath)) Then
            LoadUserSettings()
        Else
            frmsettings.ShowDialog()
        End If

And this code for frmSettings Form Closing:
VB.NET:
     If (Not File.Exists(settings.DatabasePath)) Then
            If (MessageBox.Show("Are you sure you want to exit without saving?", "Exit?", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Information) = Windows.Forms.DialogResult.Yes) Then
                System.Windows.Forms.Application.Exit()
            Else
                e.Cancel = True
            End If
            frmmain.LoadUserSettings()
        End If

I'm not sure if this is the right way to do it. I want like to check if a file exist on main form, if not: force open settings form. And then when the user is closing the form with exit button = check if the file exist again. If it doesn't exist, close application.

It's a huge application and I need optimized on most parts.

Also, the settings form is asking the closing question two times
 
Regarding your "Also, the settings form is asking the closing question two times " The reason why the closing message happens twice is because the Exit() calls the closing event, so if you rewrote that part like the following it will only ask you once.

VB.NET:
        If (MessageBox.Show("Are you sure you want to exit without saving?", "Exit?", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Information) = Windows.Forms.DialogResult.Yes) Then
            e.Cancel = False
        Else
            e.Cancel = True
        End If

I'm not sure what your other question was or if there was another one? Hope that helps.



[EDIT]

please note you can also do it this way too

VB.NET:
        If Not (MessageBox.Show("Are you sure you want to exit without saving?", "Exit?", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Information) = Windows.Forms.DialogResult.Yes) Then
            e.Cancel = True
        End If
 
Last edited:
Back
Top