Results 1 to 6 of 6

Thread: Need help on a Visual Basic programming problem

Threaded View

  1. #2
    PowerPoster cicatrix's Avatar
    Join Date
    Dec 2009
    Location
    Moscow, Russia
    Posts
    3,654

    Re: Need help on a Visual Basic programming problem

    Quote Originally Posted by jokers View Post
    I just need to know how to set up this array within the program to allow 10 values to be entered then later be shown into separate labels as a largest and smallest value.

    The question reads.

    Largest/Smallest Array Values

    Create an application that lets the user enter 10 values into an array. The application should display the largest and smallest values stored in the array. Figure 8-45 shows an example of the application's form after all 10 values have been entered, with the largest and smallest values displayed.

    The form basically shows a large label box where your values will show up. Then two regular labels next to the boxed label that determines the largest and smallest values. There are four buttons, a clear, exit, Display Min & Max, and Input Values.

    I have the actual program layout already made and the initial buttons already declared, being the exit and clear buttons. I just need to know how to setup the array's for the other two buttons.

    If you can help it would be much appreciated.

    Thanks


    Normally, I wouldn't do it using an InputBox, but if this is required then here it is:

    Add this handler to the InputValues buton click event:
    vb Code:
    1. Private Sub btnInputValues_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnInputValues.Click
    2.         Dim userinput As String
    3.         Dim intvalues(9) As Integer
    4.  
    5.         Do
    6.             userinput = InputBox("Enter 10 values (comma separated) in the field below:")
    7.             If userinput = "" Then Exit Sub ' Cancel was pressed.
    8.  
    9.             Dim stringvalues() As String = userinput.Split(New Char() {","c}, StringSplitOptions.RemoveEmptyEntries)
    10.            
    11.             ' Possibly not necessary. It checks the input to be exactly 10 elements:
    12.             If stringvalues.Length <> 10 Then
    13.                 MsgBox("Enter exactly 10 numbers.")
    14.                 Continue Do
    15.             End If
    16.  
    17.             ' Checks each element to be an integer number:
    18.             For x As Integer = 0 To stringvalues.Length - 1
    19.                 If Not Integer.TryParse(stringvalues(x), intvalues(x)) Then
    20.                     MsgBox("Enter valid integer numbers.")
    21.                     Continue Do
    22.                 End If
    23.             Next
    24.             Exit Do
    25.         Loop
    26.  
    27.         lblMaxNumber.Text = intvalues.Max.ToString
    28.         lblMinNumber.Text = intvalues.Min.ToString
    29. End Sub

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