Is it appropriate to handle controls based on your name?

0

I have a set of TextBox that are called for example: TB_Nombre, TB_Calle, TB_Telefono and TB_Email, which will be fed from a BD and which fields match the name, so make the following code:

        List<string> d = new List<string> { "Nombre", "Calle", "Telefono", "Email"};
        DataRow registro = datos.Tables[0].Rows[0];
        foreach (string i in d){
                    TextBox tb = this.Controls.Find("TB_" + i, true).FirstOrDefault() as TextBox;
                    tb.Text = registro[i].ToString();                    
        }

certainly does the work, but is it appropriate to work the controls from Controls.Find ?

    
asked by Jorge Arturo Juarez 04.07.2017 в 03:36
source

1 answer

0

If it works properly, it's appropriate, although I would probably use a dictionary:

var controlesDict= new Dictionary<string,TextBox>();
controlesDict.Add("Nombre", this.TB_Nombre);
controlesDict.Add("Calle", this.TB_Calle);
...

That way, access is very simple and immediate:

TextBox tb = controlesDict["Calle"];
controlesDict["Nombre"].Text="Pepe";
    
answered by 04.07.2017 в 09:34