Question Textboxes and control arrays

DDTdevelop

New member
Joined
Aug 17, 2011
Messages
3
Programming Experience
5-10
Hi all,
as always I'm here asking you a probably stupid question, but I'm upgrading an old vb 6 project, so I'd like to have all the new code more object oriented and as compact as possible.

I have a groupbox containing some textboxes. Each of them is filled with the "selecteditem.tostring" property of a listbox everytime I press a button (call it "OK BUTTON"), and everything works fine.
Code below:
VB.NET:
        For Each ctrl As Control In GroupBox1.Controls
            If TypeOf ctrl Is TextBox Then
                If ctrl.Text = "" Then
                    ctrl.Text = ListBox1.SelectedItem.ToString
                    Exit For
                End If
            End If
        Next

Now I have to do the opposite, regarding the "CANCEL/REMOVE" button. Everytime i press it once, i need to empty the texbox.text property to a void string. It works good if i do it from the top to the bottom with code similar to the one posted before. But i need to go the opposite way, from last textbox to first. Any suggestions? In the old version of code I used a long if/then for every textbox, but it was horrible to see!
Propbably I'm simply tired and asking something very stupid, but cannot figure it out.

Thanks in advance.
 
I don't know if this will work like you need it to but try using a For i loop instead of for each loop

VB.NET:
for i as integer = groupbox1.controls.count -1 to 0
....
next
 
Control Arrays in reverse

Here is some sample code to show how a control array of textboxes works both forward and reverse, using two different techniques. I placed 6 textboxes on a form, but you may add GroupBox. just before Control in the code.



VB.NET:
Public Class Form1

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        'forward
        Dim num As Integer = 0
        For Each ctl As Control In Controls
            If TypeOf ctl Is TextBox Then
                num += 1
                ctl.Text = num.ToString
            End If
        Next ctl
    End Sub

    Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
        'reverse
        Dim num As Integer = 7
        For Each ctl As Control In Controls
            If TypeOf ctl Is TextBox Then
                num -= 1
                ctl.Text = num.ToString
            End If
        Next ctl
    End Sub

    Private Sub Button3_Click(sender As System.Object, e As System.EventArgs) Handles Button3.Click
        'forward
        Dim num As Integer = 0
        For Each ctl As TextBox In Controls.OfType(Of TextBox)()
            num += 1
            ctl.Text = num.ToString
        Next ctl
    End Sub

    Private Sub Button4_Click(sender As System.Object, e As System.EventArgs) Handles Button4.Click
        'reverse
        Dim num As Integer = 0
        For Each ctl As TextBox In Controls.OfType(Of TextBox)().Reverse
            num += 1
            ctl.Text = num.ToString
        Next ctl
    End Sub

End Class
 
Last edited:
Looping through a list backwards is something that we all need to do at some stage, so it's good that you learned that. In this case though, why do you need a loop at all?
Public Class Form1

    Private textBoxes As TextBox()
    Private firstEmptyTextBoxIndex As Integer = 0

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        textBoxes = New TextBox() {TextBox1,
                                   TextBox2,
                                   TextBox3,
                                   TextBox4,
                                   TextBox5,
                                   TextBox6}
    End Sub

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        If firstEmptyTextBoxIndex = textBoxes.Length Then
            MessageBox.Show("All fields are full.")
        Else
            textBoxes(firstEmptyTextBoxIndex).Text = ListBox1.Text
            firstEmptyTextBoxIndex += 1
        End If
    End Sub

    Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
        If firstEmptyTextBoxIndex = 0 Then
            MessageBox.Show("All fields are empty.")
        Else
            firstEmptyTextBoxIndex -= 1
            textBoxes(firstEmptyTextBoxIndex).Clear()
        End If
    End Sub

End Class
If you're using .NET 3.5 or later then you could also use LINQ, which is much like using a loop but the code is much more succinct:
Public Class Form1

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        Dim firstEmptyTextBox = Controls.OfType(Of TextBox)().FirstOrDefault(Function(tb) tb.TextLength = 0)

        If firstEmptyTextBox Is Nothing Then
            MessageBox.Show("All fields are full.")
        Else
            firstEmptyTextBox.Text = ListBox1.Text
        End If
    End Sub

    Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
        Dim lastFullTextBox = Controls.OfType(Of TextBox)().LastOrDefault(Function(tb) tb.TextLength > 0)

        If lastFullTextBox Is Nothing Then
            MessageBox.Show("All fields are empty.")
        Else
            lastFullTextBox.Clear()
        End If
    End Sub

End Class
 
Back
Top