Sorting: Ascending and Descending Order in VB6

Submitted by: 
Language: 
Visitors have accessed this post 10078 times.

In this article, we will create a program that can sort values into ascending or descending order in numbers in visual basic 6.0.

Now, let's start this tutorial!

1.Let's start this tutorial by following the following steps in Microsoft Visual Basic 6.0: Open Microsoft Visual Basic 6.0, click Choose Standard EXE, and click Open.

2. Next, add 2 Buttons named Command1 and labeled it as "Asc" for ascending order and Command2 and labeled it as "Desc" for descending order. Insert 3 textboxes named txtFN for first number, txtSN for second number, and txtTN for third number. Add also a ListBox named List1 for displaying the result. You must design your interface like this:

design

3. For Button1 as Ascending Button, put this code below:

If txtFN has the lowest value, it will automatically add first in the List. It will decide also if what number is the next lower value between txtSN and txtTN.

  1. If Val(txtFN.Text) <= Val(txtSN.Text) And Val(txtFN.Text) <= Val(txtTN.Text) Then
  2. List1.AddItem txtFN
  3. If Val(txtSN.Text) <= Val(txtTN.Text) Then
  4. List1.AddItem txtSN.Text
  5. List1.AddItem txtTN.Text
  6. Else
  7. List1.AddItem txtTN.Text
  8. List1.AddItem txtSN.Text
  9. End If

If txtSN has the lowest value, it will automatically add first in the List. It will decide also if what number is the next lower value between txtFN and txtTN.

  1. ElseIf Val(txtSN.Text) <= Val(txtFN.Text) And Val(txtSN.Text) <= Val(txtTN.Text) Then
  2. List1.AddItem txtSN
  3. If Val(txtFN.Text) <= Val(txtTN.Text) Then
  4. List1.AddItem txtFN.Text
  5. List1.AddItem txtTN.Text
  6. Else
  7. List1.AddItem txtTN.Text
  8. List1.AddItem txtFN.Text
  9. End If

If txtTN has the lowest value, it will automatically add first in the List. It will decide also if what number is the next lower value between txtFN and txtSN.

  1. Else
  2. List1.AddItem txtTN
  3. If Val(txtFN.Text) <= Val(txtSN.Text) Then
  4. List1.AddItem txtFN.Text
  5. List1.AddItem txtSN.Text
  6. Else
  7. List1.AddItem txtSN.Text
  8. List1.AddItem txtFN.Text
  9. End If
  10. End If

4. For Button2 as Ascending Button, put this code below:

If txtFN has the greatest value, it will automatically add first in the List. It will decide also if what number is the next greater value between txtSN and txtTN.

  1. If Val(txtFN.Text) >= Val(txtSN.Text) And Val(txtFN.Text) >= Val(txtTN.Text) Then
  2. List1.AddItem txtFN
  3. If Val(txtSN.Text) >= Val(txtTN.Text) Then
  4. List1.AddItem txtSN.Text
  5. List1.AddItem txtTN.Text
  6. Else
  7. List1.AddItem txtTN.Text
  8. List1.AddItem txtSN.Text
  9. End If

If txtSN has the greatest value, it will automatically add first in the List. It will decide also if what number is the next greater value between txtFN and txtTN.

  1. ElseIf Val(txtSN.Text) >= Val(txtFN.Text) And Val(txtSN.Text) >= Val(txtTN.Text) Then
  2. List1.AddItem txtSN
  3. If Val(txtFN.Text) >= Val(txtTN.Text) Then
  4. List1.AddItem txtFN.Text
  5. List1.AddItem txtTN.Text
  6. Else
  7. List1.AddItem txtTN.Text
  8. List1.AddItem txtFN.Text
  9. End If

If txtTN has the greatest value, it will automatically add first in the List. It will decide also if what number is the next greater value between txtFN and txtSN.

  1. Else
  2. List1.AddItem txtTN
  3. If Val(txtFN.Text) >= Val(txtSN.Text) Then
  4. List1.AddItem txtFN.Text
  5. List1.AddItem txtSN.Text
  6. Else
  7. List1.AddItem txtSN.Text
  8. List1.AddItem txtFN.Text
  9. End If
  10. End If

5. Filter all the textboxes that it can only input numbers. Put this code below:

  1. Private Sub txtFN_KeyPress(KeyAscii As Integer)
  2. Select Case KeyAscii
  3. Case Is < 32
  4. Case 48 To 57
  5. Case Else
  6. KeyAscii = 0
  7. End Select
  8. End Sub
  9.  
  10. Private Sub txtSN_KeyPress(KeyAscii As Integer)
  11. Select Case KeyAscii
  12. Case Is < 32
  13. Case 48 To 57
  14. Case Else
  15. KeyAscii = 0
  16. End Select
  17. End Sub
  18.  
  19. Private Sub txtTN_KeyPress(KeyAscii As Integer)
  20. Select Case KeyAscii
  21. Case Is < 32
  22. Case 48 To 57
  23. Case Else
  24. KeyAscii = 0
  25. End Select
  26. End Sub


Output:

outputoutput

Download the source code and try it! :)

For more inquiries and need programmer for your thesis systems in any kind of programming languages, just contact my number below and hire me.

Best Regards,

Engr. Lyndon R. Bermoy
IT Instructor/System Developer/Android Developer
Mobile: 09488225971
Telephone:826-9296
E-mail:[email protected]

Follow and add me in my Facebook Account: https://www.facebook.com/donzzsky

Visit my page on Facebook at: https://www.facebook.com/BermzISware


Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.