Modal dialogs: probably an easy question!

lilalfyalien

Member
Joined
Dec 11, 2007
Messages
7
Programming Experience
3-5
I have a form which I want to make a modal form (so that users have to interact with this form before any others). But not just in the scope of my program but in the scope of the OS.

Here's my code so far:


MAIN FORM
VB.NET:
Public Class frmMain

    Dim result As System.Windows.Forms.DialogResult

    Private Sub frmMain_Activated(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        result = DialogForm.ShowDialog()

        If result = Windows.Forms.DialogResult.OK Then
            Me.Close()
        End If

    End Sub
End Class

DIALOG FORM




VB.NET:
Imports System.Windows.Forms

Public Class DialogForm


    Private Sub OK_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK_Button.Click
        Me.DialogResult = System.Windows.Forms.DialogResult.OK
        Me.Close()
    End Sub

    Private Sub Cancel_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cancel_Button.Click
        Me.DialogResult = System.Windows.Forms.DialogResult.Cancel
        Me.Close()
    End Sub

End Class


How can I get the effect I want? Is there a way to track the cursor position and stop it from moving around outside of the window or is there an easier way?

Many thanks,
 
Have a look at the TopMost property for a form.

Also, you can set the dialogue result at the form (IDE) level without having to create handlers for button clicks if all they do is set a dialogue result and close the form.

Set the button's dialogue result to OK (see button's properties tab in the IDE) for the OK button and Cancel for the Cancel button. Then on the form's properties set AcceptButton property to be your OK button and the CancelButton property to your Cancel button.

Doing this, you don't need any underlying code so your event handlers can be deleted to remove unneccesary clutter from your code window. Pressing the OK button will still close the form with a dialogue result of OK and likewise the Close button will close with a dialogue result of Cancel. You only need code if you want to do something else before the form closes, like save changes to database or cancel close if there was an error (trap form.closing event and set e.cancel = true).
 
Back
Top