65.9K
CodeProject 正在变化。 阅读更多。
Home

标题 - 轻松控制 MessageBox、InputBox 和窗体标题

starIconstarIcon
emptyStarIcon
starIcon
emptyStarIconemptyStarIcon

2.57/5 (4投票s)

2009年11月17日

CPOL

3分钟阅读

viewsIcon

18105

一个简单的枚举和方法,用于设置 MessageBox、InputBox 和窗体标题。

引言

以下代码片段允许快速轻松地控制消息框和输入框以及基本上任何其他 Windows 窗体标题。当团队合作时,试图记住其他成员在错误消息或缺少信息或任何其他普通消息框或输入框中使用什么措辞会让人抓狂。即使在单独进行项目时,试图记住为“缺少数据”使用的措辞也需要花费时间搜索,以使程序看起来统一。

背景

在创建一个包含大约 1,000 个各种弹出消息框和输入框的项目时,我正在寻找一种方法来控制消息框和输入框等窗体上将使用的标题。如果由个人决定,我们往往会得到任何东西。对于一般的程序错误,我们得到“程序错误”、“错误”、“Prgm Er”、“操作未完成”、“操作未执行”等等!

因此,我希望编写一种方法,以便简单地检索团队同意并可以坚持使用的值。与其创建一个表或 XML 文档来保存这些值,不如将这些值放在一个枚举中,并创建一个获取枚举名称(而不是值)的方法,并将该字符串返回给调用代码,这更容易。

我们使用 "_" 来分隔单词,因为枚举中不能有空格,然后简单地使用 Replace 来插入一个 " ",以便标题像普通文本一样显示。枚举的值并不重要,因此不需要将任何值与枚举关联,甚至不需要考虑枚举中值的顺序。

Using the Code

要使用以下代码,请创建一个模块并随意命名。我们将其模块命名为 MsgTitles。为了易于使用,该模块中只有 MsgTitles 枚举和 GetTitle 方法。

将模块保存在您的项目中。添加消息框的代码,例如,当 intellisense 提示输入标题时,键入 "GetTitle("。Intellisense 将提示您输入任何 MsgTitles 枚举。然后,您可以开始键入任何枚举来填充您的代码。用 ")" 关闭 GetTitle 并完成您的消息框。

当在代码中调用消息框时,GetTitle 方法将获取枚举的名称,将其转换为字符串,并将 "_" 字符替换为 " "。

' 
' 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), "")

关注点

此函数几乎可以用于任何需要为任何窗体重复使用的字符串。正在考虑添加一个例程来检查打开的窗体是否具有标题而不是空白标题,如果为空白,则从枚举中提取通用的窗体标题。

请随时评论或添加其他使用方法!

历史

刚刚创建 - 没有更新。

© . All rights reserved.