Hey people!
I'm creating a small project which consists of 36 textboxes and one button.

When I click a textbox, it should open a open file dialog and, when OK, set the text of the textbox I've clicked to the file name.

I've did this:

Public Class Form1

    Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.Click, TextBox2.Click, TextBox3.Click, TextBox4.Click, TextBox5.Click, TextBox6.Click, TextBox7.Click, TextBox8.Click, TextBox9.Click, TextBox10.Click, TextBox11.Click, TextBox12.Click, TextBox13.Click, TextBox14.Click, TextBox15.Click, TextBox16.Click, TextBox17.Click, TextBox18.Click, TextBox19.Click, TextBox20.Click, TextBox21.Click, TextBox22.Click, TextBox23.Click, TextBox24.Click, TextBox25.Click, TextBox26.Click, TextBox27.Click, TextBox28.Click, TextBox29.Click, TextBox30.Click, TextBox31.Click, TextBox32.Click, TextBox33.Click, TextBox34.Click, TextBox35.Click, TextBox36.Click

        Dim abrir As New OpenFileDialog

        With abrir
            .Filter = "M2TS|*.m2ts"
        End With

        If abrir.ShowDialog = DialogResult.OK Then
            Dim CheckBoxNumber As Integer
            Dim NewCheckBoxTextValidator As Integer

            For i As Integer = 1 To 36
                CheckBoxNumber = i

                NewCheckBoxTextValidator = Me.Controls.IndexOfKey("TextBox" & CheckBoxNumber.ToString)
                DirectCast(Me.Controls.Item(NewCheckBoxTextValidator), TextBox).Text = abrir.FileName

            Next
        End If
    End Sub
End Class

But, it don't work. It don't replace only the clicked textbox, but all textboxes. How can I fix it?

Thanks!

See if this helps.

Public Class Form1
    Private myOFD As New OpenFileDialog

    Private Sub txt_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.Click, TextBox2.Click, TextBox3.Click
        If myOFD.ShowDialog = DialogResult.OK Then
            CType(sender, TextBox).Text = myOFD.FileName
        End If
    End Sub
End Class

Edited 5 Years Ago by codeorder: n/a

With this code sample, you can add all of your TextBoxes to the txt_Click Event in one shot.

Public Class Form1

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

    Private Sub addTextBoxClickHandlers()
        Dim txt As TextBox
        For i As Integer = 1 To 36
            txt = DirectCast(Me.Controls.Item(Me.Controls.IndexOfKey("TextBox" & i.ToString)), TextBox)
            AddHandler txt.Click, AddressOf txt_Click '// Assign each TextBox to the txt_Click Event.
        Next
    End Sub

    Private Sub txt_Click(ByVal sender As Object, ByVal e As System.EventArgs) 'Handles TextBox1.Click
        MsgBox(CType(sender, TextBox).Name)
    End Sub
End Class

codeorder, always saving my day!

Can you explain me just a little thing?
What AdressOf argument actually do?

For i As Integer = 1 To 36
            txt = DirectCast(Me.Controls.Item(Me.Controls.IndexOfKey("TextBox" & i.ToString)), TextBox)
            AddHandler txt.Click, AddressOf txt_Click '// Assign each TextBox to the txt_Click Event.
        Next

It worked perfect. Now remains just the logic =)

And, one more thing... It will have a button that will start a process for each textbox. But, if the textbox is empty, it will not start.

I can do If/Else for it, but for 36 textboxes it becomes a bit tiring... there is another way to do it?

Thanks!!

See if this helps.

Public Class Form1
    Private myOFD As New OpenFileDialog
    Private selectedTextBox As New TextBox '// Declare new TextBox.

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

    Private Sub addTextBoxHandlers()
        Dim txt As TextBox
        For i As Integer = 1 To 36
            txt = DirectCast(Me.Controls.Item(Me.Controls.IndexOfKey("TextBox" & i.ToString)), TextBox)
            AddHandler txt.Click, AddressOf txt_Click
            AddHandler txt.GotFocus, AddressOf txt_GotFocus '// Assign each TextBox to the txt_GotFocus Event.
        Next
    End Sub

    Private Sub txt_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        If myOFD.ShowDialog = DialogResult.OK Then
            CType(sender, TextBox).Text = myOFD.FileName
        End If
    End Sub

    Private Sub txt_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) ' Handles TextBox1.GotFocus
        Me.selectedTextBox = CType(sender, TextBox) '// Set sender as the selected TextBox.
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Me.Text = Me.selectedTextBox.Text '// display text of selected TextBox.
    End Sub
End Class

------------------
As for the AddressOf...
When you do this:

Private Sub TextBox1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.Click, TextBox2.Click, TextBox3.Click, TextBox4.Click, TextBox5.Click, TextBox6.Click, TextBox7.Click, TextBox8.Click, TextBox9.Click, TextBox10.Click, TextBox11.Click, TextBox12.Click, TextBox13.Click, TextBox14.Click, TextBox15.Click, TextBox16.Click, TextBox17.Click, TextBox18.Click, TextBox19.Click, TextBox20.Click, TextBox21.Click, TextBox22.Click, TextBox23.Click, TextBox24.Click, TextBox25.Click, TextBox26.Click, TextBox27.Click, TextBox28.Click, TextBox29.Click, TextBox30.Click, TextBox31.Click, TextBox32.Click, TextBox33.Click, TextBox34.Click, TextBox35.Click, TextBox36.Click

..You are giving each TextBox the AddressOf the .Click Event.

Basically, I took this part:

Private Sub TextBox1_Click(ByVal sender As Object, ByVal e As System.EventArgs)

Renamed it from: TextBox1_Click to: txt_Click and dynamically assigned that Event for each TextBox to be Handled by, as provided in my previous post.

Edited 5 Years Ago by codeorder: n/a

Thanks for the codes, but I found that what I wanted to do is not possible, basically external issues.

Anyway, this will be helpful for the future. :)

This question has already been answered. Start a new discussion instead.