Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hide a .NET MessageBox' Taskbar Icon

Is it possible to call the static MessageBox class Show() method in a way that it does not have a taskbar icon, or has a custom image? I'm trying to find an alternative to constructing custom MessageBox class.

Thanks.

I tried to the the DefaultDesktopOnly option in the following way:

if (MessageBox.Show("Are you sure you would like to do something?", "Title", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly) == DialogResult.Yes)
{
    //Do stuff
}

However there was still an icon in the taskbar and also the main form started crashing as well. I'm in Win7 if it matters. Are there stability issues with DefaultDesktopOnly?

like image 534
kmarks2 Avatar asked Dec 15 '22 03:12

kmarks2


1 Answers

You need to give the MessageBox an owner window that has (or not) an icon of itself for the dialog to NOT to show on its own. If you call the MessageBox from an open form, you can pass the form as the first parameter to make it its owner:

// Assume "this" is a form, not valid from any other class
if (MessageBox.Show(this, "Are you sure you would like to do something?", "Title", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly) == DialogResult.Yes)
{
    //Do stuff
}

But if your program has no other GUI visible at the moment, you may simply create a dummy form just for the sake of providing it an owner, like so:

// A new, invisible form is created as the MessageBox owner, this prevents it from appearing in the taskbar
if (MessageBox.Show(new Form(), "Are you sure you would like to do something?", "Title", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly) == DialogResult.Yes)
{
    //Do stuff
}
like image 113
Alejandro Avatar answered Dec 31 '22 21:12

Alejandro