Windows窗体自定义消息框 [英] Windows forms custom message box

查看:108
本文介绍了Windows窗体自定义消息框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想显示带有按钮的消息框,按钮的名称可以作为参数传递,并将值返回给相应按钮的调用函数

,例如我希望两个按钮显示为Ok和Cancel,当用户单击Ok时,我想返回0和1取消对调用函数。

在另一种情况下,而不是Ok-Cancel,I想要传递Yes,No,Cancel作为按钮名称,并分别返回0,1,2。

这可以在Window Forms应用程序中使用吗?



我尝试了什么:



我创建了另一个表单,在表单中添加按钮为Ok / Cancel / Yes / No / Retry / Ignore每个按钮都有其事件处理程序和该事件处理程序的相应代码。

但我不能在多个地方使用单个按钮,因为它有单个事件处理程序。

示例:表单A调用此自定义消息框以显示是和否。在是电子邮件将被发送,否将不会发生任何事情。在另一种情况下,即表单B调用相同的自定义消息框以使用是和否按钮,但此表单B希望在是按钮单击时将数据插入到数据库中。即不同的功能将使用这些按钮来执行不同的操作,这是我尝试过的。

I want to display messagebox with buttons whose name can be passed as parameter and return a value to the calling function respective to the button clicked
e.g. I want two buttons to be displayed as Ok and Cancel, when user clicks Ok, I want to return 0 and 1 for cancel to the calling function.
In another case, instead of Ok-Cancel, I want to pass Yes,No,Cancel as button names and return value as 0,1,2 respectively.
Is this possible in Window Forms application?

What I have tried:

I have created another form, added buttons to the form as Ok/Cancel/Yes/No/Retry/Ignore each button have its event handler and respective code for that event handler.
But I can't use single button in multiple places as it has single event handler.
Example: Form A calling this custom message box to display Yes and No. Upon Yes email will be sent, upon "No" nothing will happen. In another case, i.e. Form B calling same custom message box to use buttons Yes and No, but this Form B want to insert data into DB upon Yes button click. I.e. Different features will consume these buttons to perform different operations, which is not achieved with what I have tried.

推荐答案

这可以在Window Forms应用程序中实现吗? - 是的CodeProject上有几篇文章可能会给你一些想法 - Search [ ^ ]

对于多个事件处理程序 - 调用模块根据按下的按钮知道该怎么做。自定义消息框应该告诉你*按下了哪个*按钮
"Is this possible in Window Forms application?" - Yes. There are several articles here on CodeProject that may give you some ideas - Search[^]
As to the multiple event handlers - it is the calling module that knows what to do depending on which button is pressed. The custom message box should just tell you *which* button was pressed


基本上就像CHill在他的解决方案中回答的那样。

你可以创建自己的MessageBox - 这不是很复杂。

但是......通常情况下,动作不是来自MessageBox。通常,MessageBox-Form上的按钮设置一个DialogResult-Value,它从调用MessageBox的那一点开始计算(例如从Form中的某个地方),然后调用它。所以通常MessageBox给出一个反馈,比如DialogResult-Enumation(它实际上是Integer-Values - 但是定义了)。
Basicly it is like CHill has answered in his Solution.
You can create your own MessageBox - that is not very complicated.
But ... normally the action doesn't comes from the MessageBox. Normally the Buttons on the MessageBox-Form set a DialogResult-Value which is evaluated from that point where the MessageBox is called (for example somewhere from your Form) and after it is called. So normally the MessageBox gives a Feedback like the DialogResult-Enumation (which really are Integer-Values - but defined ones).


这篇关于Windows窗体自定义消息框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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