Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Titles - Easy Control Over MessageBox, InputBox, and Form Titles

0.00/5 (No votes)
17 Nov 2009 1  
Simple enum and method to set MessageBox, InputBox, and Form titles.

Introduction

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.

Background

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 " ".

' 
' Vb.NET code  - Cope All into Module
' Here is a list of generally used Titles for InputBox and MsgBox
' Add any Title you want to make available to your programmers
' Ex of Title: Please Input Your Name = Please_Input_Your_Name
Public Enum MsgTitles
    Action_Not_Permitted
    Action_Complete
    Verify
    Message
    Additonal_Info_Needed
    Program_Error
    Need_a_Name
    Need_an_Amount
    Need_a_Date
    Need_a_Contact
    Data_Saved
    Data_Deleted
    Data_Added
    Warning
    Empty_Folder
    Dont_let_soem_one_Tipe_sumthin_lik_thes
End Enum
'Function to Get Enum Name String and remove "_" and replace with " " 

Public Function GetTitle(ByVal Title As MsgTitles) As String
    Return Replace([Enum].GetName(GetType(MsgTitles), Title), "_", " ").ToString
End Function

'-----------------------------------------------------
        
'To use in a form just place a normal msgbox like the following in your code

MsgBox("Would you like to control the TITLES that your programs use??" + &_
       " Help your team use the SAME WORDS!!", MsgBoxStyle.Critical + _
       MsgBoxStyle.OkOnly, GetTitle(MsgTitles.Dont_let_soem_one_Tipe_sumthin_lik_thes))
 or 
Dim newDesc As String = InputBox("Enter an description.", _
                        GetTitle(MsgTitles.Additonal_Info_Needed), "")

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!

History

Just created - No updates.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here