Results 1 to 22 of 22

Thread: Multiple Message Boxes

  1. #1

    Thread Starter
    Member
    Join Date
    Oct 2015
    Posts
    35

    Multiple Message Boxes

    OK so I trying to write a program where it compares 2 integers the user enters and tells you if one is greater than the other or if one is less than the other or if they are equal. I have most of the code figured out but my issue is the answer. I want a different message box to pop up for different answers but the same message box saying the first number is greater than the other keeps popping up. So how can I make it that a different message box pops up for a different answer?

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,479

    Re: Multiple Message Boxes

    We need to see your code...

  3. #3

    Thread Starter
    Member
    Join Date
    Oct 2015
    Posts
    35

    Re: Multiple Message Boxes

    Sorry I forgot to post it. Here it is:

    Dim int1 As Integer
    Dim int2 As Integer
    Dim Answer As String
    Dim Answer2 As String
    Dim Answer3 As String


    int1 = InputBox(“What is the first number?”, “NumberA”)
    int1 = InputBox(“What is the second number?”, “NumberB”)

    If int1 > int2 Then
    Answer = MsgBox("Number A is greater than Number B")
    End If
    If int1 < int2 Then
    Answer2 = MsgBox("Number A is less than Number B")
    End If
    If int1 = int2 Then
    Answer3 = MsgBox("Number A equals Number B")
    End If


    End Sub

  4. #4
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,479

    Re: Multiple Message Boxes

    here's a simple example:

    Code:
    Public Class Form1
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim a, b As Integer
            Dim msg As String
            If Integer.TryParse(TextBox1.Text, a) AndAlso Integer.TryParse(TextBox2.Text, b) Then
                If a > b Then
                    msg = String.Format("{0} is greater than {1}", a, b)
                ElseIf b > a Then
                    msg = String.Format("{0} is greater than {1}", b, a)
                Else
                    msg = String.Format("{0} is equal to {1}", a, b)
                End If
                MessageBox.Show(msg)
            End If
        End Sub
    
    End Class

  5. #5
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,479

    Re: Multiple Message Boxes

    Code:
    int1 = InputBox(“What is the first number?”, “NumberA”)
    Code:
    int1 = InputBox(“What is the second number?”, “NumberB”)

  6. #6

    Thread Starter
    Member
    Join Date
    Oct 2015
    Posts
    35

    Re: Multiple Message Boxes

    I did that code but the message boxes aren't popping up

  7. #7
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,479

    Re: Multiple Message Boxes

    both inputboxes set int1
    the other way I showed you is a safer method. try typing 'hello' in your inputbox

  8. #8

    Thread Starter
    Member
    Join Date
    Oct 2015
    Posts
    35

    Re: Multiple Message Boxes

    I did set both inputboxes int1 and it still doesn't work. Also typing hello breaks it.

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

    Re: Multiple Message Boxes

    Code:
    Dim int1 As Integer
    Dim int2 As Integer
    Dim Answer As String
    Dim Answer2 As String
    Dim Answer3 As String
    
    
    int1 = InputBox(“What is the first number?”, “NumberA”)
    int2 = InputBox(“What is the second number?”, “NumberB”)
    
    If int1 > int2 Then
        Answer = MsgBox("Number A is greater than Number B")
    End If
    If int1 < int2 Then
        Answer2 = MsgBox("Number A is less than Number B")
    End If
    If int1 = int2 Then
        Answer3 = MsgBox("Number A equals Number B")
    End If

  10. #10
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,479

    Re: Multiple Message Boxes

    What I did there, see it, you don't

  11. #11

    Thread Starter
    Member
    Join Date
    Oct 2015
    Posts
    35

    Re: Multiple Message Boxes

    Thanks so much I knew I probably made one little error which messed the whole thing up. Another question I am also trying to write a program that tells someone their mark on the test and then if they passed or failed. So how can I make a message box pop up and then when it closes another pop up? Here is my code:

    Dim mark As Double
    Dim total As Integer
    Dim percent As Double
    Dim msg As String

    mark = InputBox("What was your mark on the test?", "Mark")
    total = InputBox("How many marks where on the test?", "Total marks")

    percent = mark / total * 100

    percent = MsgBox("You percent on the test is, " & percent)

    If percent >= 50 Then
    msg.TrimStart("You passed the test")
    Else
    msg.TrimStart("You failed the test")
    End If


    End Sub
    End Class

  12. #12
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,479

    Re: Multiple Message Boxes

    TrimStart is used to remove leading spaces from a string...

    Code:
    If percent >= 50 Then
        msg = "You passed the test"
    Else
        msg = "You failed the test"
    End If
    MsgBox(msg)

  13. #13
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,479

    Re: Multiple Message Boxes

    and it wouldn't be:

    Code:
    percent = MsgBox("You percent on the test is, " & percent)
    it'd be:

    Code:
    MsgBox("You percent on the test is, " & percent)

  14. #14

    Thread Starter
    Member
    Join Date
    Oct 2015
    Posts
    35

    Re: Multiple Message Boxes

    Thanks and I have one last quick question. I want to add a nesting structure so that if the user enters an invalid mark, they get an error message, otherwise the program continues like normal. How would I go about doing that? I am not sure what nesting code looks like because I am a beginner at coding (which I why I have all these issues). Also sorry if I am troubling you / annoying you with all of this.

  15. #15

    Thread Starter
    Member
    Join Date
    Oct 2015
    Posts
    35

    Re: Multiple Message Boxes

    Actually I figured it out but ran into a issue. The message pops up saying that the mark is invalid but then the messages pops up saying the mark and that I passed. So how can I get the only the one message to pops up? Here is the code:

    If mark > total Then
    MsgBox(“Invalid marks. Start again.”)
    End If
    percent = mark / total * 100

    MsgBox("You percent on the test is, " & percent)

    If percent >= 50 Then
    msg = "You passed the test"
    Else
    msg = "You failed the test"
    End If
    MsgBox(msg)

  16. #16
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,479

    Re: Multiple Message Boxes

    Code:
    If mark > total Then
        MsgBox(“Invalid marks. Start again.”)
        Return
    End If

  17. #17

    Thread Starter
    Member
    Join Date
    Oct 2015
    Posts
    35

    Re: Multiple Message Boxes

    Thanks and sorry if I annoyed you with all of this. I am new to coding so I am getting used to it.

  18. #18
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    39,038

    Re: Multiple Message Boxes

    One thing .Paul showed in his first example, then moved away from is using .TryParse to properly convert a string to a number. InputBox returns ONLY strings. By assigning those to variables that are numeric types you are requiring the compiler to perform an implicit conversion from a string to a number. As long as the string is a number, that works. It isn't as efficient as converting it correctly, but it works. However, when you type in a string that is NOT a number...it crashes. The .TryParse methods (Integer, Double, and so forth all have them) perform the conversions safely and return False if the conversion is not possible. You really should be using them to convert ALL user input.
    My usual boring signature: Nothing

  19. #19
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,479

    Re: Multiple Message Boxes

    As Shaggy told you, your code is not ideal...
    I showed you a more professional way to write it.
    I'm not angry about the questions.

  20. #20
    Lively Member chipp's Avatar
    Join Date
    May 2012
    Posts
    78

    Re: Multiple Message Boxes

    why there's no such operator like the "?" in C++?

    i would've done it easier in C++
    Code:
    if (num1 == num2)
    	std::cout << "equal." << std::endl;
    else
    	std::cout << (num1 < num2 ? "first number is smaller" : "first number is greater");

  21. #21
    You don't want to know.
    Join Date
    Aug 2010
    Posts
    4,578

    Re: Multiple Message Boxes

    Quote Originally Posted by chipp View Post
    why there's no such operator like the "?" in C++?

    i would've done it easier in C++
    Code:
    if (num1 == num2)
    	std::cout << "equal." << std::endl;
    else
    	std::cout << (num1 < num2 ? "first number is smaller" : "first number is greater");
    There is, in the form of the If() operator.

    Code:
    MessageBox.Show(If(percent >= 50, "You passed.", "You failed."))
    It doesn't get advertised or used a lot. I've never liked it, syntactically, and also avoid the tertiary operator ?: in C-like languages for similar reasons. Sometimes they make code more pretty. Other times it's just more symbols and cognitive load. For example, I'd say an "easier" version of your code would be:
    Code:
    char* output;
    if (num1 == num2) {
      output = "equal.";
    else {
      output = (num1 < num2) ? 
        "first number is smaller." : 
        "first number is greater";
    }
    
    std::cout << output << endl;
    It's a matter of taste, but "fewer lines of code" is often not the same as "easier to read or maintain". Of course, if we want to play "which language is better" games, VB can hook you up with a Select..Case syntax that's underappreciated and getting explored in languages like Swift and F#. I think it goes something like this:
    Code:
    Dim output As String = ""
    Select Case num1
      Case Is = num2
        output = "equal"
      Case Is < num2
        output = "less"
      Case Is > num2
        output = "greater"
    End Select
    
    Console.WriteLine(output)
    I think it's pretty neat.
    This answer is wrong. You should be using TableAdapter and Dictionaries instead.

  22. #22
    Lively Member chipp's Avatar
    Join Date
    May 2012
    Posts
    78

    Re: Multiple Message Boxes

    i just know that select...casecan used that way...
    learned one more new thing
    thx, bro

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