|
-
Apr 7th, 2009, 02:48 PM
#1
Thread Starter
Member
[RESOLVED] Project
Im trying to write a program that will do
"1. The average of the four numbers. This answer will be placed into a text box reserved for that value. This text box cannot be changed by the user.
2. The maximum and minimum values. These will be denoted by changing the color of the largest number green and the smallest number red.
3. The range of the numbers. The range is defined as the difference between the largest and smallest numbers. The range will be placed into its own text box, labeled as such. This text box cannot be changed by the user."
Can anyone point me in the right direction? Im using visual studio 2008 and it has to be written in VB
-
Apr 7th, 2009, 03:08 PM
#2
Re: Project
Thread moved from CodeBank-VB6 forum (which is for you to post your code examples, not questions) to 'VB.Net' (VB2002 and later) forum
-
Apr 7th, 2009, 03:19 PM
#3
Re: Project
Can you post what you've tried?
-
Apr 7th, 2009, 10:20 PM
#4
Re: Project
As Forum said, what have you tried, people on this forum will not do your homework for you. If you can post what you have tried, we can nudge you in the right direction.
For a little help, you can use the .Max and .Min methods on an arry, which will tell you the maximum and minimum values respectively.
-
Apr 8th, 2009, 01:34 AM
#5
Re: Project
 Originally Posted by mbw290
