[英]c# How can I update a label within a form from another class that has a void method?
Right now, I have a form with a label, lblHeartBeat. 现在,我有一个带有标签的表格,lblHeartBeat。 I have a UDP listener that takes in data with the value for lblHeartBeat. 我有一个UDP侦听器,该侦听器使用lblHeartBeat的值接收数据。 I'm wondering, how can I change the label in realtime with my UDP client from another class? 我想知道,如何通过另一个类的UDP客户端实时更改标签?
Right now I have 现在我有
private void btnBopIt_Click(object sender, EventArgs e)
{
string name;
name=txtName.Text;
UDPListener myListener=new UDPListener();
myListener.GetData(name);
}
and the section of my class with the UDP client is: 和我的类与UDP客户端的部分是:
public class UDPListener
{
public void GetData(string Name)
...
while (!done)
{
byte[] bytes = listener.Receive(ref groupEP);
var result = Encoding.ASCII.GetString(bytes, 0, bytes.Length);
var data = result.Split(',')
.Select(x => x.Trim().Split('='))
.ToDictionary(x => x[0], x => x[1]);
string myFunString = result.ToString();
if (myFunString.Contains(Name) == true && myFunString.Contains("Heartbeat"))
{
decimal value=data["Hearbeat"]
//I'd like to have it some way below that:
form1.lblHeartbeat=value;
}
}
Mind you that my UDPlistener is a void method. 请注意,我的UDPlistener是无效方法。 I figure having it decimal will only return one value to the form, and not autoupdate. 我认为将其保留为十进制将仅向表单返回一个值,而不会自动更新。 Should I try to create an event within the if statement so that it changes the label? 我应该尝试在if语句中创建一个事件,以便它更改标签吗? Cheers 干杯
Update: I should also note that my UDP Listener takes on the string from txtName.Text. 更新:我还应该注意,我的UDP侦听器采用了txtName.Text中的字符串。 I may have to run this asynchronously. 我可能必须异步运行它。 Update2: Finally got it to work. Update2:终于让它工作了。 In addition to Rob's answer, I had to invoke it as I made my UDPlistiner operate on a different thread 除了Rob的回答,我还必须调用它,因为我使UDPlistiner在另一个线程上运行
Here: 这里:
UDPListener myListener=new UDPListener();
You should either pass the form, or the particular label you're interested in. Then your UDPListener
would be able to reference the form and its controls. 您既可以传递表单,也可以传递您感兴趣的特定标签。然后UDPListener
便可以引用表单及其控件。 For example: 例如:
private void btnBopIt_Click(object sender, EventArgs e)
{
UDPListener myListener = new UDPListener(this);
}
public class UDPListener
{
private Form1 _form { get; set; }
public UDPListener (Form1 form) //Assuming your form class is `Form1`
{
_form = form;
}
//..
}
Then you can write this: 然后,您可以编写以下代码:
_form.lblHeartbeat.Text = value;
Assuming you declare your label as public 假设您宣布标签为公开
I would suggest slightly different approach. 我建议稍微不同的方法。
Define an EventHandler
in UDPListener
and subscribe to that event in your main form. 在UDPListener
定义一个EventHandler
并以您的主要形式订阅该事件。
public class UDPListener
{
public event Action<string> MessageReceived;
...
}
In your ( while
)loop modify your code to notify to listeners. 在( while
)循环中,修改代码以通知侦听器。
if (myFunString.Contains(Name) == true && myFunString.Contains("Heartbeat"))
{
decimal value=data["Hearbeat"]
//I'd like to have it some way below that:
if(MessageReceived != null) MessageReceived(value);
}
Final step, subscribing to this event in Form1
最后一步,在Form1
订阅此事件
private void btnBopIt_Click(object sender, EventArgs e)
{
UDPListener myListener=new UDPListener();
myListener.MessageReceived += (s) => lblHeartbeat.Text = s;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.