John S
John S

Reputation: 8331

How to create a non-modal/locking message box on a vb Winform?

I have inherited some really messy VB.Net code that I have to change a few things on and am not sure how to go about it:

There is a form that that displays a Browser.WebBrowser1 window and then a MessageBox with Yes/No buttons is popped from the main form. When this message box pops it is modal and the end user cannot browse the data shown in the Browser window.

How can I accomplish the same thing in a non-modal way?

The Messagebox pop code:

  resultYESNO = MessageBox.Show(Me, questionText, "DisputeHandler Question", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
                If resultYESNO = DialogResult.Yes Then
                    columnValue = "Y"
                ElseIf resultYESNO = DialogResult.No Then
                    columnValue = "N"
                End If

Upvotes: 1

Views: 4107

Answers (3)

Precious Uwhubetine
Precious Uwhubetine

Reputation: 3007

If you want to use a custom dialog box, then you just have to create another form, add the yes and no buttons and other controls. When showing the form(Custom MessageBox), use Form.ShowDialog(), replace Form with the name of your form. Using Form.ShowDialog disables interaction with the main form until the custom message box is closed.

Upvotes: 0

jmcilhinney
jmcilhinney

Reputation: 54417

MessageBox.Show always displays a modal dialogue. If you don't want a modal dialogue then you need to create your own form with the appropriate controls on it and then you can display it by calling Show.

If you pass the current form as the owner, i.e. call Show(Me), then you will create a genuine modeless dialogue, i.e. one that does not prevent access to the caller but does remain on top of it and will minimise, restore and close with the caller.

As the dialogue is not modal, you cannot place code you want executed when it closes immediately after the code to display it. You would have to handle the FormClosed event and put your code there. You'll have to set the DialogResult property of the form to the appropriate value on the Click of each Button.

Upvotes: 1

tgolisch
tgolisch

Reputation: 6744

Instead of using a modal/locking messagebox, you could just use a panel control with a label on it (to display the warning message). Keep the panel hidden (visible=false) until you want to display a message.

Upvotes: 1

Related Questions