Welcome to Dream.In.Code
Getting Help is Easy!

Join 132,437 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,477 people online right now. Registration is fast and FREE... Join Now!




Simple Fixed Array Part 2

 
Reply to this topicStart new topic

> Simple Fixed Array Part 2, Find Element, Sort Array and Fill a ListBox

Graham
Group Icon



post 14 Jun, 2006 - 11:12 PM
Post #1


In the last tutorial Simple Fixed Array Part1 we had a fixed Array of Elements (Animals), and when we selected one of the Radio Buttons a Label would show us which number the Element was.
But what if we had more than just six Elements as in our Array, say we had 500 Elements and we needed to find out which Element Number the Giraffe was? Well that’s where Array.IndexOf() comes in to play.
So, in Part Two of this tutorial, we will use Array.IndexOf() we will also place all the Elements into a list box and then we will use Array.Sort() to rearrange the Elements into alphabetical order.

So let’s start with the Dim statements as follows,

CODE

Dim arrAnimal(6) As String
Dim AnimalIndex As Integer
           Dim i As Integer


You will notice a change this time in the way we have dimensioned the Array, last time we placed the Elements like this
Dim arrAnimal() as String{“Lion”,”Tiger”,Etc} and this time we have not placed the Elements in the Array, we have just set a number inside the parentheses to let the Array know that we intend to place 6 Elements inside at a later stage in the program.

The next piece of code is when the Form_Loads this is the point in this program when the Elements are placed inside the Array.
CODE

Private Sub AnimalArray_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
                    arrAnimal(0) = "Lion"
arrAnimal(1) = "Tiger"
arrAnimal(2) = "Giraffe"
arrAnimal(3) = "Bear"
arrAnimal(4) = "Elephant"
arrAnimal(5) = "Monkey"
    End Sub


Now lets change the RadioButton1_CheckedChanged Event so that when it is checked the lblDisplay.Text will show us which Element Number the Animal is, you will also notice that we are using concatenation the & character to join different parts of the text together, we have also used Chr(13) this is the Enter button on the keyboard and tells the program to place the text that comes after it on to a new line. In the case of RadioButton1 we want to know which Element Number the Lion is (Array.IndexOf(arrAnimal, "Lion")

CODE

Private Sub RadioButton1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton1.CheckedChange
    lblDisplay.Text = "The Lion" & Chr(13) & "is Element [" &
   (Array.IndexOf(arrAnimal, "Lion")) & "]" & Chr(13) & "in the Array"
End Sub


We do the same for the other five RadioButtons in our program.

The next piece of code places all of the Element in our Array into a list box when we click Button1 using a For Next Loop, notice the Numbers 0 to 5 this is because in an Array the first Element is 0
CODE

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnShowall.Click
   For i = 0 To 5
       ListBox1.Items.Add(arrAnimal(i) & " is element [" & i & "] in the array")
         Next
End Sub


Now that the Elements are in the ListBox we can click on Button2 and get them sorted into alphabetical order using Array.Sort() like this. Notice that before we sort the Elements we Clear the contents of the ListBox first using ListBox1.Items.Clear()
After you have Sorted the Array if you click on one of the RadioButtons you will see that the Element Number now matches the Sorted Array, so that the Bear, Element Number [3] unsorted, is now Element Number [1]
CODE

Private Sub btnSort_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSort.Click
ListBox1.Items.Clear()
Array.Sort(arrAnimal)
For i = 1 To 6
  ListBox1.Items.Add(arrAnimal(i) & " is element [" & i & "] in the array")
Next
End Sub


Here is the complete code for you to Copy ( Ctrl C ) and Paste ( Ctrl V )
If you just want to move on to part3 in this series then here it is Simple Fixed Array Part3
CODE

Public Class AnimalArray
    Dim arrAnimal(6) As String
    Dim AnimalIndex As Integer
    Dim i As Integer

Private Sub AnimalArray_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        arrAnimal(0) = "Lion"
        arrAnimal(1) = "Tiger"
        arrAnimal(2) = "Giraffe"
        arrAnimal(3) = "Bear"
        arrAnimal(4) = "Elephant"
        arrAnimal(5) = "Monkey
End Sub
Private Sub RadioButton1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton1.CheckedChanged
        LblDisplay.Text = "The Lion" & Chr(13) & "is Element [" & (Array.IndexOf(arrAnimal, "Lion")) & "]" & Chr(13) & "in the Array"
End Sub
Private Sub RadioButton2_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton2.CheckedChanged
        LblDisplay.Text = "The Tiger" & Chr(13) & "is Element [" & (Array.IndexOf(arrAnimal, "Tiger")) & "]" & Chr(13) & "in the Array"
End Sub
Private Sub RadioButton3_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton3.CheckedChanged
        LblDisplay.Text = "The Giraffe" & Chr(13) & "is Element [" & (Array.IndexOf(arrAnimal, "Giraffe")) & "]" & Chr(13) & "in the Array"
End Sub
Private Sub RadioButton4_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton4.CheckedChanged
        LblDisplay.Text = "The Bear" & Chr(13) & "is Element [" & (Array.IndexOf(arrAnimal, "Bear"))  & "]" & Chr(13) & "in the Array"
End Sub
Private Sub RadioButton5_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton5.CheckedChanged
        LblDisplay.Text = "The Elephant" & Chr(13) & " is Element [" & (Array.IndexOf(arrAnimal, "Elephant")) & "]" & Chr(13) & "in the Array"
End Sub
Private Sub RadioButton6_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton6.CheckedChanged
        LblDisplay.Text = "The Monkey" & Chr(13) & " is Element [" & (Array.IndexOf(arrAnimal, "Monkey")) & "]" & Chr(13) & "in the Array"
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnShowall.Click
For i = 0 To 5
ListBox1.Items.Add(arrAnimal(i) & " is element [" & i & "] in the array"
Next
End Sub

Private Sub btnSort_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSort.Click
        ListBox1.Items.Clear()
        Array.Sort(arrAnimal)
For i = 1 To 6
         ListBox1.Items.Add(arrAnimal(i) & " is element [" & i & "] in the array")
Next
End Sub

End Class


This post has been edited by Graham: 30 Jun, 2006 - 12:02 AM
Go to the top of the page
+Quote Post


Register to Make This Ad Go Away!

sam.adams61
*



post 14 Jul, 2008 - 03:06 PM
Post #2
Hey, Graham, as with the first Tutorial Simple Array 1, this second was spot on...just what the Dr ordered, you might say. Again, many thanks for your interest in beginners such as I. icon_up.gif
Go to the top of the page
+Quote Post


Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 

Lo-Fi Version Time is now: 11/22/08 10:19AM

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month