Results 1 to 15 of 15

Thread: Messagebox Display

  1. #1

    Thread Starter
    New Member
    Join Date
    Dec 2007
    Posts
    10

    Messagebox Display

    Hi All,
    Of course im a newbie but I need help as time is of the essence. Im trying to do a lab that will obtain 10 numbers from the user using an InputBox() function, store them internally in a one-dimensional array, and then compute the average of the numbers and sort them in reverse numerical order. The program should control that 10 numbers (no more, no less) are entered, and that the numbers are fully validated as integers. If the user enters non-numeric data, then an appropriate error message should be shown, and that number should be allowed to be re-entered as a valid number. Then all the numbers should be shown as a list in a 'message box'.
    So far I can get everything but the message box to show the list of numbers entered. Can someone help.

    Code:
    Dim binp As Array
        Dim bavg As Double
        Dim bsort As Integer
        Dim arrList As New ArrayList()
        Dim intcount As Integer
    
        Private Sub btnput_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnput.Click
            binp = Array.CreateInstance(GetType(Integer), 10)
    
            For intcount = 1 To 10
                InputBox("Enter a number")
            Next
            For intcount = 1 To 10
    
                MsgBox("The numbers you entered are:", MsgBoxStyle.OkOnly, "Numbers Entered")
    
            Next
        End Sub

  2. #2
    Frenzied Member
    Join Date
    Mar 2006
    Location
    Pennsylvania
    Posts
    1,069

    Re: Messagebox Display

    Here's something I threw together for you. I'm feeling generous.

    vb.net Code:
    1. Private Const numbersCount = 10 ' The amount of numbers inputted.
    2.  
    3.     Private Sub btnInput_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnInput.Click
    4.         Dim numbers(numbersCount) As Integer ' Declare the array.
    5.  
    6.         For item As Integer = 0 To numbersCount - 1 ' Loop through the array.
    7.             Integer.TryParse(InputBox("Please input an integer.", "Integral Input Box"), numbers(item)) ' Put each number inputted into the array.
    8.         Next ' Next iteration.
    9.  
    10.         Dim average As Integer = 0 ' To avoid errors, start at 0.
    11.  
    12.         For Each number As Integer In numbers ' Loop through each number in the array.
    13.             average += number ' Add the number to the average.
    14.         Next ' Next iteration.
    15.  
    16.         average /= numbersCount ' Divide the average by the amount of numbers.
    17.  
    18.         ' The ToString method is used to convert an integral value to its overloaded string representation.
    19.  
    20.         MessageBox.Show("Average: " + average.ToString()) ' Display the average. Use MessageBox.Show instead of MsgBox.
    21.  
    22.         Array.Sort(numbers) ' Easily sort all of the numbers in the array.
    23.  
    24.         For Each number As Integer In numbers ' Loop through each number in the array.
    25.             MessageBox.Show(number.ToString()) ' Display the number in the message box.
    26.         Next ' Next iteration.
    27.     End Sub

  3. #3
    Frenzied Member stimbo's Avatar
    Join Date
    Jun 2006
    Location
    UK
    Posts
    1,739

    Re: Messagebox Display

    Fromethius beat me to it

    Here's my version which is almost identical with one or two differences - if the integer validation fails it will keep going until 10 numbers have been entered and the numbers appear in a list rather than one at a time in the messagebox. I've left out the average:

    vb Code:
    1. Dim myNumbers As New List(Of Integer)
    2. Dim myInt As Integer
    3.  
    4. Do While myNumbers.Count < 10
    5.     If Integer.TryParse(InputBox("", "Enter Number", ""), myInt) = False Then
    6.          MessageBox.Show("That wasn't an integer you idiot", "Non Integer", MessageBoxButtons.OK)
    7.     Else
    8.          myNumbers.Add(myInt)
    9.    End If
    10. Loop
    11.  
    12. myNumbers.Sort()
    13. Dim numList As String = Nothing
    14.  
    15. For i As Integer = myNumbers.Count - 1 To 0 Step -1
    16.     numList &= myNumbers(i).ToString & Environment.NewLine
    17. Next
    18.  
    19. MessageBox.Show("Integers Entered: " & Environment.NewLine & numList, "Here are your numbers", MessageBoxButtons.OK)
    Stim

    Free VB.NET Book Chapter
    Visual Basic 2005 Cookbook Sample Chapter

  4. #4

    Thread Starter
    New Member
    Join Date
    Dec 2007
    Posts
    10

    Re: Messagebox Display

    Wow, Fromethius, Stimbo thank you so much! I was so stumped on this. I appreciate the generosity. Truly!!!

  5. #5

    Thread Starter
    New Member
    Join Date
    Dec 2007
    Posts
    10

    Re: Messagebox Display

    Unfortunately, this does not list the numbers entered in a messagebox. Each number entered must be listed in 1 messagebox. Like so:
    99
    100
    978
    433
    654
    654
    4332
    1

    So far this is what I have :

    Code:
    Option Explicit On
    Option Strict On
    
    Imports System.Globalization
    
    Public Class Form1
        Dim intNumbers(10) As Integer
        Dim intPut As Integer
        Dim dblAvg As Double
        Dim intSort As Integer
        Dim intCount As Integer
    
        Private Sub btnput_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnput.Click
    
            For intCount = 1 To 10
                intNumbers(intCount) = CInt(InputBox("Enter a Number"))
            Next
            MessageBox.Show("The numbers you entered: " & Environment.NewLine & intCount, "Numbers Entered", MessageBoxButtons.OK)
        End Sub

  6. #6
    Frenzied Member stimbo's Avatar
    Join Date
    Jun 2006
    Location
    UK
    Posts
    1,739

    Re: Messagebox Display

    Seriously,

    just take a look at my post. I've done it for you. You need to loop through each of the numbers, adding them to a string with a new line as well on each loop and then assign that string to the messagebox.

    Of course yours won't show on a single message box, how could it?
    Stim

    Free VB.NET Book Chapter
    Visual Basic 2005 Cookbook Sample Chapter

  7. #7
    Frenzied Member Icyculyr's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    1,934

    Re: Messagebox Display

    Option Explicit On
    Option Strict On

    Imports System.Globalization

    vb.net Code:
    1. Public Class Form1
    2.     Dim intNumbers(9) As Integer
    3.     Dim intPut As Integer = 0
    4.     Dim dblAvg As Double = 0.0
    5.     Dim intSort As Integer = 0
    6.  
    7.     Private Sub btnput_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnput.Click
    8.  
    9.         Dim displayString As String = ""
    10.         For i As Integer = 0 To 9
    11.             intNumbers(i) = CInt(InputBox("Enter a Number"))
    12.             displayString &= "Number " & (i+1) & ": Value " & intNumbers(i) & ControlChars.NewLine
    13.         Next i
    14.         MessageBox.Show("something here" & displayString, MessageBoxButtons.OK)
    15.     End Sub

    You don't actually need intNumbers() but I thought you might need it later on...... but perhaps not...

    I rewrote your code a fair bit..

  8. #8
    Frenzied Member
    Join Date
    Mar 2006
    Location
    Pennsylvania
    Posts
    1,069

    Re: Messagebox Display

    Edit: Looks like I've been beaten. Oh, how the tables have turned!

    Should be an easy fix. There is probably a better way but this works too.

    vb.net Code:
    1. Private Const numbersCount = 10 ' The amount of numbers inputted.
    2.  
    3.     Private Sub btnInput_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnInput.Click
    4.         Dim numbers(numbersCount) As Integer ' Declare the array.
    5.  
    6.         For item As Integer = 0 To numbersCount - 1 ' Loop through the array.
    7.             Integer.TryParse(InputBox("Please input an integer.", "Integral Input Box"), numbers(item)) ' Put each number inputted into the array.
    8.         Next ' Next iteration.
    9.  
    10.         Dim average As Integer = 0 ' To avoid errors, start at 0.
    11.  
    12.         For Each number As Integer In numbers ' Loop through each number in the array.
    13.             average += number ' Add the number to the average.
    14.         Next ' Next iteration.
    15.  
    16.         average /= numbersCount ' Divide the average by the amount of numbers.
    17.  
    18.         ' The ToString method is used to convert an integral value to its overloaded string representation.
    19.  
    20.         MessageBox.Show("Average: " + average.ToString()) ' Display the average. Use MessageBox.Show instead of MsgBox.
    21.  
    22.         Array.Sort(numbers) ' Easily sort all of the numbers in the array.
    23.  
    24.         Dim message As String = ""
    25.  
    26.         For Each number As Integer In numbers ' Loop through each number in the array.
    27.             message += number.ToString() + " , " ' Add the current number to the message.
    28.         Next ' Next iteration.
    29.  
    30.         MessageBox.Show(message) ' Display the message.
    31.     End Sub

  9. #9

    Thread Starter
    New Member
    Join Date
    Dec 2007
    Posts
    10

    Re: Messagebox Display

    I'm sorry Stimbo,
    This has me so stressed out I didn't grasp what I was seeing. Your the greatest! That worked exactly the way it's supposed to for the lab. I'm overwhelmed! Thank you so much! Now I can breath. Thanx again!!!

  10. #10

    Thread Starter
    New Member
    Join Date
    Dec 2007
    Posts
    10

    Re: Messagebox Display

    Actually Icyculyr,
    We have to use intNumber. See the requirements:

    This program will implement a "List Sorter", which will obtain 10 numbers from the user using an InputBox() function, store them internally in a one-dimensional array, and then compute the average of the numbers and sort them in reverse numerical order.

    It should have the following characteristics:

    A 10-element one-dimensional integer array called intNumbers.
    *
    An "Input" command button used to input 10 integers to the array one element at a time by using an InputBox() function. The program should control that 10 numbers (no more, no less) are entered, and that the numbers are fully validated as integers. If the user enters non-numeric data, then an appropriate error message should be shown, and that number should be allowed to be re-entered as a valid number. The group of numbers should be entered only once, by the way, and the "Average" and "Sort" buttons must use the data entered previously through the "Input" button.
    *
    A second command that finds the average of all elements of the array. For example, the average of 3, 7, and 13 is (3 + 7 + 13) / 3 = 7.67.
    *
    A third command button that sorts the array in descending order and displays the sorted array in a properly formatted message box (i.e., not jumbled together). “Descending order” means that the largest numbers are shown first, and the smallest last.
    *
    For example, if the user inputs into your program: 654, 88, 12, -888, 1000, 65, 889, 54, 0, 55; then the display should show the following (when clicking on the three buttons):

    then it has to have 3 separate message boxes. 1 for numbers entered, 1 for average and 1 for sorting the numbers entered. I have attached a Word doc to show what its supposed to look like.

  11. #11
    Frenzied Member
    Join Date
    Mar 2006
    Location
    Pennsylvania
    Posts
    1,069

    Re: Messagebox Display

    At least they're specific..

    I hate when teachers force naming conventions. Usually the ones they force are outdated, ugly, unspecific, etc.

  12. #12

    Thread Starter
    New Member
    Join Date
    Dec 2007
    Posts
    10

    Re: Messagebox Display

    I know it makes me ill but thanx to you guys im almost done. That messagebox was giving me chest pains...lol

  13. #13
    New Member
    Join Date
    Oct 2009
    Posts
    1

    Re: Messagebox Display

    Can anyone help the program crashes when debugged?

    Here is the code:

    Option Strict On
    Public Class Form1

    Dim intNumbers(9) As Integer
    Dim intPut As Integer = 0
    Dim dblAvg As Double = 0.0
    Dim intSort As Integer = 0


    Private Sub btnExit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnExit.Click

    Me.Close()
    End Sub

    Private Sub btnInput_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnInput.Click

    Dim intCounter As Integer
    Dim strMessage As String
    Dim strData As String
    For intCounter = 0 To 9
    strData = InputBox("Enter a number")

    If Not IsNumeric(strData) Then
    MessageBox.Show("Not a number", "Error")
    Else
    intNumbers(intCounter) = CInt(strData)
    End If


    Next
    strMessage = "The numbers entered are:" & vbNewLine
    For intCounter = 0 To 9
    strMessage = strMessage & intNumbers(intCounter).ToString("") & vbNewLine
    Next
    MessageBox.Show(strMessage, "Input Data", MessageBoxButtons.OK, _
    MessageBoxIcon.Information, MessageBoxDefaultButton.Button1, _
    MessageBoxOptions.RightAlign)
    End Sub



    Private Sub btnsort_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnsort.Click

    Dim myNumbers As New List(Of Integer)
    Dim myInt As Integer
    Do While myNumbers.Count < 10
    If Integer.TryParse(InputBox("", "Enter Number", ""), myInt) = False Then
    MessageBox.Show("That wasn't an integer", "Non Integer", MessageBoxButtons.OK)
    Else
    myNumbers.Add(myInt)
    End If
    Loop
    myNumbers.Sort()
    Dim numList As String = Nothing

    For i As Integer = myNumbers.Count - 1 To 0 Step -1
    numList &= myNumbers(i).ToString & Environment.NewLine
    Next

    MessageBox.Show("Integers Entered: " & Environment.NewLine & numList, "Here are your numbers", MessageBoxButtons.OK)
    End Sub

    Private Sub btnAvg_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAvg.Click

    Dim strMessage As String
    Dim average As Integer = 0 ' To avoid errors, start at 0.
    Dim numbersCount As Integer
    Dim numbers(numbersCount) As Integer

    For Each number As Integer In numbers ' Loop through each number in the array.

    average += number ' Add the number to the average.

    Next ' Next iteration.

    average = CInt((average / numbersCount))
    strMessage = "The numers entered are:" & vbNewLine


    MessageBox.Show(strMessage, "Input Data", MessageBoxButtons.OK, _
    MessageBoxIcon.Information, MessageBoxDefaultButton.Button1, _
    MessageBoxOptions.RightAlign)
    End Sub
    End Class

  14. #14
    Frenzied Member Icyculyr's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    1,934

    Re: Messagebox Display

    Hey, you just revived a two year old thread... lol

    Do you see any message boxes from the code you have at all?
    Try adding above the code Option Strict On, Option Explicit On
    Code:
    Dim intCounter As Integer
    Dim strMessage As String
    Dim strData As String
    For intCounter = 0 To 9
    Remove the variable intCounter, you can just do For intCounter = 0 To 9, you don't have to declare it.

    Also please wrap your code in [CODE"] [/CODE"] tags (without the little quote ("))
    It preserves spaces and is easier to read.

    Kind Regards

  15. #15
    PowerPoster keystone_paul's Avatar
    Join Date
    Nov 2008
    Location
    UK
    Posts
    3,327

    Re: Messagebox Display

    How about a hint...

    Can anyone help the program crashes when debugged?
    What do you mean exactly by crashes - what's the error message, what line is it on etc?

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