Simple Fixed Array: Part 3






1.50/5 (5 votes)
Part 3 of the Simple Fixed Array series (the final part).
Simple Fixed Array Part 3 (The Final)
Here is the last in this series of ‘Simple Fixed Arrays’. This time, we will replace elements in the array and update all the RadioButton
s, TextBox
,
ComboBox
, and Label
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 number = 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 will get errors; try it and see what happens.
Let's move on to the loading of the form. This time, we fill the array with the text of the six RadioButton
s, 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 to update LblDisplay
and change the ComboBox
text, this is done for all six RadioButton
s.
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 be show in a TextBox
.
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 alphabetical order; it also updates the text for the six RadioButton
s
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 the elements in the array. Once again, because we changed the array, we need to update all the controls on the Form to keep everything 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 its glory. I hope you have enjoyed these three tutorials on Fixed Arrays. I hope in the future to write other tutorials that will help beginners to better understand Visual Basic .NET.