【发布时间】:2014-02-01 05:27:09
【问题描述】:
谁能解释或说明为什么我的事件处理程序不更新我的 Windows 窗体文本框?我已将事件处理程序放在 UI 线程中以更新我的 GUI 窗口中的文本框。我的 UI 线程 #1 SetOperation 类中的 EventLaunch 方法会启动一个事件。 UI 线程 #1 SetOperation 类 OnChDetDisplay 事件处理程序完成,但窗口窗体文本框未更新为分配的值。我缺少什么将事件和处理程序绑定到更新文本框?
感谢任何人都可以分享的任何帮助,
下面是一些代码:
// Class runs in Thread #2: Prepares message data for Windows Form GUI display and passes to UI Thread #1
public class Aag_PrepDisplay
{
private Aag_PrepDisplay mAagPrep;
public Aag_PrepDisplay AagPrep
{
get { return mAagPrep; }
set { mAagPrep = value; }
}
// Thread #2: prepares message for Windows Form GUI display in UI Thread #1
public void PrepareDisplay(/*stuff*/)
{
mAagPrep = new Aag_PrepDisplay();
// does message prep stuff
SetOperation setOp1 = new SetOperation();
setOp1.FireEvent(mAagPrep); // call to UI Thread #1 method to fire event to update GUI; passes object with data
}
}
// UI Thread #1 class is the Windows Form. Displays and updates all textboxes.
public partial class SetOperation : Form
{
public event Action<object> OnChDet; // declared delegate object event that passes an object
public SetOperation()
{
InitializeComponent();
OnChDet += chDetDisplayHandler; // hooks handler to event
}
// Thread #1: accepts object w/data from Thread #2; Fires an event to update GUI Textbox(s)
private void FireEvent(Aag_PrepDisplay aagPrep)
{
OnChDet(aagPrep);
}
// UI Thread #1 event handler.
public void chDetDisplayHandler(object name)
{
// **** Problem: event is triggered and value assigned, but doesn't update the GUI window Textbox ********
actFreqChan1.Text = "402.5"; // this is only a test to see if event handler will update Textbox
// Next step: updateAll(name); // pass the object from Aag_PrepDisplay class
}
//Thread #1: update GUI Textbox values
public void updateAll(object name)
{
// this is where data from the Thread #2 AagPrep object will assign and update Textbox values
}
}
【问题讨论】:
-
非常难读的代码。 “ChDet”、“Chan”和“Aag”可能是什么意思? AagDisEvt1? 2在哪里?这种困惑隐藏了 this 问题,即在某处创建但实际上不可见的表单对象。因为它的 Show() 方法从未被调用过,或者是在一个不抽水的工作线程上创建的。
-
对不起。试图显示与目标有关的代码。名称是设备的简写。试图了解为什么事件处理程序在完成后不更新 ActFreqChan1.Text。请参阅下面的 cmets 到 pid。确实会出现 GUI 表单,但事件处理程序完成时文本框未更新。你能解释一下工作线程上的 Show() 吗?我有第二个线程(未显示),它定期将数据传递给第一个线程中的 EventLaunch(~) 方法。尚未用数据实现。目前,我只想在事件完成时更新文本框。我希望这更有意义。感谢您的任何建议。
标签: c# events user-interface