I need to sort three numbers that are entered in three different textboxes and display these numbers ascending and when I press a different button display them descending. Now I think that I need to put these three numbers into an array, sort and display, but I'm not really too proficient in code to accomplish this. Is there a way to do this without putting them into an Array. Here is my code so far? Which is not much of anything so far. Any guidance would be greatly appreciated, Thanks in advance.

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim num1, num2, num3 As Integer

        num1 = TextBox1.Text
        num2 = TextBox2.Text
        num3 = TextBox3.Text

You can sort three numbers without an array. But your prof/teacher may have wanted an array-based solution. So put those numbers in an array and you have a solution which can easily handle also four integer's sorting .

VB.NET arrays have a Sort-method: Array.Sort(MyIntArr) which sorts your array in ascending order and that's all you need.

In the second button (descending order) , simply dump your sorted array out in the reverse order:

For i = MyIntArr.GetUpperBound(0) To 0 Step -1
  ' Dump MyIntArr(i)
Next i

One last thing. You can't directly assign textual value to integer, you have to cast the textual representation of the number to the actual numerical value:

Dim MyIntArr(2) As Integer
MyIntArr(0) = CInt(TextBox1.Text)

If you want to do it without using an array, try this:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim Done As Boolean = False
        Dim Over As Boolean = False
        Dim num1 As Single = Single.Parse(TextBox1.Text)
        Dim num2 As Single = Single.Parse(TextBox2.Text)
        Dim num3 As Single = Single.Parse(TextBox3.Text)
        Do While Not Done
            Done = True
            If num1 > num2 Then Swap(num1, num2) : Done = False
            If num1 > num3 Then Swap(num1, num3) : Done = False
            If num2 > num3 Then Swap(num2, num3) : Done = False
        Loop
        TextBox1.Text = num1.ToString
        TextBox2.Text = num2.ToString
        TextBox3.Text = num3.ToString
    End Sub

    Private Sub Swap(ByRef n1 As Single, ByRef n2 As Single)
        Dim temp As Single = n1
        n1 = n2
        n2 = temp
    End Sub

