与验证和取消按钮的消息框 [英] Message box with Validate and Cancel buttons

查看:173
本文介绍了与验证和取消按钮的消息框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何能有两个按钮,验证并使用了MsgBox取消?

How can I have a msgbox with two buttons, Validate and Cancel?

推荐答案

不幸的是,标准的Win32 的MessageBox 功能不支持按键自定义标签。而且,由于VB.NET MSGBOX 函数是一个瘦包装上的原生功能,它不支持他们无论是。在pre定义的值是你所得到的,这意味着最好的你能做的就是像确定和取消,用文字解释说,OK的意思为继续验证。

Unfortunately, the standard Win32 MessageBox function does not support buttons with custom labels. And since the VB.NET MsgBox function is a thin wrapper over that native function, it doesn't support them either. The pre-defined values are all you get, meaning that the best you can do is something like "OK" and "Cancel", with text explaining that "OK" means to "proceed with validation".

值得一提的是,为的的,这是赞同的Windows风格指南建议的做法。事实上,它看起来像它仍然是。具体地,请注意例外以下一般规则:

It's worth noting that for years, this was the recommended practice endorsed by the Windows style guides. In fact, it looks like it still is. Specifically, note the exceptions to the following general rule:

使用积极的承诺,而不是通用标签按钮,是主要的指令具体的答复,如OK或Yes /否。用户应该能够通过读按钮文本理解的选项。
  例外:

Use positive commit buttons that are specific responses to the main instruction, instead of generic labels such as OK or Yes/No. Users should be able to understand the options by reading the button text alone.
Exceptions:

      
  • 使用关闭的没有设置,如信息对话框对话框。切勿使用关闭用于具有设置对话框。
  •   
  • 使用OK的时候,具体的反应仍然是通用,如保存,选择或选择。
  •   
  • 使用确定改变特定设置或设置的集合的时候。
  •   
  • 对于传统的对话框没有一个主要的指令,可以使用通用标签等确定。通常这样的对话框没有被设计为执行特定任务,preventing更具体的回应。
  •   
  • 在某些任务需要更多的思考和仔细阅读,为用户做出明智的决定。这通常是与确认的情况。 在这种情况下,你可以故意使用通用提交按钮标签强迫用户阅读主要说明书和prevent草率的决定。
  •   
  • Use Close for dialogs that don't have settings, such as informational dialogs. Never use Close for dialogs that have settings.
  • Use OK when the "specific" responses are still generic, such as Save, Select, or Choose.
  • Use OK when changing a specific setting or a collection of settings.
  • For legacy dialog boxes without a main instruction, you can use generic labels such as OK. Often such dialog boxes aren't designed to perform a specific task, preventing more specific responses.
  • Certain tasks require more thought and careful reading for users to make informed decisions. This is usually the case with confirmations. In such cases, you can purposely use generic commit button labels to force users to read the main instructions and prevent hasty decisions.


Windows Vista中引入了一个新的API旨在取代老化,无法对配置的MessageBox —这就是所谓的的 TaskDialog 。如果你运行的是Windows Vista或Windows 7,你毫无疑问已经看到了这个对话框整个Windows外壳使用。它呢,其实,允许您指定自定义名称为每个按钮,并提供了其他的自定义选项数组为好。但这种控制没有白吃的午餐。有两个问题与 TaskDialog 功能:


Windows Vista introduced a new API intended to replace the aging and impossible-to-configure MessageBox—it's called the TaskDialog. If you're running either Windows Vista or 7, you've undoubtedly seen this dialog used throughout the Windows shell. It does, in fact, allow you to specify custom names for each of the buttons, and provides an array of other customization options as well. But this kind of control doesn't come for free. There are two problems with the TaskDialog function:

  1. .NET框架不包括包装它的开箱。你需要要么编写自己的简单的包装,或下载的Windows API code包包括这样的包装。但是,这增加了一个额外的依赖到code—你必须决定是否值得与否

  1. The .NET Framework does not include a wrapper for it out of the box. You'll need to either write your own simple wrapper, or download the Windows API Code Pack that includes such a wrapper. But this adds an extra dependency to your code—you'll have to decide whether that's worth it or not.

