# MessageBox - looking for a better way

**URL:** <https://forums.powershell.org/t/messagebox-looking-for-a-better-way/17775>\
**Category:** PowerShell Help\
**Created:** [October 18, 2021, 10:48pm UTC](https://forums.powershell.org/t/messagebox-looking-for-a-better-way/17775 "2021-10-18T22:48:48Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![tonyd](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/tonyd/32/1041_2.png) [@tonyd](https://forums.powershell.org/u/tonyd)\
**Post date:** [October 18, 2021, 10:48pm UTC](https://forums.powershell.org/t/messagebox-looking-for-a-better-way/17775/1 "2021-10-18T22:48:48Z")

</div>

This is what I am currently doing to set the default button on a message box:

```auto
Add-Type -AssemblyName System.Windows.Forms
$msgBox = [Windows.Forms.MessageBox]
$buttonYes = [System.Windows.Forms.MessageboxDefaultButton]::Button1
$buttonNo = [System.Windows.Forms.MessageboxDefaultButton]::Button2
$buttonCancel = [System.Windows.Forms.MessageboxDefaultButton]::Button3

$msgBox::Show("Message To User.", 'Hello World', 'YesNo', 'Warning', $buttonNo)

```

There must be a better way. What I am hoping for is a way to simply reference the button by its position as in 1, 2, 3 etc, not having to hard code the button variable to a specific button in the enum. This method just seems quirky to me.

Any help is appreciated in advance. Thank you.

---

<div class="post-metadata">

**Author:** ![robert\_loblaw](https://avatars.discourse-cdn.com/v4/letter/r/f17d59/32.png) [@robert\_loblaw](https://forums.powershell.org/u/robert_loblaw)\
**Post date:** [October 19, 2021, 2:01pm UTC](https://forums.powershell.org/t/messagebox-looking-for-a-better-way/17775/2 "2021-10-19T14:01:43Z")

</div>

This can go all kinds of different directions. I would do this. Call the method when you need it. You can add arguments if you wish. I am keeping this super simple…

```auto
# Popup a window with a good message
Function Set-CompletePopup( )
    {
        [System.Windows.MessageBox]::Show("Something Completed", "Good to go", "OK", "Asterisk")
    }

```

---

<div class="post-metadata">

**Author:** ![tonyd](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/tonyd/32/1041_2.png) [@tonyd](https://forums.powershell.org/u/tonyd)\
**Post date:** [October 19, 2021, 4:25pm UTC](https://forums.powershell.org/t/messagebox-looking-for-a-better-way/17775/3 "2021-10-19T16:25:40Z")

</div>

> [@robert\_loblaw](#):
>
> ` [System.Windows.MessageBox]::Show("Something Completed", "Good to go", "OK", "Asterisk")`

Thanks Robert for the reply. I did not make myself clear enough. Sorry about that. I am looking for a better way to set the default button on the Message Box popup which would be the 5th argument to the Show Method. I am trying to avoid setting the variables to the Enum and for a better way to set those in the Show method. Make sense?

---

<div class="post-metadata">

**Author:** ![matt-bloomfield](https://avatars.discourse-cdn.com/v4/letter/m/dec6dc/32.png) [@matt-bloomfield](https://forums.powershell.org/u/matt-bloomfield)\
**Post date:** [October 19, 2021, 5:57pm UTC](https://forums.powershell.org/t/messagebox-looking-for-a-better-way/17775/4 "2021-10-19T17:57:28Z")

</div>

You can use the Button reference rather than your variable.

I took the liberty of declaring it as ‘YesNoCancel’ as I think that was your intention?

```auto
Add-Type -AssemblyName System.Windows.Forms
$msgBox = [Windows.Forms.MessageBox]
$buttonYes = [System.Windows.Forms.MessageboxDefaultButton]::Button1
$buttonNo = [System.Windows.Forms.MessageboxDefaultButton]::Button2
$buttonCancel = [System.Windows.Forms.MessageboxDefaultButton]::Button3

$msgBox::Show("Message To User.", 'Hello World', 'YesNoCancel', 'Warning', 'Button2')

```

---

<div class="post-metadata">

**Author:** ![tonyd](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/tonyd/32/1041_2.png) [@tonyd](https://forums.powershell.org/u/tonyd)\
**Post date:** [October 19, 2021, 6:06pm UTC](https://forums.powershell.org/t/messagebox-looking-for-a-better-way/17775/5 "2021-10-19T18:06:16Z")

</div>

> [@matt-bloomfield](#):
>
> `$msgBox::Show("Message To User.", 'Hello World', 'YesNoCancel', 'Warning', 'Button2')`

Wow, thanks Matt. I am pretty sure I tried everything BUT your suggestion. Feeling pretty stupid at this point. I do believe I tried this, but without the single quotes. My bad.

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:21pm UTC](https://forums.powershell.org/t/messagebox-looking-for-a-better-way/17775/6 "2024-05-16T20:21:10Z")

</div>


