VB 2010 - A variable with a value of a label name

前端 未结 5 1444
北荒
北荒 2021-01-22 13:18

I\'m using VB 2010 Express and have a label named \"lblTitle\" in my form. The next code doesn\'t work and I know it , but how can I do something like this?

Dim          


        
相关标签:
5条回答
  • 2021-01-22 13:43

    Seems like the best way to do this is to make a string representing the name of the control, then cast it as a control. This saves you having to make a variable, as well. Your method might appear to work, but I'm not sure it wouldn't fail once you run it. Also, it would throw an error in the IDE if you were trying to access a property more specific to the type of control, and not something common like Text. See my closely related question:

    accessing multiple form controls using a variable for the name

    0 讨论(0)
  • 2021-01-22 13:46

    After a deep google search , the answer seems to be easy as that:

       Dim bla = "lblName"
       Me.Controls(bla).Text = "Dan"
    

    while lblName is the label's name in the form. Thanks guys

    0 讨论(0)
  • 2021-01-22 13:51
    DirectCast(Page.FindControl("lblTitle"), Label).Text = "some new value"
    
    0 讨论(0)
  • 2021-01-22 13:58

    Try this:

    Dim myLabel As Label = DirectCast(Page.FindControl("lblTitle"), Label)
    myLabel.Text = "some new value"
    
    0 讨论(0)
  • 2021-01-22 14:07

    You can do this assuming you are using windows forms:

    DirectCast(Me.Controls.Find("lblTitle", True)(0), Label).Text = "Hello world"
    
    0 讨论(0)
提交回复
热议问题