visual basic array sorting from list box - arrays

Hello im having trouble with my code !
we are asked to organize a list of names from a text.txt file the make them show up into a lits box (got that part down :) ) . then from the list box we are asked to create an array and sort that array (using our own sorting method) and organize the names using a button in assending order and another button organizing the array in decending order. the results from the orders names should appear in another list box .
i have gotten only the last name in the list to show up in the second list box but my code has no errors it just wont order the names properly! Help!!!!!
here is my code :)
Public Class FileSort
Dim sr As IO.StreamReader = IO.File.OpenText("C:\Users\Inspiron 15\documents\visual studio 2010\Projects\assigment4 EL\assigment4 EL\names.txt")
Structure names
Dim c As Integer
Dim fullname As String
End Structure
Dim allNames(99) As names
Private Sub btnName_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnName.Click
Do While sr.Peek <> -1
Name = sr.ReadLine
LstNames.Items.Add(Name & " ")
Loop
sr.Close()
End Sub
Private Sub bubbelsort(ByRef names() As System.String, ByVal c As Integer)
c = 0
names(c) = sr.ReadLine()
c = c * 1
For c = 1 To 99 Step +1 '~~~ Addding (Z to A) to the the Listbox
lstOrderedNames.Items.Add(Name & "")'
Next
End Sub
Private Sub BtnAssend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnAssend.Click
Dim names(99) As String
Dim c As Integer
c = 0
Dim A As Integer
A = 99
names(c) = sr.ToString
c = c + 1
For c = 1 To 99 Step +1 '~~~ Addding (Z to A) to the the Listbox
lstOrderedNames.Items.Add(Name & "")
Next
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnDessend.Click
Dim names(99) As String
Dim c As Integer
c = 0
Dim A As Integer
A = 99
names(c) = sr.ToString
names(A) = sr.ToString
A = A - 1
For A = 99 To 0 Step -1 '~~~ Addding (Z to A) to the the Listbox
lstOrderedNames.Items.Add(Name & "")
Next
End Sub
enter image description here

Since your problem is the sorting algorithm (if I understand this correctly).
At first we need an array.
Dim arr(ListBox1.Items.Count - 1) As String
For i As Integer = 0 To arr.Length - 1
arr(i) = CStr(ListBox1.Items(i))
Next
Next the sorting algorithm. Since you wanted to go with BubbleSort:
Private Sub StringBubbleSort(arr As String)
For i As Integer = 0 To arr.Length - 1
For j As Integer = 0 To arr.Length - 2 - i
If String.Compare(arr(j), arr(j + 1)) > 0 Then
Dim temp As String = arr(j)
arr(j) = arr(i)
arr(i) = temp
End If
Next
Next
End Sub
Then you use this function and copy the array to your second ListBox.
StringBubbleSort(arr)
ListBox2.Items.AddRange(arr)
String.Compare: https://msdn.microsoft.com/de-de/library/84787k22(v=vs.110).aspx

you could use linq
ListBox1.Items.Add("Battle")
ListBox1.Items.Add("Cattle")
ListBox1.Items.Add("apple")
ListBox2.DataSource = (From l In ListBox1.Items
Select l Order By l Ascending).ToList

Related

Create more than one type of arrays of PictureBox

I'm trying to create more than one array of PictureBox.
The only thing I know is that I can use limited amount of arrays that was created in a code.
Dim CustomImage() As PictureBox = New PictureBox() {Array1, Array2,..and so on}
But what I'm trying to explain is how do you create those arrays something like this?
In this example, the user inputs number 6 and it shows 6 PictureBoxes.
Dim ArrayNumber as Integer = 6
Dim CustomImage() As PictureBox = New PictureBox() {some code that creates 6 arrays}
Just sample:
Dim myPictBox As PictureBox()
Private Const maxPBCount = 10
Private Sub CreateSomePictureBox(ByVal Count As Int16)
If Count > maxPBCount Then
Exit Sub
End If
Dim myPictBoxLength As Integer = 0
If myPictBox Is Nothing = False Then
myPictBoxLength = myPictBox.Length
End If
Do While myPictBoxLength > Count
myPictBox(myPictBoxLength - 1).Dispose()
myPictBoxLength -= 1
Loop
If Count <= 0 Then
myPictBox = Nothing
Exit Sub
End If
ReDim Preserve myPictBox(Count - 1)
'Arrange Position of PictureBox
MsgBox(myPictBoxLength)
Do While myPictBoxLength < Count
myPictBox(myPictBoxLength) = New PictureBox
myPictBox(myPictBoxLength).Height = 30
myPictBox(myPictBoxLength).Width = 30
myPictBox(myPictBoxLength).Location = New Point(35 * myPictBoxLength, 30)
Me.Controls.Add(myPictBox(myPictBoxLength))
myPictBox(myPictBoxLength).BackColor = Color.Red
myPictBox(myPictBoxLength).Show()
myPictBoxLength += 1
Loop
'Create Sub Procedure to Arrange The Picture Box Created
'ArrangePB()
End Sub
Private Sub Form1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Click
CreateSomePictureBox(6)
End Sub

