1

Ok, I try to simplify my problem. I have a blank winform. A textbox control with name textbox1 being added to the winform. During the form load event, I retrieve 2 string values from database and store these values into TextboxName and TextboxValue variables of string type.

I use TextboxName variable to store that textbox control name (i.e. currently TextboxName = "textbox1") and TextboxValue variable to store that textbox's text (i.e. currently TextboxValue = "Hello world"). My question is how to set the textbox1.text property without have to resort to following code:

If TextboxName = "textbox1" Then
        textbox1.Text = TextboxValue
End If

I mean how to set the textbox1.Text property without have to use IF...Then construct?

user774411
  • 1,749
  • 6
  • 28
  • 47

1 Answers1

1

Use FindControl(TextboxName) to get the control, cast it to a TextBox, then set the Text property on that variable. :)

See here for how you can implement FindControl - also a very similar example! : Find control by name from Windows Forms controls

Community
  • 1
  • 1
Kieren Johnstone
  • 41,277
  • 16
  • 94
  • 144
  • +1 @Kieren Johnstone: I know what you mean, even though there is no built-in FindControl. Thanks for helping me! – user774411 Jun 07 '11 at 10:06
  • Huh. Someone gave a random downvote. If this doesn't work for some reason, please say the problem, so we can fix it, rather than leave a bad solution here. – Kieren Johnstone Jun 28 '11 at 14:48
  • Your answer (the link) is helpful, maybe someone just directly use `FindControl(TextboxName)` and not work as expected. – user774411 Jun 30 '11 at 02:13
  • You mean someone who doesn't read the answer might not understand the answer? ... – Kieren Johnstone Jun 30 '11 at 08:31
  • I don't know but the point is I have upvoted your answer long time ago. So the downvote not from me. I appreciate your answer. – user774411 Jun 30 '11 at 08:34