Click here to Skip to main content
15,891,033 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
How to move value in ListBox2(with Duplicate value) to ListBox4 (Non Duplicate value)? The condition, each value must >= or <= 3 from other.
For example:
Each ListBox have 5 values.
ListBox2:
6
8
8
12
15

ListBox4:
6
9 --&gt; 8 become 9, because 8-6=2 (Must &gt;= 3)
20 --&gt; 8 become 20, because we random the value
12
15

Here's my code :

Private Sub MaxGenome_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MaxGenome.TextChanged
        Dim i, nilai As Integer
        ListBox2.Items.Clear()
        ListBox4.Items.Clear()
        For i = 1 To CInt(PjgGen.Text)
            nilai = GeneticAlgorithm.PopuMain.Best_member.Genome(i)
            If Not (ListBox4.Items.Contains(nilai - 1) OrElse ListBox4.Items.Contains(nilai - 2) OrElse ListBox4.Items.Contains(nilai) OrElse ListBox3.Items.Contains(nilai + 1) OrElse ListBox4.Items.Contains(nilai + 2)) Then
                ListBox4.Items.Add(nilai)
            Else
                While (ListBox4.Items.Contains(nilai - 1) OrElse ListBox4.Items.Contains(nilai - 2) OrElse ListBox4.Items.Contains(nilai) OrElse ListBox3.Items.Contains(nilai + 1) OrElse ListBox4.Items.Contains(nilai + 2))
                    Do
                        nilai = Rand_between(1, CInt(record.Text))
                    Loop
                    If Not (ListBox4.Items.Contains(nilai - 1) OrElse ListBox4.Items.Contains(nilai - 2) OrElse ListBox4.Items.Contains(nilai) OrElse ListBox3.Items.Contains(nilai + 1) OrElse ListBox4.Items.Contains(nilai + 2)) Then
                        ListBox4.Items.Add(nilai)
                    End If
                End While
            End If
            ListBox2.Items.Add(GeneticAlgorithm.PopuMain.Best_member.Genome(i))
        Next i
    End Sub


This operation begin when TextBox2.TextChanged. Every TextBox2.TextChanged, this operation will be called. I'm clear the ListBox Item for space to the next value.

Sorry my english is bad, just focus on the example, that's the main point of my question, thanks Smile :)
Posted
Updated 24-May-11 8:38am
v5
Comments
Fabio V Silva 15-May-11 9:50am    
Edited formatting.
Abdi tombang 15-May-11 21:09pm    
Anyone can help me with this problem??
Venkatesh Mookkan 15-May-11 22:40pm    
I don't know how this could work in the first place.

You are clearing the ListBox4.Items in the beginning and enumerating in a for loop. Ultimately, you would get run-time exception (IndexOutOfRangeException - I guess).
Abdi tombang 15-May-11 23:01pm    
Oh, this operation begin when TextBox2.TextChanged. Every TextBox2.TextChanged, this operation will be called. I'm clear the ListBox Item for space to the next value.

Private Sub MaxGenome_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MaxGenome.TextChanged

1 solution

I do not know VB.NET well. But you can give it a shot with Linq[^]. There is a nice method Intersect[^] and Except[^]

With this to method you can find things that are in common in a easy way.
 
Share this answer
 

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