Im trying to write a program that will do
"1. The average of the four numbers. This answer will be placed into a text box reserved for that value. This text box cannot be changed by the user.
2. The maximum and minimum values. These will be denoted by changing the color of the largest number green and the smallest number red.
3. The range of the numbers. The range is defined as the difference between the largest and smallest numbers. The range will be placed into its own text box, labeled as such. This text box cannot be changed by the user."
Can anyone point me in the right direction? Im using visual studio 2008 and it has to be written in VB
Sure, I will point you in the right direction...
http://msdn.microsoft.com/en-us/library/default.aspx
Things that you might want to search for in there are:
Double
TextBox
Gary
-
Apr 9th, 2009, 03:16 PM
#6
Thread Starter
Member
Re: Project
Thanks I managed to get some of it done. The program will calculate the average of three numbers. How can I alert the user that the text boxes are empty if they dont input a value?
-
Apr 9th, 2009, 03:19 PM
#7
Hyperactive Member
Re: Project
If TextBox1.Text = String.Empty Then
Msgbox("INPUT SOMETHING!")
Else
MsgBox("Thanks for Inputting Something")
End If
-
Apr 9th, 2009, 03:53 PM
#8
Thread Starter
Member
Re: Project
Thanks! I'm having some trouble with the min and max function
Function Max(A As Integer, B As Integer, C As Integer) As Integer
If A>B Then
If A>C Then Max = A
Else Max = C
Elseif B>C Then ‘B was greater than A
Max = B
Else Max = C
Return Max ‘This might be skipped by students, as the sample in the slides didn’t
‘have it. Okay not to have it, as END FUNCTION will automatically
‘return value in Max
End Function
Doesnt seem to work
-
Apr 9th, 2009, 03:59 PM
#9
Hyperactive Member
Re: Project
vb Code:
Function Max(ByVal A As Integer, ByVal B As Integer, ByVal C As Integer) As Integer If A > B Then If A > C Then Max = A End If Else : Max = C If B > C Then Max = B Else Max = C End If End If 'Return Max 'This might be skipped by students, as the sample in the slides didn’t 'have it. Okay not to have it, as END FUNCTION will automatically 'return value in Max MsgBox(Max) End Function
Pretty sure that's what you want.
-
Apr 9th, 2009, 04:06 PM
#10
Junior Member
Re: Project
Code:
Function Max(ByVal A As Integer, ByVal B As Integer, ByVal C As Integer) As Integer
Max = A
If B > Max Then Max = B
If C > Max Then Max = C
Return Max
End Function
-
Apr 9th, 2009, 04:07 PM
#11
Thread Starter
Member
Re: Project
Ill have to try that but first
If Me.num1.Text = Integer.Empty Then
MsgBox("INPUT SOMETHING!")
Else
MsgBox("Thanks for Inputting Something")
End If
wouldnt work for me
-
Apr 9th, 2009, 04:10 PM
#12
Hyperactive Member
Re: Project
vb Code:
If TextBox1.Text <> String.Empty Then If Not IsNumeric(TextBox1.Text) Then MsgBox("Numbers Only") End If Else MsgBox("INPUT SOMETHING") End IF
-
Apr 9th, 2009, 04:14 PM
#13
Thread Starter
Member
Re: Project
If Me.num1.Text <> String.Empty Then
If Not IsNumeric(Me.num1.Text) Then
MsgBox("Numbers Only")
End If
Else
MsgBox("INPUT SOMETHING")
End If
I tried this and it didnt work (my textbox is named num1)
-
Apr 9th, 2009, 04:15 PM
#14
Hyperactive Member
Re: Project
Take away the Me.
I don't understand why your using it, your code is already on the FORM of the control, no need for Me.num1.Text, just num1.Text.
-
Apr 9th, 2009, 04:17 PM
#15
Thread Starter
Member
Re: Project
im getting a green underline underneath num1.text and it wont run
-
Apr 9th, 2009, 04:17 PM
#16
Hyperactive Member
Re: Project
vb Code:
If num1.Text <> String.Empty Then If Not IsNumeric(num1.Text) Then MsgBox("Numbers Only") Else MsgBox("Thank You") End If Else MsgBox("INPUT SOMETHING") End If End Sub
Your num1 is a TEXTBOX, Yes?
And Sorry, I forgot to add a ELSE after the MsgBox("Numbers Only")
-
Apr 9th, 2009, 04:21 PM
#17
Thread Starter
Member
Re: Project
Public Class Form2
Private Sub CalculateAverage_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CalculateAverage.Click
Dim num1 As Integer = CInt(Me.num1.Text)
Dim num2 As Integer = CInt(Me.num2.Text)
Dim num3 As Integer = CInt(Me.num3.Text)
Dim num4 As Integer = CInt(Me.num4.Text)
Dim avg As Double = ((num1 + num2 + num3 + num4) / 4)
Dim OutputLabel2 As Double
If num1.ToString Then <> String.Empty Then
If Not IsNumeric(num1.Tostring) Then
Else
MsgBox("Numbers Only")
End If
Else
MsgBox("INPUT SOMETHING")
End If
OutputLabel2 = avg.ToString
Me.Outputlabel2.Text = OutputLabel2
End Sub
End Sub
End Class
This is my entire program
-
Apr 9th, 2009, 04:24 PM
#18
Hyperactive Member
Re: Project
well, First, you Named your Variable and Control the same, so put a i infront of your variable so its iNum1 as Integer
then use my code above.
-
Apr 9th, 2009, 04:26 PM
#19
Thread Starter
Member
Re: Project
What are you talking about?
-
Apr 9th, 2009, 04:26 PM
#20
Hyperactive Member
Re: Project
Code:
Dim inum1 As Integer = CInt(Me.num1.Text)
Dim inum2 As Integer = CInt(Me.num2.Text)
Dim inum3 As Integer = CInt(Me.num3.Text)
dim inum4 as Integer = CInt(Me.num4.Text)
Dim avg As Double = ((inum1 + inum2 + inum3 + inum4) / 4)
If num1.ToString <> String.Empty Then
If Not IsNumeric(num1.ToString) Then
MsgBox("Numbers Only")
Else
'CODE FOR SUCCESSFUL INPUT HERE
End If
Else
MsgBox("INPUT SOMETHING")
End If
OutputLabel2.Text = avg.ToString
-
Apr 9th, 2009, 04:28 PM
#21
Thread Starter
Member
Re: Project
This isnt working! Did you try it yourself?
-
Apr 9th, 2009, 04:29 PM
#22
Hyperactive Member
Re: Project
dude, it isn't helping just saying, IT ISN"T WORKING!!?!?!?!?!? I need error output and etc....
-
Apr 9th, 2009, 04:31 PM
#23
Hyperactive Member
Re: Project
Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim inum1 As Integer
Dim inum2 As Integer
Dim inum3 As Integer
Dim inum4 As Integer
Dim avg As Double
If num1.Text <> String.Empty Then
If Not IsNumeric(num1.Text) Then
MsgBox("Numbers Only")
Else
inum1 = num1.Text
inum2 = num2.Text
inum3 = num3.Text
inum4 = num4.text
avg = ((inum1 + inum2 + inum3 + inum4) / 4)
'CODE FOR SUCCESSFUL INPUT HERE
End If
Else
MsgBox("INPUT SOMETHING")
End If
OutputLabel2.Text = avg.ToString
End Sub
-
Apr 9th, 2009, 04:31 PM
#24
Thread Starter
Member
Re: Project
Sorry Im just getting frustrated
I run the program and leave the num1 text box empty the program stops running and highlights the line where i declared inum1 and says conversion from type string to type 'integer' is not valid
-
Apr 9th, 2009, 04:34 PM
#25
Thread Starter
Member
Re: Project
It says numbers only when the numbers are inputted
-
Apr 9th, 2009, 04:35 PM
#26
Hyperactive Member
Re: Project
Dude, I just tested it with code:
vb Code:
Dim inum1 As Integer Dim inum2 As Integer Dim inum3 As Integer Dim inum4 As Integer Dim avg As Double If num1.Text <> String.Empty Then If Not IsNumeric(num1.Text) Then MsgBox("Numbers Only") Else inum1 = num1.Text inum2 = num2.Text inum3 = num3.Text inum4 = num4.text avg = ((inum1 + inum2 + inum3 + inum4) / 4) 'CODE FOR SUCCESSFUL INPUT HERE End If Else MsgBox("INPUT SOMETHING") End If OutputLabel2.Text = avg.ToString
EXACT CODE - it works, I did 1 + 2 + 3 + 4
then / 4 and it = 2.5
-
Apr 9th, 2009, 04:38 PM
#27
Thread Starter
Member
Re: Project
Thank you so much! I think I may have had the code in the wrong order. Do you think you could help me in getting the minimum and maximum from four numbers?
-
Apr 9th, 2009, 04:39 PM
#28
Hyperactive Member
Re: Project
Use the Math.Max ans Math.Min, do a Google search on it.
-
Apr 9th, 2009, 04:41 PM
#29
Re: Project
Code:
Public Class frmMain
Private Sub frmMain_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim ListOfInts As New List(Of Integer)
With ListOfInts
.Add(214)
.Add(41)
.Add(514)
.Add(353)
.Add(820)
.Add(532)
.Add(422)
End With
MsgBox("Max: " & Max(ListOfInts))
MsgBox("Average: " & Average(ListOfInts))
End Sub
Private Function Max(ByVal Integers As List(Of Integer)) As Integer
Integers.Sort()
Return Integers.Item(Integers.Count - 1)
End Function
Private Function Average(ByVal Integers As List(Of Integer)) As Integer
Dim ListAvg As Integer = 0
For Each Int As Integer In Integers
ListAvg += Int
Next
Return CInt(ListAvg / Integers.Count)
End Function
End Class
-
Apr 9th, 2009, 04:43 PM
#30
Thread Starter
Member
Re: Project
For the previous code I need something that will make the message box appear Numbers Only when any of the four text boxes or empty. How would i do that?
-
Apr 9th, 2009, 05:00 PM
#31
Thread Starter
Member
-
Apr 9th, 2009, 05:20 PM
#32
Re: Project
Well I probably made it a little to complex but add a groupbox on the form and put the four textboxes inside the groupbox:
Code:
Public Class frmMain
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim InValid As Boolean = False
For Each ctl As Control In Me.GroupBox1.Controls
If TypeOf ctl Is TextBox Then
Dim myTxt As TextBox = DirectCast(ctl, TextBox)
If myTxt.Text = String.Empty Or Not Double.TryParse(myTxt.Text, New Double) Then
InValid = True
End If
End If
Next
If InValid Then
MsgBox("Verify textbox inputs.")
Else
Dim ListOfDbl As New List(Of Double)
With ListOfDbl
.Add(CDbl(TextBox1.Text))
.Add(CDbl(TextBox2.Text))
.Add(CDbl(TextBox3.Text))
.Add(CDbl(TextBox4.Text))
End With
MsgBox("Max: " & Max(ListOfDbl))
MsgBox("Average: " & Average(ListOfDbl))
End If
End Sub
Private Function Max(ByVal Doubles As List(Of Double)) As Double
Doubles.Sort()
Return Doubles.Item(Doubles.Count - 1)
End Function
Private Function Average(ByVal Doubles As List(Of Double)) As Double
Dim ListAvg As Double = 0
For Each Dbl As Double In Doubles
ListAvg += Dbl
Next
Return Math.Round(ListAvg / Doubles.Count, 2)
End Function
End Class
Also, I changed it to Doubles so you can use decimals.
-
Apr 9th, 2009, 05:59 PM
#33
Thread Starter
Member
Re: Project
Sorry Im such a newb but how would that code fit into the code I have now which reads
Public Class Form2
Private Sub CalculateAverage_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CalculateAverage.Click
Dim inum1 As Integer
Dim inum2 As Integer
Dim inum3 As Integer
Dim inum4 As Integer
Dim avg As Double
If num1.Text <> String.Empty Then
If Not IsNumeric(num1.Text) Then
MsgBox("Numbers Only")
Else
inum1 = num1.Text
inum2 = num2.Text
inum3 = num3.Text
inum4 = num4.Text
avg = ((inum1 + inum2 + inum3 + inum4) / 4)
'CODE FOR SUCCESSFUL INPUT HERE
End If
Else
MsgBox("INPUT SOMETHING")
End If
Outputlabel2.Text = avg.ToString
End Sub
End Class
-
Apr 10th, 2009, 01:00 AM
#34
Re: Project
Hey,
I am seeing a lot of code flying around in this thread, but I am not sure that this is the right way to approach this, mbw290 has made it clear that this is a homework assignment, so rather than just give him/her code that will do the job, I think explanation of the problem and some of the code that has been given would be a better approach.
For instance, the problem needs four integers, so how can we go about doing this.
1) You could use 4 TextBoxes, and validate the input to ensure that they are numbers.
2) Use a NumericUpDown Control to receive the numbers from the user which only allow numbers to be input, therefore validation of whether it is a number is not needed.
If you are going down option 1, then I would suggest that you use TryParse, and this would be used as follows:
Code:
Dim inum1 As Integer
If Not Integer.TryParse(num1.Text, inum1) Then
MessageBox.Show("Please enter a valid number in num1.")
Return
End If
This should be used on each TextBox that is being used, at the minute, you are only validating the first TextBox.
In the code above, if a valid number isn't entered, then the code Return without doing anything further.
Gary
-
Apr 10th, 2009, 09:29 AM
#35
Thread Starter
Member
-
Apr 11th, 2009, 03:31 AM
#36
Re: Project
mbw290 have all your questions on this topic been answered? If so, remember to follow the links in my signature to close it off. If not, feel free to post back with additional questions.
Gary
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
|