Ok, thanks for the help, any further suggestions on how to get this to work would be appreciated. Here is my code so far, I'm not sure what I'm doing wrong?:(

Public Class Form1
    Dim MyIntArr(2) As Integer
    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        Me.Close()

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        'Dim num1, num2, num3 As Integer

        MyIntArr(0) = CInt(TextBox1.Text)
        MyIntArr(1) = CInt(TextBox2.Text)
        MyIntArr(2) = CInt(TextBox3.Text)
        Array.Sort(MyIntArr)

        outputBox.Text = MyIntArr(2)
        'MyIntArr(2) = outputBox.Text

    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim i As Integer

        For i = MyIntArr.GetUpperBound(0) To 0 Step -1
            ' Dump MyIntArr(i)
        Next i
        outputBox.Text = MyIntArr(i)
    End Sub
End Class

You need just a small fix to output values, namely loop the array and append each array element to output text:

Public Class Form1
    Dim MyIntArr(2) As Integer
    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        Me.Close()

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        'Dim num1, num2, num3 As Integer
        Dim i As Integer

        MyIntArr(0) = CInt(TextBox1.Text)
        MyIntArr(1) = CInt(TextBox2.Text)
        MyIntArr(2) = CInt(TextBox3.Text)
        Array.Sort(MyIntArr)

        'outputBox.Text = MyIntArr(2)
        'MyIntArr(2) = outputBox.Text

        ' Loop the array
        outputBox.Text = ""
        For i = 0 To MyIntArr.GetUpperBound(0)
           outputBox.Text = outputBox.Text & MyIntArr(i).ToString  & ", "
        Next i

    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim i As Integer

        ' Loop the array
        outputBox.Text = ""
        For i = MyIntArr.GetUpperBound(0) To 0 Step -1
            ' Dump MyIntArr(i)
            outputBox.Text = outputBox.Text & MyIntArr(i).ToString  & ", "
        Next i
        'outputBox.Text = MyIntArr(i)
    End Sub
End Class

Thank you, but the outputbox will not populate with the values. I always have problems with populating textboxes. What am I missing?

Public Class Form1
    Dim MyIntArr(2) As Integer
    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        Me.Close()

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        'Dim num1, num2, num3 As Integer
        Dim i As Integer

        MyIntArr(0) = CInt(TextBox1.Text)
        MyIntArr(1) = CInt(TextBox2.Text)
        MyIntArr(2) = CInt(TextBox3.Text)
        
        Array.Sort(MyIntArr)

        'outputBox.Text = MyIntArr(2)
        'MyIntArr(2) = outputBox.Text

        outputBox.Text = ""
        For i = 0 To MyIntArr.GetUpperBound(0)

            outputBox.Text = outputBox.Text & MyIntArr(i).ToString & ", "

        Next i

    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim i As Integer

        For i = MyIntArr.GetUpperBound(0) To 0 Step -1
            ' Dump MyIntArr(i)
            outputBox.Text = outputBox.Text & MyIntArr(i).ToString & ", "
        Next i
        'outputBox.Text = MyIntArr(i)
    End Sub
End Class

Button1 does populate outputbox, right?
Let's fix Button2 code:

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim i As Integer

        MyIntArr(0) = CInt(TextBox1.Text)
        MyIntArr(1) = CInt(TextBox2.Text)
        MyIntArr(2) = CInt(TextBox3.Text)
        
        Array.Sort(MyIntArr)

        outputBox.Text = ""
        For i = MyIntArr.GetUpperBound(0) To 0 Step -1
            outputBox.Text = outputBox.Text & MyIntArr(i).ToString & ", "
        Next i

End Sub

Does it work now? If it doesn't, what's in the outputbox after pressing Button2?

still doesn't populate, does it matter that the outputbox is a listbox?
button1 should display numbers in list box in ascending order when pressed
button2 should display numbers in list box in descending order when pressed

see attached pic of my form (might be hard to see)

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        'Dim num1, num2, num3 As Integer
        Dim i As Integer

        MyIntArr(0) = CInt(TextBox1.Text)
        MyIntArr(1) = CInt(TextBox2.Text)
        MyIntArr(2) = CInt(TextBox3.Text)
        
        Array.Sort(MyIntArr)

        'outputBox.Text = MyIntArr(2)
        'MyIntArr(2) = outputBox.Text

        outputBox.Text = ""
        For i = 0 To MyIntArr.GetUpperBound(0)

            outputBox.Text = outputBox.Text & MyIntArr(i).ToString & ", "

        Next i

    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim i As Integer

        MyIntArr(0) = CInt(TextBox1.Text)
        MyIntArr(1) = CInt(TextBox2.Text)
        MyIntArr(2) = CInt(TextBox3.Text)

        Array.Sort(MyIntArr)

        outputBox.Text = ""
        For i = MyIntArr.GetUpperBound(0) To 0 Step -1
            ' Dump MyIntArr(i)
            outputBox.Text = outputBox.Text & MyIntArr(i).ToString & ", "
        Next i
        'outputBox.Text = MyIntArr(i)
    End Sub
End Class

BTW, neither button poulates the listbox (outputbox)

I see. You didn't mention it's a listbox :)

Replace both outputBox.Text = outputBox.Text & MyIntArr(i).ToString & ", " lines in Button_Click handlers with outputBox.Items.Add(MyIntArr(i)) and it works. ListBoxes are populated a bit differently than TextBoxes.
And you clear a ListBox with outputBox.Items.Clear instead of outputBox.Text = ""

Good Day Sir Teme,

Can you help me ? I need a program in VB 6.0 that the program ask the user to input 5 numbers in Inputbox then if the user wish to sort it in ascending and descending manners which is need two commandbutton and display it in listbox then the flow of the program is to build an array first then sort in ascending and descending manners then display it in the listbox . . . please reply as soon as you can . . . sorry for my bad english :(

For your knowledge, and only if you prefer to implement your own sorting algorithm, there are a bunch of them.

Hope this helps

FYI

Read the Daniweb's Member Rules first!

  1. Do read the forum description to ensure it is is relevant for your posting (this is not a VB6 forum)
  2. Do provide evidence of having done some work yourself if posting questions from school or work assignments (VB6 still in schools???)
  3. Do not hijack old threads by posting a new question as a reply to an old one (this thread originates from 2008!)

Name 'MyIntArr' is not declared. how to overcome this?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.