8 Replies - 1272 Views - Last Post: 26 July 2012 - 08:39 AM Rate Topic: -----

#1 Jpopto  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 31
  • Joined: 04-April 12

Preserve first array and Sort array in listbox

Posted 12 July 2012 - 07:55 PM

    Dim NamesArr() As String ' array
    Dim MaxNum As Integer 'How many values the user wishes to enter
    Dim index As Integer ' 

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load


        btnEnter.Enabled = False ' Initialize retry button to disable
        btnSearch.Enabled = False ' Initialize Search button to disable



    End Sub

    Private Sub btnNames_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNames.Click




        btnEnter.Enabled = False ' Initialize retry button to disable
        btnSearch.Enabled = False ' Initialize Search button to disable

        Dim count As Integer
        Dim TestNumber As String

        lstNames.Items.Clear()
        

        TestNumber = txtNumberNames.Text
        






        If IsNumeric(TestNumber) = True Then
            MaxNum = Val(txtNumberNames.Text)    'Get how many numbers the user will enter

            If MaxNum >= 0 Then

                ReDim NamesArr(MaxNum)

                While count < MaxNum 'Keep going until MaxNum is reached





                    For index = LBound(NamesArr) To UBound(NamesArr) - 1
                        NamesArr(index) = InputBox("Please enter name number " & count + 1)
                        count = count + 1

                    Next index



                    btnEnter.Enabled = True ' Initialize retry button to invisible
                End While 'End While count < MaxNum
                'Loop ends when count = the number of values user wanted to enter
            Else
                MsgBox("Please enter a number greater than 0")
                txtNumberNames.Focus()
                txtNumberNames.Clear()
            End If
        Else

            MsgBox("Please enter a real Number")
            txtNumberNames.Focus()
            txtNumberNames.Clear()

        End If











    End Sub

    Private Sub btnEnter_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEnter.Click

        For index = LBound(NamesArr) To MaxNum - 1
            lstNames.Items.Add(NamesArr(index))
        Next index

        btnSearch.Enabled = True









    End Sub

    Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearch.Click



        Dim Message As String



        Message = InputBox("Enter your search term")
        index = LBound(NamesArr)
        Dim running As Boolean


        While running = False And index <= UBound(NamesArr)
            If NamesArr(index) = Message Then
                running = True

            Else
                index = index + 1

            End If
        End While

        If running = True Then
            MsgBox(" The name" & " " & Message & " has been found in cell # " & " " & index + 1)

        ElseIf running = False Then

            MsgBox(" The name was not found")

        End If

        txtNumberNames.Focus()
        txtNumberNames.Clear()








    End Sub

    Private Sub btnSort_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSort.Click

        Dim temp As String
        Dim i As Integer
        Dim j As Integer


        For i = 0 To (UBound(NamesArr) - 1)
            For j = 1 To (UBound(NamesArr))
                If NamesArr(j) < NamesArr(j - 1) Then
                    temp = NamesArr(j)
                    NamesArr(j) = NamesArr(j - 1)
                    NamesArr(j - 1) = temp
                End If
            Next j
        Next i


    End Sub
End Class


I need to preserve the array and be able to add names to it. I also need to be able to sort it alphabetically.

Is This A Good Question/Topic? 0
  • +

Replies To: Preserve first array and Sort array in listbox

#2 BobRodes  Icon User is offline

  • Your Friendly Local Curmudgeon
  • member icon

Reputation: 547
  • View blog
  • Posts: 2,904
  • Joined: 19-May 09

Re: Preserve first array and Sort array in listbox

Posted 13 July 2012 - 07:18 PM

You know, this is the sort of thing that irritates people on here. What's so difficult about typing "vb array preserve" in google and reading the first link that comes up, and then typing "vb array sort" in google, and doing the same? I mean, if a servant is what you want, you ought to pay at least minimum wage, you know?
Was This Post Helpful? 0
  • +
  • -

#3 Ionut  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 385
  • View blog
  • Posts: 1,053
  • Joined: 17-July 10

Re: Preserve first array and Sort array in listbox

Posted 14 July 2012 - 03:20 AM

Here is VB.Net, so start taking advantage of .Net framework. You can use a List(of String) that will keep all the values and, in addition, has a method named Sort already implemented for you. All you have to do is to call it.
Was This Post Helpful? 0
  • +
  • -

#4 BobRodes  Icon User is offline

  • Your Friendly Local Curmudgeon
  • member icon

Reputation: 547
  • View blog
  • Posts: 2,904
  • Joined: 19-May 09

Re: Preserve first array and Sort array in listbox

Posted 17 July 2012 - 07:44 PM

True, and Arrays have a Sort method too, don't they?
Was This Post Helpful? 0
  • +
  • -

#5 Jpopto  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 31
  • Joined: 04-April 12

Re: Preserve first array and Sort array in listbox

Posted 18 July 2012 - 09:23 PM

View PostBobRodes, on 17 July 2012 - 07:44 PM, said:

True, and Arrays have a Sort method too, don't they?


I have been able to sort my listbox using bubble method. The last problem I have is being able to increase the array by 1 every time the user hits the "Add Names" button. I tried Redim Preserve but it Preserves my first list but replaces my last increment everytime I click the add names. Here is my code. Any help is appreciated.

