Simple Fixed Array: Part III

Ok, here is the last in this series of 'Simple Fixed Arrays.' 

This time we will replace Elements in the Array and update all the RadioButtons, Textbox, ComboBox and labels to reflect the changes we make.

Let's start as always with the Dim Statements

Dim arrAnimal(5) As String

Dim AnimalIndex As Integer

Dim i As Integer

Notice that this time the number inside the Parenthesis is 5, the reason for this is that VB numbers the Elements 0 to 5, so, because we have 6 Elements the array = 5,  strange but true. I know that we got away with it last time when we placed 6 inside the parenthesis but this time if we try to use 6 we get errors, try it and see what happens.

Lets move on to the Loading of the form, this time we fill the Array with the text of the 6 RadioButtons as follows.

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

        arrAnimal(0) = RadioButton1.Text

        arrAnimal(1) = RadioButton2.Text

        arrAnimal(2) = RadioButton3.Text

        arrAnimal(3) = RadioButton4.Text

        arrAnimal(4) = RadioButton5.Text

        arrAnimal(5) = RadioButton6.Text

        RadioButton1.Checked = True

End Sub

Next we need the RadioButton Events update the LblDisplay and change the ComboBox Text, this is done for all6 RadioButtons.

Private Sub RadioButton1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton1.CheckedChanged

 

LblDisplay.Text = "The " & RadioButton1.Text & Chr(13) & "is Element [" & (Array.IndexOf(arrAnimal, RadioButton1.Text)) & "]" & Chr(13) & "in the Array"

 cmbArrNum.Text = "0"

End Sub

Now let's add some code so that when we click the Load Button the Elements of the Array will show in a Text Box.

Private Sub btnShowAll_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnShowAll.Click

        ListBox1.Items.Clear()

        For i = 0 To 5

            ListBox1.Items.Add(arrAnimal(i) & " is element [" & i & "] in the array")

        Next

End Sub

The next button to sort out is the Sort Button this will place all the Elements of the Array in to Alphabetical order, it also updates the text for the 6 RadioButtons and the Label text.

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 = 0 To 5

            ListBox1.Items.Add(arrAnimal(i) & " is element [" & i & "] in the array")

        Next

        RadioButton1.Text = arrAnimal(0)

        RadioButton2.Text = arrAnimal(1)

        RadioButton3.Text = arrAnimal(2)

        RadioButton4.Text = arrAnimal(3)

        RadioButton5.Text = arrAnimal(4)

        RadioButton6.Text = arrAnimal(5)

        Select Case cmbArrNum.SelectedIndex

            Case 0

                RadioButton1.Checked = True

                lblDisplay.Text = "The " & RadioButton1.Text & Chr(13) & " is Element [" & (Array.IndexOf(arrAnimal, RadioButton1.Text)) & "]" & Chr(13) & "in the Array"

            Case 1

                RadioButton2.Checked = True

                lblDisplay.Text = "The " & RadioButton2.Text & Chr(13) & " is Element [" & (Array.IndexOf(arrAnimal, RadioButton2.Text)) & "]" & Chr(13) & "in the Array"

            Case 2

                RadioButton3.Checked = True

                lblDisplay.Text = "The " & RadioButton3.Text & Chr(13) & " is Element [" & (Array.IndexOf(arrAnimal, RadioButton3.Text)) & "]" & Chr(13) & "in the Array"

            Case 3

                RadioButton4.Checked = True

                lblDisplay.Text = "The " & RadioButton4.Text & Chr(13) & " is Element [" & (Array.IndexOf(arrAnimal, RadioButton4.Text)) & "]" & Chr(13) & "in the Array"

            Case 4

                RadioButton5.Checked = True

                lblDisplay.Text = "The " & RadioButton5.Text & Chr(13) & " is Element [" & (Array.IndexOf(arrAnimal, RadioButton5.Text)) & "]" & Chr(13) & "in the Array"

            Case 5

                RadioButton6.Checked = True

                lblDisplay.Text = "The " & RadioButton6.Text & Chr(13) & " is Element [" & (Array.IndexOf(arrAnimal, RadioButton6.Text)) & "]" & Chr(13) & "in the Array"

        End Select

        txtReplace.Text = ""

End Sub

That was a lot of code! Still it is Part 3 the final.

Let's move on and write some code to Replace Elements in the Array.

Once again because we have changed the Array we need to update all the Controls on the Form to keep everthing updated.

Private Sub btnReplace_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReplace.Click

        If txtReplace.Text = "" Then

            MessageBox.Show("No text has been entered")

            Exit Sub

        End If

        arrAnimal.SetValue(txtReplace.Text, cmbArrNum.SelectedIndex)

        Select Case cmbArrNum.SelectedIndex

            Case 0

                RadioButton1.Text = txtReplace.Text

                RadioButton1.ForeColor = Color.Blue

            Case 1

                RadioButton2.Text = txtReplace.Text

                RadioButton2.ForeColor = Color.Blue

            Case 2

                RadioButton3.Text = txtReplace.Text

                RadioButton3.ForeColor = Color.Blue

            Case 3

                RadioButton4.Text = txtReplace.Text

                RadioButton4.ForeColor = Color.Blue

            Case 4

                RadioButton5.Text = txtReplace.Text

                RadioButton5.ForeColor = Color.Blue

            Case 5

                RadioButton6.Text = txtReplace.Text

                RadioButton6.ForeColor = Color.Blue

        End Select

        ListBox1.Items.Clear()

        For i = 0 To 5

            ListBox1.Items.Add(arrAnimal(i) & " is element [" & i & "] in the array")

        Next

        arrAnimal(0) = RadioButton1.Text

        arrAnimal(1) = RadioButton2.Text

        arrAnimal(2) = RadioButton3.Text

        arrAnimal(3) = RadioButton4.Text

        arrAnimal(4) = RadioButton5.Text

        arrAnimal(5) = RadioButton6.Text

        lblDisplay.Text = "The " & txtReplace.Text & Chr(13) & " is Element [" & (Array.IndexOf(arrAnimal, txtReplace.Text)) & "]" & Chr(13) & "in the Array"

        txtReplace.Text = ""

End Sub

Well, that's it, in all it's glory, I hope you have enjoyed these 3 tutorials on Fixed Arrays.

I hope in the future to write other tutorials that will help beginners to better understand Visual Basic.net.

Up Next
    Ebook Download
    View all
    Learn
    View all