Reputation: 199
I want to use a string variable to get a label name to change its text. for example, I have the following code:
string labelName = "lbl_text";
lbl_Heart_Rate.Invoke((MethodInvoker)(() => lbl_Heart_Rate.Text = displayValue.ToString()));
How do I use the string variable labelName to change the lbl_text's value?
Upvotes: 2
Views: 8610
Reputation: 3143
I think the form has a function for this: Control.ControlCollection.Find
Full Code:
string labelName = "lbl_text";
TextBox lbl_text = this.Controls.Find(labelName , true).FirstOrDefault() as TextBox;
//You can access 'lbl_text' here...
Upvotes: 5
Reputation: 16956
You have to find Label
Control from the list of form control for a given name.
var control = this.Controls.OfType<Label>()
.FirstOrDefault(c=>c.Name == labelName");
if(control != null)
{
// Now you can play with your logic.
control.Invoke((MethodInvoker)(() => control.Text = displayValue.ToString()));
}
Upvotes: 1