Dim NamesArr() As String ' array
    Dim MaxNum As Integer 'How many values the user wishes to enter
    Dim index As Integer ' 
    Dim count As Integer
    Dim NumCells As Integer

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load


        btnEnter.Enabled = False ' Initialize retry button to disable
        btnSearch.Enabled = False ' Initialize Search button to disable



    End Sub

    Private Sub btnNames_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNames.Click




        btnEnter.Enabled = False ' Initialize retry button to disable
        btnSearch.Enabled = False ' Initialize Search button to disable


        Dim TestNumber As String




        lstNames.Items.Clear()
        

        TestNumber = txtNumberNames.Text
        






        If IsNumeric(TestNumber) = True Then
            MaxNum = Val(txtNumberNames.Text)    'Get how many numbers the user will enter
            ReDim NamesArr(MaxNum)


            If MaxNum >= 0 Then




                'Keep going until MaxNum is reached



                For index = LBound(NamesArr) To MaxNum - 1
                    NamesArr(index) = InputBox("Please enter name number " & count + 1)
                    count = count + 1

                Next index





                btnEnter.Enabled = True ' Initialize retry button to invisible
                'End While count < MaxNum
                'Loop ends when count = the number of values user wanted to enter
            Else
                MsgBox("Please enter a number greater than 0")
                txtNumberNames.Focus()
                txtNumberNames.Clear()
            End If

        Else

            MsgBox("Please enter a real Number")
            txtNumberNames.Focus()
            txtNumberNames.Clear()

        End If








    End Sub

    Private Sub btnEnter_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEnter.Click

        lstNames.Items.Clear()

        For index = LBound(NamesArr) To UBound(NamesArr) - 1
            lstNames.Items.Add(NamesArr(index))
        Next index


        btnSearch.Enabled = True

        



    End Sub

    Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearch.Click



        Dim Message As String



        Message = InputBox("Enter your search term")
        index = LBound(NamesArr)
        Dim running As Boolean


        While running = False And index <= UBound(NamesArr)
            If NamesArr(index) = Message Then
                running = True

            Else
                index = index + 1

            End If
        End While

        If running = True Then
            MsgBox(" The name" & " " & Message & " has been found in cell # " & " " & index + 1)

        ElseIf running = False Then

            MsgBox(" The name was not found")

        End If

        txtNumberNames.Focus()
        txtNumberNames.Clear()








    End Sub

    Private Sub btnSort_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSort.Click

        Dim temp As String
        Dim i As Integer
        Dim j As Integer

        lstNames.Items.Clear()

        For i = 0 To (UBound(NamesArr) - 1)
            For j = 1 To (UBound(NamesArr))
                If NamesArr(j) < NamesArr(j - 1) Then
                    temp = NamesArr(j)
                    NamesArr(j) = NamesArr(j - 1)
                    NamesArr(j - 1) = temp


                End If
            Next j
        Next i

        For i = 1 To UBound(NamesArr)
            lstNames.Items.Add(NamesArr(i))
        Next



    End Sub

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




        ReDim Preserve NamesArr(MaxNum + 1)



        For index = MaxNum To UBound(NamesArr) - 1
            NamesArr(index) = InputBox("Please enter name number " & count + 1)
            count = count + 1

        Next index


    End Sub
End Class

Was This Post Helpful? 0
  • +
  • -

#6 BobRodes  Icon User is offline

  • Your Friendly Local Curmudgeon
  • member icon

Reputation: 547
  • View blog
  • Posts: 2,904
  • Joined: 19-May 09

Re: Preserve first array and Sort array in listbox

Posted 19 July 2012 - 03:48 PM

The way to do this is
Redim for the first size.
Every time you want to add a value to the array, use redim preserve ubound + 1.

I have no idea what the value of MaxNum is when you do your Redim, but it's probably 1 less than ubound.

Sure, you can implement a bubble sort method for the mental exercise. Or you could just say NamesArr.Sort.

This post has been edited by BobRodes: 19 July 2012 - 03:52 PM

Was This Post Helpful? 0
  • +
  • -

#7 rgfirefly24  Icon User is online

  • D.I.C Lover
  • member icon


Reputation: 233
  • View blog
  • Posts: 1,311
  • Joined: 07-April 08

Re: Preserve first array and Sort array in listbox

Posted 19 July 2012 - 04:17 PM

if you need to use an array and need to preserve the data use ReDim Preserve.

Also you are redimming the array to add another element, but your using maxNum which would be what ever your original array size was. So for this line:

For index = MaxNum To UBound(NamesArr) - 1



try changing it to

For index = MaxNum + 1 TO UBound(NamesArr) -1


This post has been edited by rgfirefly24: 19 July 2012 - 04:23 PM

Was This Post Helpful? 0
  • +
  • -

#8 Jpopto  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 31
  • Joined: 04-April 12

Re: Preserve first array and Sort array in listbox

Posted 20 July 2012 - 02:09 AM

View PostBobRodes, on 19 July 2012 - 03:48 PM, said:

The way to do this is
Redim for the first size.
Every time you want to add a value to the array, use redim preserve ubound + 1.

I have no idea what the value of MaxNum is when you do your Redim, but it's probably 1 less than ubound.

Sure, you can implement a bubble sort method for the mental exercise. Or you could just say NamesArr.Sort.


i'm not able to redim ubound + 1 without getting compiler error "Error 1 'ReDim' statements require a parenthesized list of the new bounds of each dimension of the array '
Was This Post Helpful? 0
  • +
  • -

#9 BobRodes  Icon User is offline

  • Your Friendly Local Curmudgeon
  • member icon

Reputation: 547
  • View blog
  • Posts: 2,904
  • Joined: 19-May 09

Re: Preserve first array and Sort array in listbox

Posted 26 July 2012 - 08:39 AM

Well, that means that you haven't quite worked through the syntax correctly. Break it apart. Let's say that your upper bound is currently 100. You now want it to be 101. So:
ReDim Preserve NamesArr(101)
will get the job done. Now how would you use UBound + 1 to get the value 101? Once you have that, substitute it in the above code for the 101.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1