由于该API是在Vista中只介绍,这意味着程序在Vista或更高版本上运行时,只能使用它。如果您还需要面向Windows XP(或2000),你的运气了。在code表现出 TaskDialog 将失败,你会需要包括一个备用程序来显示标准的MessageBox 。这当然让您立即回到开始的地方,没有自定义按钮标签,在这些旧操作系统的能力。同样,只有你自己才能决定,如果这是你的应用程序和/或部署方案中的显著的关注。

Because the API was only introduced in Vista, that means programs can only use it when running on Vista or later. If you still need to target Windows XP (or 2000), you're out of luck. The code to show a TaskDialog will fail, and you'll need to include a fallback routine to show a standard MessageBox. Which of course puts you right back where you started, without the ability to customize the button labels, on those legacy operating systems. Again, only you can decide if this is a significant concern for your application and/or deployment scenario.

另一种选择,用几代人的VB程序员,是一鼓作气你自己的小的MessageBox 式的形式。这并不是说辛辛苦​​苦打下一份简单的表格与图标/图像的地方,一些标题文本,以及所有你想要的按钮。既然你已经创建整个表单,你就可以自由地将其从code但是你想定制。

The other option, used by generations of VB programmers, is to bang up your own little MessageBox-style form. It's not that hard to lay out a simple form with a place for an icon/image, some caption text, and all the buttons you want. Since you've created the entire form, you'll be free to customize it from your code however you wish.

如果您的绝对的需要的Windows Vista之前的Windows版本中此功能,然后创建自己的消息框的形式是你唯一的选择。否则,我的的建议您采取新的 TaskDialog API的优势。在应用程序中这样做促进一致性与其他应用程序的用户很可能已经在计算机上安装,甚至与Windows本身。这是很难保证自己的自定义消息框的形式得到小小的感动吧,像自动包装取决于它的长度和用户的屏幕尺寸标签文本。而显示/隐藏在这取决于你的对话框中是否包含取消按钮的标题栏中的X关闭按钮。和其他的东西一个令人难以置信的数字,标准的Windows 的MessageBox / TaskDialog 为您做免费的,而你不必电梯一个手指。这是一般的原则,只是重申:绝不重新发明轮子,当你不绝对要做到这一点

If you absolutely need this functionality in versions of Windows prior to Vista, then creating your own message box form is about your only option. Otherwise, I highly recommend that you take advantage of the new TaskDialog API. Doing so within your application promotes consistency with other applications the user is likely to have installed on their computer, and even with Windows itself. It's difficult to ensure that your own custom message box form gets the little touches right, like automatically wrapping the label text depending on its length and the user's screen size. And showing/hiding the "X" close button in the title bar depending on whether your dialog includes a "Cancel" button. And an unbelievable number of other things that the standard Windows MessageBox/TaskDialog does for you for free without your having to lift a finger. This is just a restatement of the general principle: never re-invent the wheel when you don't absolutely have to do so.

一个很好的妥协可能是使用像这样 TaskDialog 包装/仿真器。在Vista和更高版本,其中原生 TaskDialog 的API,它会自动调用该函数。另外,它采用了标准的形式,试图模拟本地 TaskDialog API的行为。我写了一个类似的自定义类为我所用,但我从来没有抽时间去网上出版它。

A good compromise might be to use something like this TaskDialog wrapper/emulator. On Vista and later, where the native TaskDialog API is available, it automatically calls that function. Otherwise, it uses a standard form and attempts to simulate the behavior of the native TaskDialog API. I've written a similar custom class for my own use, but I never got around to publishing it online.

这篇关于与验证和取消按钮的消息框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