Equivalent to refer to control by variable name?

前端 未结 4 1005
北荒
北荒 2021-01-22 16:44

In VB I can loop through controls, or refer to a control by concatenating a variable to a string. Something like:

Dim I as integer
I = 1
Me[\"Textbox\" & I]         


        
相关标签:
4条回答
  • 2021-01-22 16:52
    int i = 1;
    this.Controls["TextBox" & i].Text = "Some text";
    

    The above code is assuming that it is in a Control/Form.

    0 讨论(0)
  • 2021-01-22 16:57

    You can access the control by the control's name:

    Me.Controls("TextBox" & I).Text = "Some text"
    

    And the same in C#:

    this.Controls["TextBox" + I].Text = "Some text";
    
    0 讨论(0)
  • 2021-01-22 16:57

    Close to SysDragan' solution, but Me just needs to be replaced with this. And yes, you need to specify the Controls collection.

    this.Controls["TextBox" & I].Text = "Some text";
    
    0 讨论(0)
  • 2021-01-22 17:17
     int I = 1;
     this["Textbox" + I].Text = "some text";
    

    OR

     int I = 1;
     this.Page["Textbox" + I].Text = "some text";
    

    OR

     int I = 1;
     this.Controls["Textbox" + I].Text = "some text";
    
    0 讨论(0)
提交回复
热议问题