Messagebox.Show from a background application [duplicate] - c#

I have some code that popups a message box:
MessageBox.Show(this,
"You have not inputted a username or password. Would you like to configure your settings now?",
"Settings Needed",
MessageBoxButtons.YesNo,
MessageBoxIcon.Question);
My problem is when this pops up my app is usually minimized to the tray. As a result the messagebox doesn't come to the front and it also doesnt appear along the start bar. The only way of seeing it is by alt-tabbing.
Here is the code that minimizes my app (the parent) to the tray:
if (FormWindowState.Minimized == WindowState)
{
Hide();
}

There's an additional flag you can specify as an option to the standard Windows MessageBox function that isn't exposed in the WinForms wrapper.
What you're looking for is called MB_TOPMOST, which ensures that the message box is displayed as the top-most window over everything else on your desktop. Simply amend your code as shown below:
MessageBox.Show(this,
"You have not inputted a username or password. Would you like to configure your settings now?",
"Settings Needed",
MessageBoxButtons.YesNo,
MessageBoxIcon.Question,
MessageBoxDefaultButton.Button1, // specify "Yes" as the default
(MessageBoxOptions)0x40000); // specify MB_TOPMOST

you can try like this
MessageBox.Show(new Form() { TopMost = true }, "You have not inputted a username or password. Would you like to configure your settings now?",
"Settings Needed",
MessageBoxButtons.YesNo,
MessageBoxIcon.Question);

I only needed this for testing, so if you don't mind being extra cheesy,
MessageBoxOptions.ServiceNotification will do the trick...
MessageBox.Show(message,
"Error",
MessageBoxButton.YesNo,
MessageBoxImage.Exclamation,
MessageBoxResult.OK,
MessageBoxOptions.ServiceNotification);

MessageBox on top of all windows (no tray icon):
MessageBox.Show(new Form() { TopMost = true }, boxText, "Box Title",
MessageBoxButtons.OK, boxIcon);
MessageBox and your app on top of all windows (no tray icon):
TopMost = true;
MessageBox.Show(boxText, "Box Title", MessageBoxButtons.OK, boxIcon);
TopMost = false;
MessageBox on top of all windows, plus tray icon (app loses focus):
MessageBox.Show(boxText, "Box Title", MessageBoxButtons.OK, boxIcon, 0,
MessageBoxOptions.DefaultDesktopOnly);
// (The "0" can also be "MessageBoxDefaultButton.Button1".)
MessageBoxButtons.OK and boxIcon are optional arguments in the first two.
Setting TopLevel doesn't do anthing; it is already true.
There is no direct way to center a MessageBox on its parent form. (Except maybe centering the parent form.)

The more correct way to do this is to set the owner of the MessageBox

Related

Message Box hidden in task bar in win form

I have a win form containing only progress bar and
logic for opening and closing an excel or word document using interopt dll.
This win form runs when I click a button in my web application.
The problem is, the win form after closing document displays a Message Box.
This message box always remains in the taskbar instead of Showing up in the
screen.
PS:
DialogResult Result = MessageBox.Show("Save","Confirm", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);
When I give the message box in this format, the message box always remains in the taskbar. But the functionalities given as properties for the message box works fine.
DialogResult Result = MessageBox.Show("Save","Confirm", MessageBoxButtons.YesNoCancel,MessageBoxOptions.DefaultDesktopOnly, MessageBoxIcon.Question);
When I give the message box in this format, the message box does not remains in the taskbar
but the variable "Result" always returns "No".
Thanks in Advance
Try the below:
DialogResult Secondpopups = MessageBox.Show(new Form() { TopMost = true },"Message u need to diaplay", "Title of the Message", MessageBoxButtons.OK, MessageBoxIcon.Information);

WPF- MessageBox to be top most

I have a WPF application, i need the MessageBox to always be top most.
in win forms i would do something like that:
System.Windows.Forms.MessageBox.Show(new Form() { TopMost = true }, "sure you wanna save?", "confirm", MessageBoxButtons.YesNo)
but how do i do that in WPF?
i saw a few different answers but non of them work for me e.g:
MessageBox.Show(Application.Current.MainWindow, "Im always on top - of the main window");
My mainWindo is null.
In my application the MessageBox is opening from different pages- not windows
Any idea how i do it in the most simple way?
Use MessageBoxOptions.DefaultDesktopOnly and it keeps the MessageBox on top of the window.
MessageBox.Show("You entered an incorrect value. Try once more.", "Wrong input", MessageBoxButton.OK, MessageBoxImage.Exclamation,MessageBoxResult.OK,MessageBoxOptions.DefaultDesktopOnly);
this.Dispatcher.Invoke((Action)(() =>
{
MessageBox.Show("Im always on top - of the main window");
}));
This will run it in the UI thread.Throw this in the method that shows the msg box.
Cheers,
G
You will have to create your own Window and set its Topmost value to true.
MyWindow dialog = new MyWindow();
dialog.Topmost = true;
dialog.Show();

messagebox on top of splashscreen

