开发者

How to set winform control properties at run time

开发者 https://www.devze.com 2023-03-11 18:43 出处:网络
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 stor

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?


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号