I've named textboxes in my UI the same as the field-names in the 'Stuff' class below, I'm iterating over the controls and trying to populate them programmatically with the respective fields from Stuff-
I did search, extensively. But the line below (gettype.getproperty.getvalue[stuff,null]) has never returned a value, at all. It just keeps crashing out as null- I found this line here on Stack
foreach (Control c in gbInfo.Controls) {
if (c.Name.StartsWith("tb")) {
string fieldIWantToAccess = c.Name.Substring(2); //removes the tb from tbWeAllAre
// it's this line below that's giving me issues- I'm just looking for a simple
// way to accomplish this. Back in the perl days it was just:
// $name = "IsNerd"; c.Text = $$name;
c.Text = stuffObj.GetType().GetProperty(fieldIWantToAccess).GetValue(Stuff, null);
}
}
public class Stuff {
public string Name;
public string Age;
public string IsNerd;
public string WeAllAre;
}
In my actual code, 'Stuff' has about 2 dozen public strings, and there's a large array of TextBox controls on the UI that I'd like to populate this way.. Rather than doing them all individually.
Aucun commentaire:
Enregistrer un commentaire