Results 1 to 36 of 36

Thread: [RESOLVED] Project

  1. #1

    Thread Starter
    Member
    Join Date
    Apr 2009
    Posts
    34

    Resolved [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

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    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

  3. #3
    Master Of Orion ForumAccount's Avatar
    Join Date
    Jan 2009
    Location
    Canada
    Posts
    2,802

    Re: Project

    Can you post what you've tried?

  4. #4
    PowerPoster 2.0 Negative0's Avatar
    Join Date
    Jun 2000
    Location
    Southeastern MI
    Posts
    4,367

    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.

  5. #5
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: Project

    Quote Originally Posted by mbw290 View Post
    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

  6. #6

    Thread Starter
    Member
    Join Date
    Apr 2009
    Posts
    34

    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?

  7. #7
    Hyperactive Member
    Join Date
    Oct 2007
    Posts
    258

    Re: Project

    If TextBox1.Text = String.Empty Then
    Msgbox("INPUT SOMETHING!")
    Else
    MsgBox("Thanks for Inputting Something")
    End If

  8. #8

    Thread Starter
    Member
    Join Date
    Apr 2009
    Posts
    34

    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

  9. #9
    Hyperactive Member
    Join Date
    Oct 2007
    Posts
    258

    Re: Project

    vb Code:
    1. Function Max(ByVal A As Integer, ByVal B As Integer, ByVal C As Integer) As Integer
    2.         If A > B Then
    3.             If A > C Then
    4.                 Max = A
    5.             End If
    6.         Else : Max = C
    7.             If B > C Then
    8.                 Max = B
    9.             Else
    10.                 Max = C
    11.             End If
    12.         End If
    13.  
    14.         'Return Max 'This might be skipped by students, as the sample in the slides didn’t
    15.         'have it. Okay not to have it, as END FUNCTION will automatically
    16.         'return value in Max
    17.         MsgBox(Max)
    18.     End Function

    Pretty sure that's what you want.

  10. #10
    Junior Member iRoN_RoCK's Avatar
    Join Date
    Jul 2008
    Posts
    31

    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

  11. #11

    Thread Starter
    Member
    Join Date
    Apr 2009
    Posts
    34

    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

  12. #12
    Hyperactive Member
    Join Date
    Oct 2007
    Posts
    258

    Re: Project

    vb Code:
    1. If TextBox1.Text <> String.Empty Then
    2. If Not IsNumeric(TextBox1.Text) Then
    3. MsgBox("Numbers Only")
    4. End If
    5. Else
    6. MsgBox("INPUT SOMETHING")
    7. End IF

  13. #13

    Thread Starter
    Member
    Join Date
    Apr 2009
    Posts
    34

    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)

  14. #14
    Hyperactive Member
    Join Date
    Oct 2007
    Posts
    258

    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.

  15. #15

    Thread Starter
    Member
    Join Date
    Apr 2009
    Posts
    34

    Re: Project

    im getting a green underline underneath num1.text and it wont run

  16. #16
    Hyperactive Member
    Join Date
    Oct 2007
    Posts
    258

    Re: Project

    vb Code:
    1. If num1.Text <> String.Empty Then
    2.             If Not IsNumeric(num1.Text) Then
    3.                 MsgBox("Numbers Only")
    4.             Else
    5.                 MsgBox("Thank You")
    6.             End If
    7.         Else
    8.             MsgBox("INPUT SOMETHING")
    9.         End If
    10.     End Sub

    Your num1 is a TEXTBOX, Yes?

    And Sorry, I forgot to add a ELSE after the MsgBox("Numbers Only")

  17. #17

    Thread Starter
    Member
    Join Date
    Apr 2009
    Posts
    34

    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

  18. #18
    Hyperactive Member
    Join Date
    Oct 2007
    Posts
    258

    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.

  19. #19

    Thread Starter
    Member
    Join Date
    Apr 2009
    Posts
    34

    Re: Project

    What are you talking about?

  20. #20
    Hyperactive Member
    Join Date
    Oct 2007
    Posts
    258

    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

  21. #21

    Thread Starter
    Member
    Join Date
    Apr 2009
    Posts
    34

    Re: Project

    This isnt working! Did you try it yourself?

  22. #22
    Hyperactive Member
    Join Date
    Oct 2007
    Posts
    258

    Re: Project

    dude, it isn't helping just saying, IT ISN"T WORKING!!?!?!?!?!? I need error output and etc....

  23. #23
    Hyperactive Member
    Join Date
    Oct 2007
    Posts
    258

    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

  24. #24

    Thread Starter
    Member
    Join Date
    Apr 2009
    Posts
    34

    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

  25. #25

    Thread Starter
    Member
    Join Date
    Apr 2009
    Posts
    34

    Re: Project

    It says numbers only when the numbers are inputted

  26. #26
    Hyperactive Member
    Join Date
    Oct 2007
    Posts
    258

    Re: Project

    Dude, I just tested it with code:

    vb Code:
    1. Dim inum1 As Integer
    2.         Dim inum2 As Integer
    3.         Dim inum3 As Integer
    4.         Dim inum4 As Integer
    5.         Dim avg As Double
    6.         If num1.Text <> String.Empty Then
    7.             If Not IsNumeric(num1.Text) Then
    8.                 MsgBox("Numbers Only")
    9.             Else
    10.                 inum1 = num1.Text
    11.                 inum2 = num2.Text
    12.                 inum3 = num3.Text
    13.                 inum4 = num4.text
    14.                 avg = ((inum1 + inum2 + inum3 + inum4) / 4)
    15.                 'CODE FOR SUCCESSFUL INPUT HERE
    16.             End If
    17.         Else
    18.             MsgBox("INPUT SOMETHING")
    19.         End If
    20.         OutputLabel2.Text = avg.ToString

    EXACT CODE - it works, I did 1 + 2 + 3 + 4

    then / 4 and it = 2.5

  27. #27

    Thread Starter
    Member
    Join Date
    Apr 2009
    Posts
    34

    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?

  28. #28
    Hyperactive Member
    Join Date
    Oct 2007
    Posts
    258

    Re: Project

    Use the Math.Max ans Math.Min, do a Google search on it.

  29. #29
    Master Of Orion ForumAccount's Avatar
    Join Date
    Jan 2009
    Location
    Canada
    Posts
    2,802

    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

  30. #30

    Thread Starter
    Member
    Join Date
    Apr 2009
    Posts
    34

    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?

  31. #31

    Thread Starter
    Member
    Join Date
    Apr 2009
    Posts
    34

    Re: Project

    *are empty

  32. #32
    Master Of Orion ForumAccount's Avatar
    Join Date
    Jan 2009
    Location
    Canada
    Posts
    2,802

    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.

  33. #33

    Thread Starter
    Member
    Join Date
    Apr 2009
    Posts
    34

    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

  34. #34
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    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

  35. #35

    Thread Starter
    Member
    Join Date
    Apr 2009
    Posts
    34

    Re: Project

    Thank you SO MUCH!!!

  36. #36
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    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
  •  



Click Here to Expand Forum to Full Width