|
-
May 6th, 2013, 11:29 AM
#1
[RESOLVED] What's wrong with my MessageBox ?
Hi, I'm trying to get a return result from a MessageBox like this:
vb.net Code:
Private Sub Button9_Click(sender As System.Object, e As System.EventArgs) Handles Button9.Click
Dim ask As String = "Are you sure you want to delete this record ?"
Dim flag As Boolean = True
MessageBox.Show(ask, "Delete", MessageBoxButtons.YesNo, MessageBoxIcon.Warning)
flag = CBool(Windows.Forms.DialogResult.No)
If flag Then Exit Sub
End Sub
Clicking either the 'Yes' or 'No' button on the MessageBox always returns flag = True.
I've tried 'Dim flag As Boolean = False' and ran the code but still get 'True', so it's not just bypassing the code, it's actually setting flag to True.
The MsgBox title is 'Delete' as it ought to be, the correct question is being asked and the 'Warning' icon is being displayed.
I originally had ' flag = Windows.Forms.DialogResult.No' (i.e. without the CBool( ... ) but Option Strict insisted !.
Oh... and I've also tried 'flag = CBool(Windows.Forms.DialogResult.Yes)'... still with the same always 'True' result.
Poppa.
Along with the sunshine there has to be a little rain sometime.
-
May 6th, 2013, 11:34 AM
#2
Re: What's wrong with my MessageBox ?
Dim flag = MessageBox.Show(ask, "Delete", MessageBoxButtons.YesNo, MessageBoxIcon.Warning)
If flag = Windows.Forms.DialogResult.No Then
As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"
Reviews: "dunfiddlin likes his DataTables" - jmcilhinney
Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!
-
May 6th, 2013, 11:38 AM
#3
Re: What's wrong with my MessageBox ?
you need to catch whats being clicked...
like this
IF msgbox(ask,vbYesNo+vbCritical,"Delete") = vbNo then exit sub
or set it to a variable
Dim Result as Integer = msgbox(ask,vbYesNo+vbCritical,"Delete")
etc
JPnyc rocks!! (Just ask him!)
If u have your answer please go to the thread tools and click "Mark Thread Resolved"
-
May 6th, 2013, 12:04 PM
#4
Re: What's wrong with my MessageBox ?
@ Static: I shall try that as soon as I've posted this solution:
I've used the following code, slightly amended, for a similar check question before, but I thought the code suggested by 'Home and Learn 2010' was worth a try...
vb.net Code:
Private Sub Button9_Click(sender As System.Object, e As System.EventArgs) Handles Button9.Click
Dim flag As Boolean = Ask()
If flag Then Exit Sub
End Sub
Private Function Ask() As Boolean
Dim Q As String = "Are you sure you want to delete this record ?"
Dim R As MsgBoxResult
Dim Sty As MsgBoxStyle = MsgBoxStyle.YesNo
R = MsgBox(Q, Sty, Title:="Delete")
If R = MsgBoxResult.No Then
Return True
Else
Return False
End If
End Function
A bit lengthier, but at least it works !
Off to try Static's suggestion now...
Poppa.
Along with the sunshine there has to be a little rain sometime.
-
May 6th, 2013, 12:12 PM
#5
Re: What's wrong with my MessageBox ?
Do you want me to sit in a corner and rust or just fall apart where I'm standing?
As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"
Reviews: "dunfiddlin likes his DataTables" - jmcilhinney
Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!
-
May 6th, 2013, 12:17 PM
#6
Re: What's wrong with my MessageBox ?
MsgBox returns MsgBoxResult
Messagebox.Show returns DialogResult
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
May 6th, 2013, 12:20 PM
#7
Re: What's wrong with my MessageBox ?
 Originally Posted by Static
you need to catch whats being clicked...
like this
IF msgbox(ask,vbYesNo+vbCritical,"Delete") = vbNo then exit sub
or set it to a variable
Dim Result as Integer = msgbox(ask,vbYesNo+vbCritical,"Delete")
etc
Thanks Static, what's much tidier...
vb.net Code:
Private Sub Button9_Click(sender As System.Object, e As System.EventArgs) Handles Button9.Click
Dim Ask As String = "Are you sure you want to delete this record ?"
If MsgBox(Ask, CType(vbYesNo + vbExclamation, MsgBoxStyle), "Delete") = vbNo Then Exit Sub
End Sub
And of course, works both ways.
Poppa.
Along with the sunshine there has to be a little rain sometime.
-
May 6th, 2013, 12:24 PM
#8
Re: What's wrong with my MessageBox ?
 Originally Posted by dunfiddlin
Do you want me to sit in a corner and rust or just fall apart where I'm standing?
I've no idea what this is supposed to mean, but I only asked why the code supplied by a tutorial reputedly up-to-date (2012) 'Home and Learn' wasn't working.
Poppa.
Along with the sunshine there has to be a little rain sometime.
-
May 6th, 2013, 12:26 PM
#9
Re: [RESOLVED] What's wrong with my MessageBox ?
dunfiddlin showed you how to make your code from 'Home and Learn' work.
an explanation with the code might've been helpful
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
May 6th, 2013, 12:43 PM
#10
Re: [RESOLVED] What's wrong with my MessageBox ?
Explanations are for wimps! 
flag = CBool(Windows.Forms.DialogResult.No)
There is no direct conversion from the dialog result to Boolean so what this amounts to is 'Is Windows.Forms.DialogResult.No Windows.Forms.DialogResult.No?" to which the Boolean answer is always True. Even if that were not the case, there is no reference at any point to what button was actually pressed on the Dialog. In other words you would get exactly the same result if there never actually was a message box.
If this is from Home and Learn verbatim, I suggest you contact the author and put him right!
As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"
Reviews: "dunfiddlin likes his DataTables" - jmcilhinney
Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!
-
May 6th, 2013, 12:45 PM
#11
Re: [RESOLVED] What's wrong with my MessageBox ?
 Originally Posted by .paul.
dunfiddlin showed you how to make your code from 'Home and Learn' work.
an explanation with the code might've been helpful
Yeah, I suppose that's right but if you look at the post times you see I was writing the reply to Static's post when Dunfiddlin posted his.
Oh... I was just looking back for exact times and discovered a post I'd not seen. I didn't see your first post Dunfiddlin, my email said I'd had a reply and clicking it took me straight to Static's post, bypassed yours entirely... Sorry.
I must say I was very surprised by Dunfiddlin's uncharacteristically caustic post but I can see his point now.
Poppa.
Along with the sunshine there has to be a little rain sometime.
-
May 6th, 2013, 12:48 PM
#12
Re: [RESOLVED] What's wrong with my MessageBox ?
Why fool around with converting???? Don't make things more complicated than they need to be.
Code:
Dim ask As String = "Are you sure you want to delete this record ?"
Dim flag As DialogResult = MessageBox.Show(ask, "Delete", MessageBoxButtons.YesNo, MessageBoxIcon.Warning)
If flag = Windows.Forms.DialogResult.No Then
'no, exit
Exit Sub
Else
'yes
End If
I find it helpful to explicitly define the data types.
Last edited by dbasnett; May 6th, 2013 at 12:52 PM.
Reason: changed result to flag
-
May 6th, 2013, 12:50 PM
#13
Re: [RESOLVED] What's wrong with my MessageBox ?
uncharacteristically caustic
Uncharacteristic? I must be going soft in my old age!!!
As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"
Reviews: "dunfiddlin likes his DataTables" - jmcilhinney
Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!
-
May 6th, 2013, 12:50 PM
#14
Re: [RESOLVED] What's wrong with my MessageBox ?
 Originally Posted by Poppa Mintin
uncharacteristically caustic???
BTW I posted too. didn't notice a rating
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
May 6th, 2013, 12:54 PM
#15
Re: [RESOLVED] What's wrong with my MessageBox ?
 Originally Posted by dunfiddlin
Uncharacteristic? I must be going soft in my old age!!! 
He'll learn 
Hopefully poppa will learn to scan the threads when he gets a notification so he doesn't miss one.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|