如何使用字符串变量 -c# 获取标签名称 [英] How to get label name by using string variable -c#

查看:31
本文介绍了如何使用字符串变量 -c# 获取标签名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用字符串变量来获取标签名称以更改其文本.例如,我有以下代码:

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()));

如何使用字符串变量 labelName 来更改 lbl_text 的值?

How do I use the string variable labelName to change the lbl_text's value?

推荐答案

您必须从给定名称的表单控件列表中找到 Label 控件.

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())); 
}

这篇关于如何使用字符串变量 -c# 获取标签名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