How can you display a MessageBox dialog in a LinqPad query?
Asked Answered
G

3

9

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.

Greenroom answered 12/8, 2015 at 21:22 Comment(3)
It shouldn't be blocked. Does the following work for you? share.linqpad.net/942ift.linqShareeshareholder
Another option is to use Console.ReadLine() or Util.ReadLine()Shareeshareholder
If the answer is useful, please mark it as an answer.Slovenly
S
7

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

Slovenly answered 30/1, 2017 at 14:20 Comment(0)
E
3

you can also do this:

Util.RawHtml("<script>alert('hello world');</script>").Dump();
Errhine answered 27/9, 2021 at 5:17 Comment(0)
L
3

below code works for me, I am on LINQPad 7.

enter image description here

the 'issue' I have is when I run LINQpad on my 2nd monitor, the popup dialog always shows on my main monitor. (feels like it's blocked, when I forgot it)

the code is copied from https://mcmap.net/q/166488/-is-there-a-builtin-confirmation-dialog-in-windows-forms

Lithia answered 29/8, 2022 at 21:47 Comment(1)
It only works because the .NET System.Windows.Forms.dll is already referenced, then either the fully qualified name should be mentioned, as System.Windows.Forms.MessageBox.Show (also for MessageBoxButtons), or the namespace should be added in "Query Properties" window (by hitting F4) => "Namespace Imports" tab. You can go to the "Advanced" tab, and click on "Show assembly resolution log", which will generate an html report and shows which dlls are already referenced.Ginaginder

© 2022 - 2024 — McMap. All rights reserved.