public ModelessDialog(IModelessDialogCallback parent)
{
this.parent = parent;
this.Text = "Modeless Dialog";
this.bOK = new Button();
this.bOK.Parent = this;
this.bOK.Bounds = new Rectangle (10, 10, 150, 30);
this.bOK.Text = "OK";
this.bOK.Click += new EventHandler(this._Click);
this.bCancel = new Button();
this.bCancel.Parent = this;
this.bCancel.Bounds = new Rectangle (10, 50, 150, 30);
this.bCancel.Text = "Cancel";
this.bCancel.Click += new EventHandler(this._Click);
}
private void _Click(object o, EventArgs e)
{
this.Hide();
this.parent.DialogResultCallback(o == this.bOK ? DialogResult.OK : DialogResult.Cancel);
}
protected override void OnClosing(CancelEventArgs e)
{
e.Cancel = true;
this.Hide();
this.parent.DialogResultCallback(DialogResult.Cancel);
}
}
'VB
Imports System
Imports System.Drawing
Imports System.Windows.Forms
Imports System.Collections
Imports System.ComponentModel
Public Interface IModelessDialogCallback
Sub DialogResultCallback(result As DialogResult)
End Interface IModelessDialogCallback'
Public Class Test
Inherits System.Windows.Forms.Form
Implements IModelessDialogCallback
Private dlg As ModelessDialog
Private bShow As Button
Private counter As Integer = 0
Protected Overrides Sub OnLoad(e As EventArgs)
Me.Text = "Modal(less) Dialog Example"
Me.bShow = New Button()
Me.bShow.Parent = Me
Me.bShow.Bounds = New Rectangle(10, 10, 150, 30)
Me.bShow.Text = "Show Dialog"
AddHandler Me.bShow.Click, AddressOf Me._Click
Me.dlg = New ModelessDialog(Me)
End Sub 'OnLoad
Private Sub _Click(o As Object, e As EventArgs)
Me.Enabled = False
Me.bShow.Text = "waiting for dlg"
dlg.Show()
End Sub '_Click
Public Sub DialogResultCallback(ByVal result As DialogResult) Implements _
IModelessDialogCallback.DialogResultCallback
MessageBox.Show(("dialog returned: " + IIf(result = DialogResult.OK, "OK", "Cancel")))
Me.Enabled = True
counter += 1
Me.bShow.Text = String.Format("Show Dialog: {0}", counter)
End Sub 'DialogResultCallback
Public Shared Sub Main()
Application.Run(New Test)
End Sub 'Main
End Class 'Test
Public Class ModelessDialog
Inherits Form
Private myParent As IModelessDialogCallback
Private bOK, bCancel As Button
Public Sub New(parent As IModelessDialogCallback)
Me.myParent = parent
Me.Text = "Modeless Dialog"
Me.bOK = New Button()
Me.bOK.Parent = Me
Me.bOK.Bounds = New Rectangle(10, 10, 150, 30)
Me.bOK.Text = "OK"
AddHandler Me.bOK.Click, AddressOf Me._Click