Dynamic Variable Name Use in C# for WinForms

前端 未结 7 1567
余生分开走
余生分开走 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 01:39

    You don't need dynamic for that. Put chkCategory1 - 12 in an array, and loop through it with a for loop. I would suggest you keep it around in a field and initialize it at form construction time, because chkCategory seems to be related. But if you want a simple example of how to do it in that simple method, then it would be something like this:

    private void frmFilter_Load(object sender, EventArgs e)
    {
        var chkCategories = new [] { chkCategory1, chkCategory2, chkCategory3, .......... };
        for(int i = 0 ; i < chkCategories.Length ; i++ ) 
            chkCategoies[i].Text = categories[i];
    }
    

    You know more about the application, so you could perhaps avoid writing out all the control names - for instance, if they are placed on a common parent control, then you could find them by going through it's children.

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

    Sometimes it can help to put your controls into an array or collection as such:

    Checkbox[] chkCataegories = new Checkbox[] { chkCategory1, chkCategory2 ... };
    for(int i = 0; i < chkCategories.Length; i++)
        chkCategories[i].Text = categories[i];
    

    As another approach, you can dynamically create your checkboxes at runtime instead of design time:

    for(int i = 0; i < categories.Length; i++)
    {
        Checkbox chkCategory = new chkCategory { Text = categories[i] };
        someContainer.Controls.Add(chkCategory);
    }
    

    At least with dynamically created controls, you don't need to modify your GUI or your form code whenever you add new categories.

    0 讨论(0)
  • 2021-01-13 02:00
    for(...)
    {
         CheckBox c = this.Controls["chkCategory" + i.ToString()] as CheckBox ;
    
         c.Text = categories[i];  
    }
    
    0 讨论(0)
  • 2021-01-13 02:01

    The "this.Controls["chkCategory" + i.ToString()]" and "this.Controls.Find("chkCategory" + i.ToString(), true)" both do not work... the former informs you that the contents of the [] are not an int and the latter that ControlCollection does not contain a definition for Find.

    Use "Control myControl1 = FindControl("TextBox2");" instead.

    I needed this form as I was looping through another array, extracting values and using them to populate form fields. Much easier to look for label1, label2, label3, etc.

    0 讨论(0)
  • 2021-01-13 02:02

    You can do that with reflection. But don't.

    It's more proper to instantiate a list of contols, add them programmatically to your form, and index that.

    0 讨论(0)
  • 2021-01-13 02:02

    No, but you could do something like this (untested, beware of syntax errors):

    private readonly CheckBox[] allMyCheckboxes = new CheckBox[] { chkCategory1, chkCategory2, ... }
    

    Then you just need to do

    for (i = 0; i < 12; i++) allMyCheckboxes[i].Text = categories[i];
    
    0 讨论(0)
提交回复
热议问题