Results 1 to 15 of 15

Thread: [RESOLVED] What's wrong with my MessageBox ?

  1. #1

    Thread Starter
    PowerPoster Poppa Mintin's Avatar
    Join Date
    Mar 2009
    Location
    Bottesford, North Lincolnshire, England.
    Posts
    2,500

    Resolved [RESOLVED] What's wrong with my MessageBox ?

    Hi, I'm trying to get a return result from a MessageBox like this:

    vb.net Code:
    1. Private Sub Button9_Click(sender As System.Object, e As System.EventArgs) Handles Button9.Click
    2.         Dim ask As String = "Are you sure you want to delete this record ?"
    3.         Dim flag As Boolean = True
    4.  
    5.         MessageBox.Show(ask, "Delete", MessageBoxButtons.YesNo, MessageBoxIcon.Warning)
    6.         flag = CBool(Windows.Forms.DialogResult.No)
    7.  
    8.         If flag Then Exit Sub
    9.  
    10.     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.

  2. #2
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    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!

  3. #3
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    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"

  4. #4

    Thread Starter
    PowerPoster Poppa Mintin's Avatar
    Join Date
    Mar 2009
    Location
    Bottesford, North Lincolnshire, England.
    Posts
    2,500

    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:
    1. Private Sub Button9_Click(sender As System.Object, e As System.EventArgs) Handles Button9.Click
    2.         Dim flag As Boolean = Ask()
    3.  
    4.         If flag Then Exit Sub
    5.  
    6.     End Sub
    7.  
    8.     Private Function Ask() As Boolean
    9.         Dim Q As String = "Are you sure you want to delete this record ?"
    10.         Dim R As MsgBoxResult
    11.         Dim Sty As MsgBoxStyle = MsgBoxStyle.YesNo
    12.  
    13.         R = MsgBox(Q, Sty, Title:="Delete")
    14.         If R = MsgBoxResult.No Then
    15.             Return True
    16.         Else
    17.             Return False
    18.         End If
    19.  
    20.     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.

  5. #5
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    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!

  6. #6
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,414

    Re: What's wrong with my MessageBox ?

    MsgBox returns MsgBoxResult
    Messagebox.Show returns DialogResult

  7. #7

    Thread Starter
    PowerPoster Poppa Mintin's Avatar
    Join Date
    Mar 2009
    Location
    Bottesford, North Lincolnshire, England.
    Posts
    2,500

    Re: What's wrong with my MessageBox ?

    Quote Originally Posted by Static View Post
    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:
    1. Private Sub Button9_Click(sender As System.Object, e As System.EventArgs) Handles Button9.Click
    2.         Dim Ask As String = "Are you sure you want to delete this record ?"
    3.         If MsgBox(Ask, CType(vbYesNo + vbExclamation, MsgBoxStyle), "Delete") = vbNo Then Exit Sub
    4.  
    5.     End Sub
    And of course, works both ways.


    Poppa.
    Along with the sunshine there has to be a little rain sometime.

  8. #8

    Thread Starter
    PowerPoster Poppa Mintin's Avatar
    Join Date
    Mar 2009
    Location
    Bottesford, North Lincolnshire, England.
    Posts
    2,500

    Re: What's wrong with my MessageBox ?

    Quote Originally Posted by dunfiddlin View Post
    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.

  9. #9
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,414

    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

  10. #10
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    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!

  11. #11

    Thread Starter
    PowerPoster Poppa Mintin's Avatar
    Join Date
    Mar 2009
    Location
    Bottesford, North Lincolnshire, England.
    Posts
    2,500

    Re: [RESOLVED] What's wrong with my MessageBox ?

    Quote Originally Posted by .paul. View Post
    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.

  12. #12
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    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
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  13. #13
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    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!

  14. #14
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,414

    Re: [RESOLVED] What's wrong with my MessageBox ?

    Quote Originally Posted by Poppa Mintin View Post
    uncharacteristically caustic???
    BTW I posted too. didn't notice a rating

  15. #15
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: [RESOLVED] What's wrong with my MessageBox ?

    Quote Originally Posted by dunfiddlin View Post
    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.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width