|
-
Dec 15th, 2007, 03:32 PM
#1
Thread Starter
New Member
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
-
Dec 15th, 2007, 03:55 PM
#2
Frenzied Member
Re: Messagebox Display
Here's something I threw together for you. I'm feeling generous.
vb.net Code:
Private Const numbersCount = 10 ' The amount of numbers inputted.
Private Sub btnInput_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnInput.Click
Dim numbers(numbersCount) As Integer ' Declare the array.
For item As Integer = 0 To numbersCount - 1 ' Loop through the array.
Integer.TryParse(InputBox("Please input an integer.", "Integral Input Box"), numbers(item)) ' Put each number inputted into the array.
Next ' Next iteration.
Dim average As Integer = 0 ' To avoid errors, start at 0.
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 /= numbersCount ' Divide the average by the amount of numbers.
' The ToString method is used to convert an integral value to its overloaded string representation.
MessageBox.Show("Average: " + average.ToString()) ' Display the average. Use MessageBox.Show instead of MsgBox.
Array.Sort(numbers) ' Easily sort all of the numbers in the array.
For Each number As Integer In numbers ' Loop through each number in the array.
MessageBox.Show(number.ToString()) ' Display the number in the message box.
Next ' Next iteration.
End Sub
-
Dec 15th, 2007, 04:08 PM
#3
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:
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 you idiot", "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)
-
Dec 15th, 2007, 05:13 PM
#4
Thread Starter
New Member
Re: Messagebox Display
Wow, Fromethius, Stimbo thank you so much! I was so stumped on this. I appreciate the generosity. Truly!!!
-
Dec 15th, 2007, 06:42 PM
#5
Thread Starter
New Member
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
-
Dec 15th, 2007, 06:47 PM
#6
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?
-
Dec 15th, 2007, 06:50 PM
#7
Frenzied Member
Re: Messagebox Display
Option Explicit On
Option Strict On
Imports System.Globalization
vb.net Code:
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 btnput_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnput.Click
Dim displayString As String = ""
For i As Integer = 0 To 9
intNumbers(i) = CInt(InputBox("Enter a Number"))
displayString &= "Number " & (i+1) & ": Value " & intNumbers(i) & ControlChars.NewLine
Next i
MessageBox.Show("something here" & displayString, MessageBoxButtons.OK)
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..
-
Dec 15th, 2007, 06:51 PM
#8
Frenzied Member
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:
Private Const numbersCount = 10 ' The amount of numbers inputted.
Private Sub btnInput_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnInput.Click
Dim numbers(numbersCount) As Integer ' Declare the array.
For item As Integer = 0 To numbersCount - 1 ' Loop through the array.
Integer.TryParse(InputBox("Please input an integer.", "Integral Input Box"), numbers(item)) ' Put each number inputted into the array.
Next ' Next iteration.
Dim average As Integer = 0 ' To avoid errors, start at 0.
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 /= numbersCount ' Divide the average by the amount of numbers.
' The ToString method is used to convert an integral value to its overloaded string representation.
MessageBox.Show("Average: " + average.ToString()) ' Display the average. Use MessageBox.Show instead of MsgBox.
Array.Sort(numbers) ' Easily sort all of the numbers in the array.
Dim message As String = ""
For Each number As Integer In numbers ' Loop through each number in the array.
message += number.ToString() + " , " ' Add the current number to the message.
Next ' Next iteration.
MessageBox.Show(message) ' Display the message.
End Sub
-
Dec 15th, 2007, 06:58 PM
#9
Thread Starter
New Member
-
Dec 15th, 2007, 07:24 PM
#10
Thread Starter
New Member
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.
-
Dec 15th, 2007, 07:29 PM
#11
Frenzied Member
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.
-
Dec 15th, 2007, 08:06 PM
#12
Thread Starter
New Member
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
-
Oct 16th, 2009, 09:20 PM
#13
New Member
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
-
Oct 17th, 2009, 11:33 PM
#14
Frenzied Member
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
-
Oct 18th, 2009, 03:33 AM
#15
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|