Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can you display a MessageBox dialog in a LinqPad query?

Tags:

linqpad

When running/debugging, calls to MessageBox, or Microsoft.VisualBasic.Interaction.MsgBox hang without showing any dialog. I think it's being blocked, but see no examples on how to resolve this.

like image 858
WhiskerBiscuit Avatar asked Aug 12 '15 21:08

WhiskerBiscuit


Video Answer


1 Answers

I am providing you with step by step instructions, I hope it helps:

Preparation

You need to add a reference by pressing F4 in the LinqPad editor. The query property dialog opens.

There, use Add... to add the assembly Microsoft.VisualBasic.dll and switch to the tab Additional Namespace Imports. Type Microsoft.VisualBasic and close the dialog by clicking OK.

Note: Although we are using the DLL from VisualBasic, we can utilize it in C# without any limitations. Since it is compiled in .NET it can be used by any .NET language. So the instructions in this answer apply to C#, but in the same way one could use it in VB.NET (just use the language dropdown in Linqpad to make your choice).


General usage

Finally, you can use it as follows:

void Main()
{
    Interaction.MsgBox("Hello"); 
}

Note: The Microsoft.VisualBasic works with C# as well as with VB.NET, however this example is using the C# Program setting.

Be aware that if you're using multiple monitors, then the messagebox might appear on a different monitor and is not showing in the foreground, so you might not notice it.

I recommend that you force displaying it in the foreground this way:

Interaction.MsgBox("Hello", MsgBoxStyle.MsgBoxSetForeground);       

But you can't force it displaying on a specific monitor.


Buttons and other parameters

If you need to know more about the parameters, like title, buttons to display etc., you can look here at MSDN.

For example, to display an Abort, Retry, Ignore dialog with Retry as default button (DefaultButton2), you can use:

MsgBoxResult result =
    Interaction.MsgBox(Title: "Critical Error", Prompt: "Cannot read file",
        Buttons: MsgBoxStyle.MsgBoxSetForeground | MsgBoxStyle.AbortRetryIgnore     
                 | MsgBoxStyle.Critical | MsgBoxStyle.DefaultButton2);

Likewise, to set other buttons as default: Abort would be DefaultButton1 while Ignore would be DefaultButton3. You may only specify one of them, if you don't specify it, then DefaultButton1 is assumed.


Checking the result

After the user has clicked, you can query the variable result to find out which button was clicked (MsgBoxResult enumeration), i.e.

if (result==MsgBoxResult.Retry)
{
    Console.WriteLine("Retrying...");
}

Hint: In LinqPad you can also add aliases for namespaces. If you don't like to type Interaction.MsgBox each time, you can press F4, go to tab Additional Namespace Imports, enter there Dlg=Microsoft.VisualBasic.Interaction and close the dialog by clicking OK. Then you can use Dlg.MsgBox(...) instead of Interaction.MsgBox(...).


More LinqPad-related stuff in StackOverflow can be found here and there

like image 183
Matt Avatar answered Nov 15 '22 11:11

Matt