Thursday, August 13, 2009

VB.NET MsgBox, adding extra buttons

I have heard a lot of people ask how they can place custom buttons on a VB.NET msgBox, you know, "YES", "NO" and "WELL, MAYBE!". Often you'll see yes, no, cancel with directions on what each response means, but that just isn't what the programmer wants.

The fact of the matter is that there is no practicle way to change up the msgbox to do your bidding! Your best bet is to create your own msgbox.

I have used 2 techniques; First is to set up my own newmsgbox class to accept an array of commands. When my newmsgbox class is 'called' it dynamically creates the necessary buttons and reports back the button chosen when clicked.

Second technique is to use a static newmsgbox class with the amount of buttons already chosen. Now I can set up an enumeration for the response, I can set default text and actions for the buttons, and rename the buttons in the call, as follows:

Dim mr As MyMsgResult

mr = MyMsgBox.ShowMyMsg("msgbox Message" , "button1 text", "button2 text", "button3 text", "Title").response
'Where message is required, btnText and Title optional.

I too wish it was not this difficult to add an extra button, but I assume we are suppost to ask ourselves whether or not we actually still want to use a msgbox if we're adding so many options.

Happy coding.