0

I have several texboxes that are created programatically. I need EACH textbox to have ONE event, meaning 1 textbox have 1 event that is different from the other event. I used the Name property to give each textbox a name hopefully that will identify each one separately.

for i = 0 to 5
   Dim TextBoxes As New TextBox
   With TextBoxes
     .Name = "InputTextBox" & i
     .AutoSize = True
     .Parent = FlowLayoutPanel1
   End With
Next

How can I use the Name property that I set in the For loop so I can put in a TextChanged event PER textbox. Is it possible in what I am planning to approach it? What is the right way to do it?

THANKS <3

Ajax
  • 15
  • 1
  • 5
  • Why don't you use one single TextChanged event and inside it look at the sender property (it is the textbox that called the event) to find out the name to use in a select case? – Steve Mar 21 '19 at 09:28
  • Thank you @Steve, for this idea. I can not get the Name of the textboxes. Additionally, I did this [link] https://stackoverflow.com/questions/14566156/get-the-id-name-of-a-button-in-a-click-event-vb-net. Thank you very much!!!!! – Ajax Mar 21 '19 at 09:44

2 Answers2

0

I can suggest a simpler approach. If you use a single event handler the building part of your textboxes are a lot easier. Then in the common TextChanged event you examine the sender object passed to the handler and use it to call a specific handler for that textbox

So we could have

for i = 0 to 5
   Dim TextBoxes As New TextBox
   With TextBoxes
     .Name = "InputTextBox" & i
     .AutoSize = True
     .Parent = FlowLayoutPanel1

   End With

   ' Add the common handler for all textboxes
   AddHandler TextBoxes.TextChanged, AddressOf onChanged
Next

In the common onChanged event you write this code

Sub onChanged(sender As Object, e As EventArgs)
    Dim t As TextBox = CType(sender,TextBox)
    Select Case t.Name
        case "inputTextBox0"
           HandleInputTextBox0()
        case "inputTextBox1"
           HandleInputTextBox1()
        ..... and so on....
    End Select
End Sub

But we could also get rid of that Select Case if you prepare a Dictionary where each key is the name of your textboxes and each value is the Action to be performed for that box

Dim dict As Dictionary(Of String, Action) = New Dictionary(Of String, Action) From
{
    {"inputTextBox0", AddressOf HandleInputTextBox0},
    {"inputTextBox1", AddressOf HandleInputTextBox1}
}

and change the common textchanged handler to a simple two lines code

Sub onChanged(sender As Object, e As EventArgs)
    Dim t As TextBox = CType(sender,TextBox)
    dict(t.Name).Invoke()
End Sub
Steve
  • 213,761
  • 22
  • 232
  • 286
0

Thanks to @Steve. This is how I solved my problem

for i = 0 to 5
  Dim TextBoxes As New TextBox
  With TextBoxes
    .Name = "InputTextBox" & i
    .AutoSize = True
    .Parent = FlowLayoutPanel1
    AddHandler .TextChanged, AddressOf InputPercentage
  End With
Next

Friend Sub InputPercentage(sender as Object, e as EventArgs)
  Dim txt As TextBox = CType(sender, TextBox)
  MessageBox.Show(txt.Name)
End Sub

I was able to get the name of the control. Thanks!

Ajax
  • 15
  • 1
  • 5