Dynamic Variable Name Use in C# for WinForms

前端 未结 7 1568
余生分开走
余生分开走 2021-01-13 01:28

Not sure what is the best way to word this, but I am wondering if a dynamic variable name access can be done in C# (3.5).

Here is the code I am currently looking to

相关标签:
7条回答
  • 2021-01-13 02:03

    Yes, you can use

      Control c = this.Controls.Find("chkCategory" + i.ToString(), true).Single();
      (c as textBox).Text = ...;
    

    Add some errorchecking and wrap it in a nice (extension) method.


    Edit: It returns Control[] so either a [0] or a .Single() are needed at the end. Added.

    0 讨论(0)
提交回复
热议问题