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.
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
you can also do this:
Util.RawHtml("<script>alert('hello world');</script>").Dump();
below code works for me, I am on LINQPad 7.
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
© 2022 - 2024 — McMap. All rights reserved.