如何创建带问题的消息框 [英] How to create a message box with question
本文介绍了如何创建带问题的消息框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想写一个'你想离开吗?'的消息。这是一个是/否的问题,如果答案是肯定的,它会离开,但如果我说不,它也会退出。
出了什么问题?
MessageBox.Show( 你真的想离开吗?, 退出。,MessageBoxButtons.YesNo)
如果 Windows.Forms.DialogResult.Yes 那么
结束
ElseIf Windows.Forms.DialogResult.No 然后
退出 Sub
结束 如果
解决方案
if-Statement的谓词有一个常量值,即Windows 。形式.DialogResult.Yes
,显然算作True
。所以第一个分支总是被执行,无论如何。换句话说,您实际上并未检查MessageBox.Show(..)
-Method的结果。您需要捕获其返回值并将其与Windows.Forms.DialogResult.Yes
进行比较:
Dim 结果 As Windows.Forms.DialogResult
result = MessageBox。显示( 你真的想离开吗?, 退出。,MessageBoxButtons.YesNo)
如果结果= Windows。 Forms.DialogResult.Yes 然后
结束
否则
退出 Sub
结束 如果
而不是将结果保存到变量你可以直接在如果条件
中使用它并检查你ser行动。请参考以下代码:
如果 MessageBox.Show( 你真的想离开吗?, Exist,MessageBoxButtons.YesNo,MessageBoxIcon.Question)= Windows.Forms.DialogResult.Yes 然后
结束 ' 如果用户选择是
否则
退出 Sub 如果用户选择否
结束 如果跨度>
I am trying to make a 'do you want to leave?' message. It is a yes/no question and if the answer is yes it leaves, but then if I say no, it will quit as well.
What is going wrong?
MessageBox.Show("Do you really want to leave?", "Exiting.", MessageBoxButtons.YesNo)
If Windows.Forms.DialogResult.Yes Then
End
ElseIf Windows.Forms.DialogResult.No Then
Exit Sub
End If
解决方案
The predicate of your if-Statement has a constant value, namelyWindows.Forms.DialogResult.Yes
and that apparently counts asTrue
. So the first branch gets always executed no matter what. In other words, you don't actually check for the result of theMessageBox.Show(..)
-Method. You need to capture its return value and compare that toWindows.Forms.DialogResult.Yes
:
Dim result As Windows.Forms.DialogResult result = MessageBox.Show("Do you really want to leave?", "Exiting.", MessageBoxButtons.YesNo) If result = Windows.Forms.DialogResult.Yes Then End Else Exit Sub End If
Instead of saving result into a variable you can directly use it inIf condition
and check user action. Please refer below code :
If MessageBox.Show("Do you really want to leave?", "Exist", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then End 'If User Select Yes Else Exit Sub 'If User Select No End If
这篇关于如何创建带问题的消息框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文