Results 1 to 15 of 15

Thread: Can i have multiple IF statements in a single button?

  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 2011
    Posts
    7

    Can i have multiple IF statements in a single button?

    Hi i am very very new to visual basic.. i have some experiance with SQL scripting that's it.

    I am just starting to learn the visual basic language so i am trying to make a simple application where a user can put in it's first and last name or else the application exists.

    I manage to do this fine untill i get to the point of ending one if statement and starting another in the same button.. I do not really know how to explain it but maybe someone can look and my code and guide me to what is wrong.

    (in the form i have button1, button2, label1, label2. Button2 is only a "quit" button)

    Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    Dim prompt1, prompt2, firstname, lastname, msg1, msg2 As String
    Dim response As MsgBoxResult
    Dim response2 As MsgBoxResult

    prompt1 = "give me your first name"
    prompt2 = "give me your last name"
    msg1 = "acsess denied"
    msg2 = "acsess denied"

    'prompts you for firstname'
    firstname = InputBox(prompt1)
    MsgBox(firstname, vbYesNo, "input result")
    If response = MsgBoxResult.Yes Then
    'the firstname is supposed to be visible in the lable if you click yes'
    Label1.Text = firstname
    Else
    'the application is supposed to close when you click no'
    MsgBox(msg1, , )
    Application.Exit()

    End If

    'prompts you for lastname'
    lastname = InputBox(prompt2)
    MsgBox(lastname, vbYesNo, "input result")
    If response2 = MsgBoxResult.Yes Then
    'the lastname is supposed to be visible in the lable if you click yes'
    Label2.Text = lastname
    Else
    'the application is supposed to close when you click no'
    MsgBox(msg2, , )
    Application.Exit()

    End If

    End Sub

    What happens when i run this is that i can put in the first name but then when i click yes the application closes.

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,104

    Re: Can i have multiple IF statements in a single button?

    Application.EXIT is your problem. That will do exactly what it says: Exit the application. There is almost never a good reason to have that in there, and it is certainly wrong in your case.

    Spoke a bit too soon: I still wouldn't use Application.Exit, since it really is an unkind thing to do to the user, in this case as in most, but here the problem is actually that you are reaching the Else statement because of an error in the If statement

    Code:
    MsgBox(firstname, vbYesNo, "input result")
    If response = MsgBoxResult.Yes Then
    Write them like this:
    Code:
    If MessageBox.Show(firstname,"Input Result",MessageBoxButtons.YesNo) = DialogResult.Yes Then
    MsgBox is a holdover from VB6, while MessageBox is the .NET version.
    Last edited by Shaggy Hiker; Mar 10th, 2011 at 11:55 AM.
    My usual boring signature: Nothing

  3. #3

    Thread Starter
    New Member
    Join Date
    Mar 2011
    Posts
    7

    Re: Can i have multiple IF statements in a single button?

    but it is only supposed to exit if i click "no" and after the "acsess denied" message is displayed.

    Instead when i click yes then the "acsess denied" message is displayed the firstname does not show in the label1 and it immediately goes to the second prompt then it only exits when clicking no at the second response

  4. #4
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,104

    Re: Can i have multiple IF statements in a single button?

    Yeah, I wrote in haste, and have now given a more complete answer. The problem was that you were never putting the response from the MsgBox into the result variable that you had created to receive it. You don't actually need the result variable, though, as you can switch directly on the return value of the MsgBox, or MessageBox if you go .NET.
    My usual boring signature: Nothing

  5. #5

    Thread Starter
    New Member
    Join Date
    Mar 2011
    Posts
    7

    Re: Can i have multiple IF statements in a single button?


    Write them like this:
    Code:
    If MessageBox.Show(firstname,"Input Result",MessageBoxButtons.YesNo) = DialogResult.Yes Then
    MsgBox is a holdover from VB6, while MessageBox is the .NET version.
    I tried that but it is still ignoring the "no" answer or would that be the "else" that it is ignoring... maybe i am getting ahead of myself with trying to put two IF statements on one button, considering i just started learning this a few days ago?

  6. #6
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,104

    Re: Can i have multiple IF statements in a single button?

    You can have as many If statements as you like, so that isn't the issue.

    If you have replaced both of your If statements with code like that (the second one would be a bit different as you are showing different things), then if you press No, then the Else should be executed, which will show the second messagebox. If that is not what is happening, post the new code, and this time wrap it in Code blocks by highlighting it and pressing the Code button.
    My usual boring signature: Nothing

  7. #7

    Thread Starter
    New Member
    Join Date
    Mar 2011
    Posts
    7

    Re: Can i have multiple IF statements in a single button?

    Code now looks like this:

    Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim prompt1, prompt2, firstname, lastname, msg1, msg2 As String

    prompt1 = "give me your first name"
    prompt2 = "give me your last name"
    msg1 = "acsess denied"
    msg2 = "acsess denied"

    'prompts you for first name'
    firstname = InputBox(prompt1)
    MessageBox.Show(firstname, "input result", MessageBoxButtons.YesNo)
    If DialogResult.Yes Then
    'the firstname is supposed to be visible in the lable if you click yes'
    Label1.Text = firstname
    Else
    'the application is supposed to close when you click no'
    MsgBox(msg1, , )
    Application.Exit()

    End If

    'prompts you for first name'
    lastname = InputBox(prompt2)
    MessageBox.Show(lastname, "input result", MessageBoxButtons.YesNo)
    If DialogResult.Yes Then
    'the lastname is supposed to be visible in the lable if you click yes'
    Label2.Text = lastname
    Else
    'the application is supposed to close when you click no'
    MsgBox(msg2, , )
    Application.Exit()

    End If

    End Sub

  8. #8

    Thread Starter
    New Member
    Join Date
    Mar 2011
    Posts
    7

    Re: Can i have multiple IF statements in a single button?

    Can not edit my posts :P

    vb Code:
    1. Public Class Form1
    2.  
    3. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    4. Dim prompt1, prompt2, firstname, lastname, msg1, msg2 As String
    5.  
    6. prompt1 = "give me your first name"
    7. prompt2 = "give me your last name"
    8. msg1 = "acsess denied"
    9. msg2 = "acsess denied"
    10.  
    11. 'prompts you for first name'
    12. firstname = InputBox(prompt1)
    13. MessageBox.Show(firstname, "input result", MessageBoxButtons.YesNo)
    14. If DialogResult.Yes Then
    15. 'the firstname is supposed to be visible in the lable if you click yes'
    16. Label1.Text = firstname
    17. Else
    18. 'the application is supposed to close when you click no'
    19. MsgBox(msg1, , )
    20. Application.Exit()
    21.  
    22. End If
    23.  
    24. 'prompts you for first name'
    25. lastname = InputBox(prompt2)
    26. MessageBox.Show(lastname, "input result", MessageBoxButtons.YesNo)
    27. If DialogResult.Yes Then
    28. 'the lastname is supposed to be visible in the lable if you click yes'
    29. Label2.Text = lastname
    30. Else
    31. 'the application is supposed to close when you click no'
    32. MsgBox(msg2, , )
    33. Application.Exit()
    34.  
    35. End If
    36.  
    37. End Sub

  9. #9
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,104

    Re: Can i have multiple IF statements in a single button?

    Yeah, you may have to get another few posts in for that.

    You didn't exactly follow my code, did you? Take a look at how I suggested you write the If statement in post #2 and compare it to how you have it written. After all,

    If DialogResult.Yes Then

    is ALWAYS going to be True, so the Else is never going to be reached, but that's not what I had written, nor is it what you quoted.
    My usual boring signature: Nothing

  10. #10

    Thread Starter
    New Member
    Join Date
    Mar 2011
    Posts
    7

    Re: Can i have multiple IF statements in a single button?

    Oh yea i was in a bit of hurry there, exciting stuff.
    Thanks for the help though, i can not work with this on my laptop here at home but I'll be back at this first thing tomorrow once i get to work.(part of current job description is to learn this)

    Question:
    If "If dialogresult.yes then" equates to always being true then what is the point of having else there when there is a choice of yes or no?

    The original code i had was how i learnt to do it from the book (me adding the if statements is me playing with the code and getting ahead of the book :P)
    Last edited by Lukee; Mar 10th, 2011 at 02:26 PM. Reason: not wanting to add another post for the question.

  11. #11
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,104

    Re: Can i have multiple IF statements in a single button?

    An If statement evaluates it's conditional statement to either True or False. If it is just a number, then anything other than 0 will be True. Dialog.Yes is an Enum, which means that it will be evaluated as an integer, though I'm not sure which one. It would be the same as writing:

    If 4 Then

    Since 4 is not 0, this will always evaluate to True.

    In my code, I had (roughly):

    If messageBox = DialogResult.Yes Then

    In which case the messagebox will be shown, and whatever the user clicks on will be compared to Yes. If the user clicks on Yes, then the MessageBox will return DialogResult.Yes, which will be equal to DialogResult.Yes, so the expression will evaluate to True. If the user clicks the No button, then MessageBox will return DialogResult.No which is NOT equal to DialogResult.Yes, so the expression will evaluate to False.
    My usual boring signature: Nothing

  12. #12
    Fanatic Member
    Join Date
    Jul 2009
    Posts
    629

    Re: Can i have multiple IF statements in a single button?

    Just for when you ever have to evaluate all three results, you can store the messagebox result in a new variable and evaluate that.

    Code:
                        Dim firstname As String = InputBox(prompt1)
                        Dim result As MsgBoxResult = MessageBox.Show(firstname, "input result", MessageBoxButtons.YesNo)
                        If result = MsgBoxResult.Yes Then
                            Label1.Text = firstname
                        ElseIf result = MsgBoxResult.No Then
                            Label1.Text = "None"
                        Else
                            'Cancel, others as well
                            Application.Exit()
                        End If

  13. #13
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,104

    Re: Can i have multiple IF statements in a single button?

    Yes, but if you only have two possible values, you don't need the variable.

    The extra variable will cost you nothing at all in this case, so if you find it more readable to write the statement the way bergekiller did, then certainly do so. The extra variable will take a bit more memory, which isn't really an issue at all, but since it is a local variable, it will be created on the stack when the method is run, so that insignificant detail is even MORE insignificant.
    My usual boring signature: Nothing

  14. #14

    Thread Starter
    New Member
    Join Date
    Mar 2011
    Posts
    7

    Re: Can i have multiple IF statements in a single button?

    Thank you for your help Shaggy Hiker, consider this resolved i used the code as you wrote and it works perfectly
    I'll be reading up now on the difference between msgbox and messagebox and where to place the IF.

  15. #15
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,104

    Re: Can i have multiple IF statements in a single button?

    There is little difference between msgbox and messagebox, except that messagebox has replaced msgbox, so don't be expecting to find much there.

    You can mark the thread as resolved by going to the Thread Tools menu item and picking the resolved (or whatever) option.
    My usual boring signature: Nothing

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