Sometime when you have a lot of items in Listbox and you want to know which item is the most repeated. Show below is the code to find the most repeated item using VB.Net. In this tutorial, I use 3 controls:
No | Control Type | Name | Text |
1 | Listbox | ListboxData | |
2 | Label | LabelShowMostRepeatedItem | |
3 | Button | ButtonShowRepeate | Show Most Repeated Item |
- We have add items to item collection of listbox as below
- Banana
- Mango
- Banana
- Apple
- Orange
- Lemon
- Apple
- Fruit
- Apple
- Apple
- And we use event_click on Button: ButtonShowRepeate to show the most repeated items in Listbox which is displayed in Label: LabelShowMostRepeatedItem.
Code
Public Class FormMostRepeatedItem
Private Sub ButtonShowRepeate_Click(sender As Object, e As EventArgs) Handles ButtonShowRepeate.Click
Try
Dim o1, o2, o3 As Object
Dim i1, i2 As Integer
For Each o1 In ListBoxData.Items
For Each o2 In ListBoxData.Items
If o1 = o2 Then
i1 += 1
End If
Next
If i1 > i2 Then
i2 = i1
o3 = o1
End If
i1 = 0
Next
LabelShowMostRepeatedItem.Text = ("The most Repeated Fruits is " & o3.ToString & " is repeated " & i2 & " times")
Catch ex As Exception
End Try
End Sub
End Class
0 comments:
Post a Comment