The code snippet included below allows quick and easy control over Form titles for message boxes and input boxes and basically any other Windows Form title. When working as a team, trying to remember what other members are using for an error message or for missing information or any other normal message box or input box gets crazy. Even when working on a project alone, trying to remember the wording you used for "Missing Data" takes time to search in an effort to have the program look uniform.
While creating a project that has about 1,000 various popup message boxes and input boxes, I was looking for a method to control what would be used for tTitles on Forms like message box and input box. When left up to the individual, we tended to get basically anything. For a general program error, we were getting "Program Error", "Error", "Prgm Er", "Action Not Complete", "Action Not Done", etc. etc. etc!
So, I wanted to write a method that would allow simple retrieval of values that the team agreed to and could stick with. Instead of creating a table or an XML doc to hold these values, it was easier just to place the values in an enum and create a method of getting the enum name, not value, and return that string to the calling code.
We used an "_" to separate the words, because the can be no spaces in an enum, and then simply used a Replace to insert a " ", so the title would read like normal text. The values of the enum does not matter, so no need to associate any values with the enum, or even what order they are put in the enum.
Using the Code
To use the following code, create a module and name it what you will. We named our module
MsgTitles. We only have the
MsgTitles enum and the
GetTitle method in the module for ease of use.
Save the module in your project. Add code for a message box, for example, and when prompted by intellisense for a title, type "GetTitle(". You will be prompted by intellisense for any of your
MsgTitles enums. You can then just start typing any of the enums to fill in your code. Close off the
GetTitle with a ")" and finish off your message box.
When the message box is called in code, the
GetTitle method will get the name of the enum, turn it into a string, and replace the "_" characters with " ".
Public Enum MsgTitles
Public Function GetTitle(ByVal Title As MsgTitles) As String
Return Replace([Enum].GetName(GetType(MsgTitles), Title), "_", " ").ToString
MsgBox("Would you like to control the TITLES that your programs use??" + &_
" Help your team use the SAME WORDS!!", MsgBoxStyle.Critical + _
Dim newDesc As String = InputBox("Enter an description.", _
Points of Interest
This function could be used for nearly any re-occurring string needed for any form. Was thinking about adding a routine to check that when a form opened up that it had a title and not a blank caption, and if it was blank, to pull a generic form title from the enum.
Please feel free to comment, or add other ideas of how to use!
Just created - No updates.