从自定义消息框类生成对话框结果 [英] Generate dialog result from a custom message box class
本文介绍了从自定义消息框类生成对话框结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
亲爱的朋友,
我正在开发类似以下内容的自定义消息框类-
Dear friend,
I am developing a custom messagebox class like the following-
Public Class MyCustomMsgBox
Private MyForm As Form = New Form
Private lblHeadline As Label = New Label
Private lblMessageBody As Label = New Label
Private btnNo As Button = New Button
Private btnOk As Button = New Button
Private btnYes As Button = New Button
Public Sub New(ByVal Message As String)
With MyForm
.Width = 438
.Height = 214
.Controls.AddRange(New Control() {lblHeadline, lblMessageBody, btnNo, btnYes, btnOk})
End With
End Sub
Public Shared Function ShowErrorMsg(ByVal ErrorMessage As String) As Windows.Forms.DialogResult
Dim obj As MyCustomMsgBox = New MyCustomMsgBox(ErrorMessage)
obj.MyForm.ShowDialog()
End Sub
Public Shared function ShowSuccessMsg(ByVal SuccessMessage As String) As Windows.Forms.DialogResult
''some code
End Sub
Public Shared Function AskQuestions(ByVal Questions As String) As Windows.Forms.DialogResult
''some code
End Sub
Public Shared Function ShowExceptions(ByVal ExMessage As String) As Windows.Forms.DialogResult
''some code
End Sub
''Private Sub btnNo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNo.Click
'' Windows.Forms.DialogResult.No()
''End Sub
End Class
这些功能具有相关的图形,颜色,标题和标题.
btnOk将返回DialogResult.Ok,btnNo将返回DialogResult.No,btnYes将返回DialogResult.Yes
我怎么知道按下了哪个按钮?
我不知道如何在无格式的类中处理按钮单击事件.
你能给我个主意吗?
提前谢谢您.
SKPaul
Those functions are designed with related graphics, color, title and heading.
btnOk will return DialogResult.Ok, btnNo will return DialogResult.No and btnYes will return DialogResult.Yes
How do i know that which button is pressed?
i dont know how to handle a button click event in a formless class.
Would you give me the idea?
Thank you in advance.
SKPaul
推荐答案
您是否尝试过:
Have you tried:
Private Sub btnNo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNo.Click
Me.DialogResult = DialogResult.No
End Sub
因为您没有派生您的类MyCustomMsgBox
只需将其包装在表单中即可,您可以自由地以所需的方式实现返回结果.不清楚为什么您会认为这是一个问题.
可能您想从ShowErrorMsg
返回对话框结果.首先,使其成为函数并返回obj.MyForm.ShowDialog()
.该调用已经返回System.Windows.Forms.DialogResult
.现在,您想返回正确的值.这很简单,因为System.Windows.Forms.Form.DialogResult
是读/写属性,因此可以为MyForm
分配适当的值.将其分配给按钮的事件句柄.
在ShowSuccessMsg
中执行相同的操作.
现在,我认为您的临时对象obj
是多余的.它没有任何功能.您可以改为创建表单的临时实例,然后返回其DialogResult
.另外,具有两个单独的功能ShowErrorMsg
和ShowSuccessMsg
也是非常……有问题的.
从消息对话框功能的角度来看,错误和成功之间没有明显区别.您只需使用一种方法ShowMessage
即可完全满足您的要求.即使您想以不同的方式(例如,不同的颜色)显示错误和成功,方法参数也可以.通过System.Windows.Forms.MessageBoxIcon
有什么问题? (还要考虑一下:为什么要显示全部成功?您可以将成功视为所有操作中最正常的预期行为,仅显示错误.:-))
为什么不重现System.Windows.Forms.MessageBox.Show
方法的接口?您传递有关所需按钮和图标的信息(也可以传递有关消息缩进的信息:错误,成功或其他信息).您甚至可以改善此界面:添加图像,允许自定义按钮设置等.
—SA
As you do not derive your classMyCustomMsgBox
from anything but simply wrap it around a form, you have a freedom to implement return result the way you want. Not clear why you think this is a problem.
Probably, you want to return dialog result fromShowErrorMsg
. First, make it a function and returnobj.MyForm.ShowDialog()
. This call already returnsSystem.Windows.Forms.DialogResult
. Now, you want to return right value. It''s simple becauseSystem.Windows.Forms.Form.DialogResult
is a read/write property, so you can assign proper value toMyForm
. Assign it in the event handles of your buttons.
Do the same thing inShowSuccessMsg
.
Now, I think your temporary objectobj
is redundant. It does not carry any functionality. You can create a temporary instance of the form instead and return itsDialogResult
. Also, having two separate functionsShowErrorMsg
andShowSuccessMsg
is very… questionable.
From the stand point of the message dialog functionality there is no distinct difference between error and success. You could perfectly satisfy your requirements with only one methodShowMessage
. Even if you want to show error and success in different ways (say, different color), a method parameter will do. What''s wrong about passingSystem.Windows.Forms.MessageBoxIcon
? (Also, think about this: why showing success at all? You can consider success as a normal most expected behavior of all operation, show only the errors. :-))
Why not reproducing interface ofSystem.Windows.Forms.MessageBox.Show
methods? You pass information about buttons you need, icon (which also conduct information about the indent of the message: error, success or something else). You can even improve this interface: add images, allow for custom button set, etc.
—SA
这篇关于从自定义消息框类生成对话框结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文