Search code examples
vb.netwinformsmultithreadingmessagebox

Making a form halt other program activity like a messagebox (in VB.NET)


I want to create a custom form (in visual basic .NET) that will stop other process responsiveness until the form is acknowledged. It would be a nice bonus if I can add a beep when trying to access the main program UI while this form is displayed as well (like how a messagebox does).

My initial idea was to create another thread for the messagebox-type form and have the main thread sleep until the messagebox-type form is responded too, however I think this would create a bug-like appearance on the main program as it simply wouldnt respond or update its UI (also worth noting, I have little experience working with multithreading, so this may be incorrect).

I really don't have much idea of how to proceed with this, any ideas and/or guidance are greatly appreciated. thank you! :)


Solution

  • I think that this type of behavior is that for which there is ShowDialog()

    this sample is on the MSDN page for ShowDialog()

    Public Sub ShowMyDialogBox()
        Dim testDialog As New Form2()
    
        ' Show testDialog as a modal dialog and determine if DialogResult = OK.
        If testDialog.ShowDialog(Me) = System.Windows.Forms.DialogResult.OK Then
            ' Read the contents of testDialog's TextBox.
            txtResult.Text = testDialog.TextBox1.Text
        Else
            txtResult.Text = "Cancelled"
        End If
        testDialog.Dispose()
    End Sub 'ShowMyDialogBox
    

    When the code calls ShowDialog() the program continue on the testDialog and exits only when your user press OK, Cancel, Close or whatever method, which, in the called dialog, set the property DialogResult to any value different from DialogResult.None.