Click here to Skip to main content
15,889,403 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Visual Basic 2008
The user selects the decimal numbers to be put on the list ( for example: 4; 0,3333; 0,75; 2,125 etc).
List of decimal numbers must be arranged in listbox from smallest to bigest( 0,3333; 0,75; 2,125; 4; etc).
I have made this code and it works great for me.


VB
Option Strict On
Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        If TextBox1.Text <> "" Then
            ListBox1.Items.Add(TextBox1.Text)
        Else
            MsgBox("Empty field")
           End If
        TextBox1.Text = ""
      
    End Sub
  TextBox1.Select()


    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ListBox1.Items.Clear()
        TextBox1.Select()
    End Sub

    Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged

        Dim List As New List(Of Decimal)
        For Each ListBoxItem In ListBox1.Items
            List.Add(CDec(ListBoxItem))
        Next

        List.Sort()
        ListBox1.Items.Clear()

        For Each ListItem In List
            If Not ListBox1.Items.Contains(ListItem) Then 
                ListBox1.Items.Add(ListItem)
            Else
                MsgBox("Already exist ")
            End If
        Next

    End Sub
End Class

But for this specific application is more appropriate if user enters the numbers in fractional form, ie 4; 1/3; 3/4; 17/8 etc ;
This list must also be sorted from the smallest to the largest number ie 1/3; 3/4;17/8; 4 etc .
How to do it?.
Posted

1 solution

This is called "rational numbers": http://en.wikipedia.org/wiki/Rational_number[^].

You need to learn their theory a bit (a big deal of their basics should be taught in elementary school) and develop their arithmetic. For representation of them, you can use the structure or a class with two integer member: numerator and denominator. You need to be able to factorize each of them into the product of prime numbers. You will need it to simplify (reduce) rational numbers such as: 6/8 = 3/4. If your integer member are not too big numbers, it will be a quite fast operation, but it will be extremely slow for big numbers.

Therefore, you can use a different approach, the approach with some redundancy use to improve performance.

You can work with prime numbers in first place. Your numerator and denominator could be presented as a list of prime number representing their product, System.Collection.Generic.List<int>, where each list member is a prime number. If you, say, multiply a rational number by an integer, you should factorize this integer into a product of prime numbers in first place, then you add all prime factors to your list, and then you reduce your rational number, immediately, by removing identical prime factors from both lists by pairs, from numerator and denominator. I hope this is clear. Division is made in the same fashion, but numerator and denominator roles are inverted. When these operations with integers are done, you can define multiplication and division of rational numbers. I hope you already understand how.

Now, addition and subtraction are implemented using the method of the common denominator. Remember the elementary school.

Please see:
http://en.wikipedia.org/wiki/Factorization[^],
http://en.wikipedia.org/wiki/Prime_number[^],
http://en.wikipedia.org/wiki/Redundancy_%28information_theory%29[^].

Now, working with the list of ComboBox don't store strings. Store the objects of some structures or classes which helps you to work with list elements. For example, it could be the structures implementing the rational number arithmetic I described above. One of the biggest fallacies of the beginners these days is the trend to work with strings representing data instead of data itself. Don't do this mistake.

The only problem is: what will be shown in UI when you add such list element? The answer is simple: whatever the method ToString() returns. So, in your class or structure, also override System.Object.ToString(), to show "3/4" based on your numerator/denominator value, not structure name.

[EDIT]

For more detail on this technique, please see my past answers:
Displaying an image from a list box[^],
combobox.selectedvalue shows {} in winform[^].

—SA
 
Share this answer
 
v3
Comments
Matt T Heffron 18-Sep-13 15:06pm    
+5
Sergey Alexandrovich Kryukov 18-Sep-13 15:07pm    
Thank you, Matt.
—SA
zlatkodo 20-Sep-13 23:44pm    
Hi,S.A.K.
Why you answer the question if you do not want or do not know to answer?
Your attitude, pointless theorizing and knowledge copied from Wikipedia are completely unnecessary and useless.
Also, the numbers 3/4 or 35/66 ARE FRACTIONAL NUMBERS. See: http://en.wikipedia.org/wiki/Fraction_%28mathematics%29

In case you are the only person who answers the questions here, I will ask my future questions somewhere else.

By the way, if someone needs, here is a solution (in just a couple of lines of code):
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim DT As New DataTable
Dim myArrayOfFractions() As String = ListBox1.Items.Cast(Of String).ToArray

With ListBox1.Items
.Clear()
.AddRange(myArrayOfFractions.OrderBy(Function(x) CDec(DT.Compute(x, Nothing))).ToArray)
End With
End Sub

Не тот грамотен, кто читать умеет, а тот, кто слушает да разумеет.
Sergey Alexandrovich Kryukov 21-Sep-13 0:03am    
You are as rude as illiterate. Those are rational numbers. "Fractional" is more general category as rational, rational numbers is a subset of rational.

Usually, rudeness is proportional to the illiteracy. Anti-intellectualism is very familiar, too.
And I never copy anything without proper attribution. So, in addition, you are also a liar.

—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900