I have a messagebox that pops-up when a user can't be loaded (in this case because he don't have a warehouse) while loading there is a splashscreen that shows that the data is being loaded.
I tried the TopMost set to true, but yeah the spalshscreen isn't the parent so it don't work, so i tried TopLevel set to true but it didn't do the trick.
So i tried:
MessageBox.Show(Splashscreen.LoadingScreen.ActiveForm, e.Message, "No warehouses", MessageBoxButtons.OK, MessageBoxIcon.Error);
but this is cross thread so I get an: InvalidOperationException
So is there another way to set the messagebox on top?
Try this it will show your MessageBox at the top of every window currently open.
MessageBox.Show(this,
"Your text",
"Settings Needed",
MessageBoxButtons.YesNo,
MessageBoxIcon.Question,
MessageBoxDefaultButton.Button1,
(MessageBoxOptions)0x40000); // this is MB_TOPMOST flag
This will keep the message box on top of every window because we are passing MB_TOPMOST Value to MessageBoxOptions parameter. You can visit this for more information.
I faced similar issue - my MessageBox get hide behind of SplashScreen. Neither use of "this" adwiced here, nor "new Form" works for my WPF application. However, the construction
MessageBox.Show(msg, errType, MessageBoxButton.OK,
MessageBoxImage.Asterisk, reply, MessageBoxOptions.ServiceNotification);
with MessageBoxOptions.ServiceNotification helps, putting MessageBox on top of SplashScreen.

DialogResult in WPF Application

I am currently developing an application in C# using WPF, I have always only used WinForms. Normally if I want to ask the user a question instead of making my own dialogue I use
DialogResult result = MessageBox.Show(
"My Message Question", "My Title", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
This is the first time that I have used a WPF form and DialogResult does not seem to be available. What do I use to get the same effect?
Here is how you do the same in WPF:
MessageBoxResult result = MessageBox.Show("My Message Question", "My Title", MessageBoxButton.YesNo, MessageBoxImage.Question);
if (result == MessageBoxResult.Yes)
{
// Do this
}
Use MessageBoxResult instead. And use the MessageBox class. But this message box will look pretty ugly "classic" style.
Another option would be to use Extended WPF toolkit
Yet another option would be to go here and download CrossTechnologySamples.exe then look into the VistaBridge project. I recommend you give a good look here because you will find other samples for other dialogs (like FileOpen, FileSave etc.) that do not exist by default in WPF.

How do I create a MessageBox in C#?

I have just installed C# for the first time, and at first glance it appears to be very similar to VB6. I decided to start off by trying to make a 'Hello, World!' UI Edition.
I started in the Form Designer and made a button named "Click Me!" proceeded to double-click it and typed in
MessageBox("Hello, World!");
I received the following error:
MessageBox is a 'type' but used as a 'variable'
Fair enough, it seems in C# MessageBox is an Object. I tried the following
MessageBox a = new MessageBox("Hello, World!");
I received the following error:
MessageBox does not contain a constructor that takes '1' arguments
Now I am stumped. Please help.
MessageBox.Show also returns a DialogResult, which if you put some buttons on there, means you can have it returned what the user clicked. Most of the time I write something like
if (MessageBox.Show("Do you want to continue?", "Question", MessageBoxButtons.YesNo) == MessageBoxResult.Yes) {
//some interesting behaviour here
}
which I guess is a bit unwieldy but it gets the job done.
See https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.dialogresult for additional enum options you can use here.
Code summary:
using System.Windows.Forms;
...
MessageBox.Show( "hello world" );
Also (as per this other stack post): In Visual Studio expand the project in Solution Tree, right click on References, Add Reference, Select System.Windows.Forms on Framework tab. This will get the MessageBox working in conjunction with the using System.Windows.Forms reference from above.
It is a static function on the MessageBox class, the simple way to do this is using
MessageBox.Show("my message");
in the System.Windows.Forms class. You can find more on the msdn page for this here . Among other things you can control the message box text, title, default button, and icons. Since you didn't specify, if you are trying to do this in a webpage you should look at triggering the javascript alert("my message"); or confirm("my question"); functions.
Try below code:
MessageBox.Show("Test Information Message", "Caption", MessageBoxButtons.OK, MessageBoxIcon.Information);
Also you can use a MessageBox with OKCancel options, but it requires many codes.
The if block is for OK, the else block is for Cancel. Here is the code:
if (MessageBox.Show("Are you sure you want to do this?", "Question", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
{
MessageBox.Show("You pressed OK!");
}
else
{
MessageBox.Show("You pressed Cancel!");
}
You can also use a MessageBox with YesNo options:
if (MessageBox.Show("Are you sure want to doing this?", "Question", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
MessageBox.Show("You are pressed Yes!");
}
else
{
MessageBox.Show("You are pressed No!");
}
In the System.Windows.Forms class, you can find more on the MSDN page for this here. Among other things you can control the message box text, title, default button, and icons. Since you didn't specify, if you are trying to do this in a webpage you should look at triggering the javascript alert("my message"); or confirm("my question"); functions.
This is some of the things you can put into a message box. Enjoy
MessageBox.Show("Enter the text for the message box",
"Enter the name of the message box",
(Enter the button names e.g. MessageBoxButtons.YesNo),
(Enter the icon e.g. MessageBoxIcon.Question),
(Enter the default button e.g. MessageBoxDefaultButton.Button1)
More information can be found here
I got the same error 'System.Windows.Forms.MessageBox' is a 'type' but is used like a 'variable', even if using:
MessageBox.Show("Hello, World!");
I guess my initial attempts with invalid syntax caused some kind of bug and I ended up fixing it by adding a space between "MessageBox.Show" and the brackets ():
MessageBox.Show ("Hello, World!");
Now using the original syntax without the extra space works again:
MessageBox.Show("Hello, World!");

Categories

Resources