none
Embedding one if statement within another with a messagebox... RRS feed

  • Question

  • I have run into a small situation, I attempted to write this code:

            If Shift = "EVE" Or "NIGHT" Then
                MessageBox.Show("Have you attempt to find the shift report created by the DAY shift?", "Attention", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) = Windows.Forms.DialogResult.No
                then blah blah blah..
                End If

    Now I understand what is basically happening and why it isn't allowing me to run code like this, but what I am trying to say, is that if EVE or NIGHT is selected, display this messagebox with two options, yes and no, and if no is selected, then end the subroutine.

    the problem being that to set up this multiple IF while using and IF with the messagebox is erroring me out.. 

    HELP!!


    Douglas

    • Moved by Amanda Zhu Friday, May 10, 2013 6:11 AM
    Thursday, May 9, 2013 3:58 PM

Answers

  • Hi Dnetbandit,

    first of all if shift="EVE" or "Night" then
    blabla
    end if

    it wont works.

    you have to do it in this way

    if shift="eve" or shift="Night" then
    argument
    end if

    so to make a choice between yes or no u have to do it in this way

    if shift="eve" or shift="Night" then

    Dim result = MessageBox.Show("your message", "caption", MessageBoxButtons.YesNo) If result = DialogResult.yes Then MessageBox.Show("good" & shift) Else exit sub End If

    end if




    • Edited by CSNewbie Friday, May 10, 2013 8:13 AM
    • Proposed as answer by Cor Ligthert Friday, May 10, 2013 9:19 AM
    • Marked as answer by Dnetbandit Friday, May 10, 2013 12:32 PM
    Friday, May 10, 2013 8:09 AM

All replies

  • Hello,

    I have moved this thread to Visual Basic forum for better response.

    Best regards,


    Amanda Zhu [MSFT]
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, May 10, 2013 6:12 AM
  • Hi Dnetbandit,

    first of all if shift="EVE" or "Night" then
    blabla
    end if

    it wont works.

    you have to do it in this way

    if shift="eve" or shift="Night" then
    argument
    end if

    so to make a choice between yes or no u have to do it in this way

    if shift="eve" or shift="Night" then

    Dim result = MessageBox.Show("your message", "caption", MessageBoxButtons.YesNo) If result = DialogResult.yes Then MessageBox.Show("good" & shift) Else exit sub End If

    end if




    • Edited by CSNewbie Friday, May 10, 2013 8:13 AM
    • Proposed as answer by Cor Ligthert Friday, May 10, 2013 9:19 AM
    • Marked as answer by Dnetbandit Friday, May 10, 2013 12:32 PM
    Friday, May 10, 2013 8:09 AM
  • make sure ur shift.toString are in same case as the string u are comparing. if not it will just skip the code and treat it as diff string
    Friday, May 10, 2013 8:19 AM
  •         If Shift = "EVE" Or "NIGHT" Then
                If MessageBox.Show("Have you attempt to find the shift report created by the DAY shift?", "Attention", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) = DialogResult.Yes Then
                    'blah blah blah..'
                Else
                    Return
                End If
                Return
            End If

    More meant to show SNewbie that his way is a little bit old fashion, but his sample goes as well.


    Success
    Cor

    Friday, May 10, 2013 9:19 AM
  •  If Shift = "EVE" Or "NIGHT" Then
    this works as well?
    cos i always put that way vb always give me error.
     If MessageBox.Show("Have you attempt to find the shift report created by the DAY shift?", "Attention", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) = DialogResult.Yes Then
                    'blah blah blah..'
                Else

    good i will try that next time haha

    must follow the trend
    Friday, May 10, 2013 9:36 AM
  • Hi, I don't understand what exactly you want to do, but I suspect you may have case sensitivity issue. Try this:

    If Shift.ToUpper = "EVE" Or Shift.ToUpper = "NIGHT" Then
    Messagebox.Show("Your message")
    End if

    However even if this does not solve your problem, it is a good practice to take care for case sensitivity when comparing strings; this may help avoid logical bugs which are very difficult to catch.
    • Edited by BGQQ Friday, May 10, 2013 9:51 AM
    Friday, May 10, 2013 9:47 AM
  • CSNEWBIE

    Genius, thank you, so that is how you do this.. so you literally create a variable to store the result of the message box.. I thought that the only way you could work with the decisions of a message box was within the box itself.. excellent.. it worked.. 

    thank you for the instructional.. 

    Doug.


    Douglas

    Friday, May 10, 2013 12:32 PM
  • Genius, thank you, so that is how you do this.. so you literally create a variable to store the result of the message box.. I thought that the only way you could work with the decisions of a message box was within the box itself.. excellent.. it worked.. 

    You don't necessarily have to create a variable to store the result of the MessageBox (although that is often the best approach). You can test the result directly in an If statement. For example in a similar way to the one Cor showed in his reply.

    If Shift.ToUpper() = "EVE" Or Shift.ToUpper() = "NIGHT" Then
        If MessageBox.Show("Have you attempt to find the shift report created by the DAY shift?", "Attention", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) = DialogResult.No Then
            'Do something
        Else
            'Do something else
        End If
    End If

    Friday, May 10, 2013 1:02 PM