As sort an array of numbers in ascending order

I have a code to display a non-repeating random numbers, but I stuck by not sort in ascending order.
Here my code:
Public Class Form1
Dim intNumber As Integer
Dim arrNumber(0 To 5) As Integer
Dim i, x, y As Integer
Private Sub mostrar_resultados_sorteo(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
repetido()
For i = 0 To 5
ListBox1.Items.Add(arrNumber(i).ToString)
Next
End Sub
Private Sub repetido()
For x = 0 To 5
Start:
Randomize()
intNumber = Int((49 * Rnd()) + 1)
For y = 0 To 5
If intNumber = arrNumber(y) Then
GoTo Start
End If
Next y
arrNumber(x) = intNumber
Next x
ordenar()
End Sub
Private Sub ordenar()
End Sub
End Class
Your question is not tagged with programming language, but I guess it is VB.
Please see this link on how to sort an array.

join arrays and find matches

So i'm working on a project and i've hit a bit of a roadblock. i'm trying to compare two arrays that've been populated with read-in and split text files, find the matches of individual elements (names), then populate a listbox with the resulting matches. here's the code i have so far:
Dim sr As IO.StreamReader
Dim sw As IO.StreamWriter
Dim guest As String()
Dim gift As String()
Private Sub frmTYC_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim strFile1 As String
Dim strFile2 As String
Dim intCount As Integer
strFile1 = "Guests.txt"
strFile2 = "Gifts.txt"
intCount = 0
sr = IO.File.OpenText(strFile1)
Do While sr.Peek <> -1
guest = sr.ReadLine.Split(",")
lstGuests.Items.Add(guest(0))
intCount += 1
Loop
sr.Close()
intCount = 0
sr = IO.File.OpenText(strFile2)
Do While sr.Peek <> -1
gift = sr.ReadLine.Split(",")
lstGifts.Items.Add(gift(1) & " " & "gifted" & " " & gift(0))
intCount += 1
Loop
sr.Close()
End Sub
Private Sub btnList_Click(sender As Object, e As EventArgs) Handles btnList.Click
End Sub
i'm trying to compare guest(0) and gift(1) as those are the names, with guest having all the names and gift having all the names of the people in guest who gave a gift. i need the listbox to be populated on the list button click

How to remove duplicates from two dimensional Array in VBS

I have a two dimensional Array which is filled row by row. I got like ~200 entries. This is variable but also duplicates are filled.
How can i remove those duplicates? Or even check if the entries already exist in the Array and skip that duplicate entry?
for each oSingleNode in oNodeList
if oSingleNode.xml <> "" Then
Set oNode = oSingleNode.selectSingleNode("j.8:entity-reference")
if not oNode is nothing then
s = oNode.getAttribute("rdf:resource")
a = Split(s, "/")
attribute = a(ubound (a)-1)
Set oNodeTwo = oSingleNode.selectSingleNode("j.8:entity-label")
if not oNodeTwo is nothing then
label = oNodeTwo.text
array(rowIndex,index) = attribute
array(rowIndex,constClm)= label
debug2File array(rowIndex,index) & " " & array (rowIndex, constClm)
End if
End if
End If
The question was really interesting that's why a make a try on it.sorry for violating the tag since they ware requested for support in vb script, hope that this will make you think of my concept and make try the same in vb script, my result is as follows.
Dim a(10, 10), i, j As Integer
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
j += 1
If j > 10 Then
i += 1
j = 0
End If
check_val(CInt(TextBox1.Text), i, j)
End Sub
the function will check for duplicates and insert if not exist
Public Sub check_val(ByVal x As Integer, ByVal i As Integer, ByVal j As Integer)
Dim t As Integer = 0
For k As Integer = 0 To 10
For l As Integer = 0 To 10
If x = a(k, l) Then
t = 1
End If
Next
Next
If t = 0 Then
a(i, j) = x
Else
MsgBox("Repeting value")
j = j - 1
End If
End Sub

Get all posible combinations of array of arrays in vb.net

I have several arrays in VB.net. These arrays have different lengths and I need the combination between them. The simple solution is using nested loop as in the example:
Dim Array1= {10, 11, 12}
Dim Array2= {15}
Dim Array3= {1,2,3}
Dim array(2) As Object
array(0) = Array1
array(1) = Array2
array(2) = Array3
for (a = 1 to < Array1.Length - 1)
for (b = 1 to < Array2.Length - 1)
for (c = 1 to < Array3.Length - 1)
'Get combination
Next
Next
Next
Output: {10,15,1},{10,15,2},{10,15,3},{11,15,1},{11,15,2},...
But the real problem is that the number of arrays is not a fixed parameter (could be 3 as in the example or any other number) and therefore nested loops is not a solution.
Any idea?
Here's a quick rewrite of some code I wrote almost a decade ago while still using VB.Net 2003...what great memories lying around in my hard drive!
Hope you find its output useful:
10,15,1
10,15,2
10,15,3
11,15,1
11,15,2
11,15,3
12,15,1
12,15,2
12,15,3
The code that generated it:
Public Class Form1
Private WithEvents FC As ArrayCombinations = Nothing
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
If IsNothing(FC) Then
FC = New ArrayCombinations
FC.AddArray(New Integer() {10, 11, 12})
FC.AddArray(New Integer() {15})
FC.AddArray(New Integer() {1, 2, 3})
FC.GenerateCombos()
End If
End Sub
Private Sub FC_combinations(combos As System.Collections.Generic.List(Of System.Collections.Generic.List(Of Integer))) Handles FC.Combinations
For Each combo As List(Of Integer) In combos
Debug.Print(String.Join(",", combo.Select(Function(i) i.ToString())))
Next
FC = Nothing
End Sub
End Class
Public Class ArrayCombinations
Private Arrays As New List(Of Integer())
Private WithEvents BGW As New System.ComponentModel.BackgroundWorker
Public Event Combinations(ByVal combos As List(Of List(Of Integer)))
Public Sub New()
BGW.WorkerReportsProgress = True
End Sub
Public Sub AddArray(ByVal values() As Integer)
If Not BGW.IsBusy Then
Arrays.Add(values)
End If
End Sub
Public Sub GenerateCombos()
If Not BGW.IsBusy Then
BGW.RunWorkerAsync()
End If
End Sub
Private Sub BGW_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles BGW.DoWork
Dim sizes As New List(Of Integer)
Dim combinations As Integer
Dim remainder As Integer
Dim quotient As Integer
Dim combination As List(Of Integer)
Dim combos As New List(Of List(Of Integer))
If Arrays.Count > 0 Then
combinations = 1
For Each factor() As Integer In Arrays
sizes.Add(factor.Count)
combinations = combinations * factor.Count
Next
For i As Integer = 0 To combinations - 1
combination = New List(Of Integer)
For j As Integer = 1 To Arrays.Count
combination.Add(Nothing)
Next
quotient = i \ sizes.Item(sizes.Count - 1) ' Integer Division
remainder = i Mod sizes.Item(sizes.Count - 1)
combination(Arrays.Count - 1) = Arrays.Item(Arrays.Count - 1)(remainder)
For j As Integer = (sizes.Count - 2) To 0 Step -1
combination.Item(j) = Arrays.Item(j)(quotient Mod sizes.Item(j))
quotient = quotient \ sizes.Item(j) ' Integer Division
Next
combos.Add(combination)
Next
e.Result = combos
End If
End Sub
Private Sub BGW_RunWorkerCompleted(sender As Object, e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BGW.RunWorkerCompleted
RaiseEvent Combinations(e.Result)
End Sub
End Class
You can create a list to hold the combined values from all the arrays and then push those values back into an array, like this:
Dim listCombined = New List(Of Integer)
listCombined.AddRange(array1)
listCombined.AddRange(array2)
listCombined.AddRange(array3)
Dim arrayCombined As Integer() = listCombined.ToArray()
Note: This example assumes you have integer arrays, but you can change the type to whatever the arrays actually hold. You can also loop through the arrays instead of listing them one by one for the AddRange calls.

Resources