user7776356
user7776356

Reputation: 89

Dynamically assign text to text box in the loop

I want to assign Text to the textBox in the look, I tried

       Dim textBoxHB As TextBox = FindName("txt_HB_" + iRecCnt.ToString())

                    Me.Controls(String.Format("txt_HB_" + iRecCnt.ToString()).Text = .HouseBill

My Text box name change form txt_HB_1 ,txt_HB_2 and so on, and i want to where iRecCnt has 1,2.. values and Text is coming form .HouseBill

Is there any other way i can try?

Upvotes: 0

Views: 97

Answers (1)

Joel Coehoorn
Joel Coehoorn

Reputation: 416179

Replace the loop with this:

Dim boxes = Me.Controls.OfType(Of TextBox).Where(Function(b) b.Name.StartsWith("txt_HB_"))
For Each box As TextBox in boxes
    box.Text = .HouseBill
Next 

Upvotes: 1

Related Questions