通过事件方法将值传递给控件. [英] Passing a value to a control from an event method.

查看:66
本文介绍了通过事件方法将值传递给控件.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

希望外面有人可以帮助我解决这个问题.
背景:我正在用C#编写Windows窗体程序,以从外部信号数字化仪收集和处理数据.数字化仪通过TCP发送数据,在这里我将数据收集到循环缓冲区中(感谢Karam Chandrabose提供了缓冲区类).
我在队列类中创建了一个事件,以在将脉冲结构添加到队列时触发该事件,因此我可以将其删除并以主要形式显示在四个带状图上.

我的问题是我不知道如何从事件方法到图表获取数据.我在队列类中使用了与主窗体相同的名称空间.为什么会出现在当前上下文中不存在"错误?

我对C#(和委托/事件)还很陌生,所以我对错误消息的搜索令我感到困惑……尤其是MSDN.

任何帮助将不胜感激.提前谢谢.

这是我遇到麻烦的部分代码:

在Form1.cs中:
从TCP端口接收数据的位置开始:

Hopefully someone out there can help me with this problem.
Background: I am writing a Windows Forms program in C# to collect and process data from an external signal digitizer. The digitizer sends the data across TCP, where I collect them in a circular buffer (thanks to Karam Chandrabose for the buffer class).
I created an event in the queue class to fire when a pulse structure is added to the queue so I can remove it and display it on four strip charts in the main form.

My problem is that I don''t know how to get the data from the event method to the charts. I used the same namespace in the queue class as I have for the main form. Why do I get a "does not exist in the current context" error?

I am still fairly new to C# (and delegates/events), so my searches for the error message is confusing to me...especially MSDN.

Any help will be appreciated. Thanks in advance.

Here is the portion of code I am having trouble with:

In Form1.cs:
From the point where the data is received at the TCP port:

namespace RealTimeClient
{
        ...

        /// <summary>
        /// Message received from PdwClient
        /// </summary>
        /// <param name="socket">the socket</param>
        /// <param name="bytes">the message</param>
        void m_PdwClient_MessageReceived(Socket socket, byte[] bytes)
        {
            Serializer ser = new Serializer();
            currentPdw = ser.ReadStruct<PdwWord>(socket, bytes);
            data = new PdwData();
            ProcessPdw(data, currentPdw);
        }

        /// <summary>
        /// ProcessPdw: extract data from message into PdwData structs
        /// </summary>
        /// <param name="data">the output struct</param>
        /// <param name="pdw">the struct from the digitizer</param>
        public void ProcessPdw(PdwData data, PdwWord pdw)
        {
            ...

            //
            // The data is extracted from the message here...
            //
            // add the PdwData struct to the queue
            CircularBuffer.Enqueue(data);
            // generate the event to remove it from the queue and
            // add it to the chart
            QueueCB.QueueAddedEventArgs e =
                new QueueCB.QueueAddedEventArgs(CircularBuffer);
            PdwAddedNotify(this, e);
        }

        public event QueueCB.QueueAddedEventHandler PdwAddedNotify;

        /// <summary>
        /// OnPdwAddedNotify
        /// </summary>
        protected virtual void OnPdwAddedNotify(QueueCB.QueueAddedEventArgs e)
        {
            if (PdwAddedNotify != null)
                PdwAddedNotify(this, e);
        }

        ...
}



在CircularBuffer.cs中



In CircularBuffer.cs

namespace RealTimeClient
{
    public class QueueCB : IEnumerable
    {
        public class QueueAddedEventArgs : EventArgs
        {
            /// <summary>
            /// Constructor
            /// </summary>
            /// <param name="pdw"></param>
            public QueueAddedEventArgs(QueueCB cb)
            {
                Form1.PdwData pdw = (Form1.PdwData)cb.Dequeue();
                // Append new values  //
//
// This is where I get the error message "Pesgo1 does not exist in the current context"
// Pesgo1 is the strip chart in Form1.cs
//
                Api.PEvsetW(Pesgo1.PeSpecial.HObject,
                            DllProperties.AppendYData,
                            pdw.Amplitude,
                            1);
                Api.PEvsetW(Pesgo1.PeSpecial.HObject,
                            DllProperties.AppendXData,
                            pdw.TimeOfArrival,
                            1);
                // Update image and force paint //
                Pesgo1.PeFunction.ReinitializeResetImage();
                Pesgo1.Refresh();
            }
        }

        public delegate void QueueAddedEventHandler(object sender, QueueAddedEventArgs e);

        ...
    }
}



诚然,我在事件以及如何与事件交互方面非常虚弱.我什至不确定我是否将代码放在正确的位置.
想法是生成一个事件,以触发每次添加队列时从队列中删除PdwData结构的操作(如果可能的话).
我认为我将需要队列,因为数据可能以每秒> 500000的速率进入.为此,许多消息将包含多个数据点.队列可能在安静的时间"赶上.我不能错过任何脉冲数据.

如果您需要更多信息,请让我知道.

谢谢
Dave



Admittedly, I am very weak on events and how to interact with them. I''m not even sure if I put the code in the right places.
The idea was to generate an event to trigger the removal of a PdwData struct from the queue every time one was added, if possible.
I think I will need the queue because the data could possibly come in at a rate of >500000 per second. To do this, many of the messages will have multiple data points included. The queue could catch up during ''quiet times''. I cannot miss any pulse data.

If you need more info, please let me know.

Thanks
Dave

推荐答案

有很多可能的原因,这里仅举几例:
1)您的队列类位于其他项目中,并且尚未在表单中引用该项目.添加参考.
2)您尚未包括using语句来访问队列所在的名称空间.
3)队列是私有的,受保护的或内部的,或者方法是私有的,受保护的或内部的,并且您试图从外部访问它.
4)您正在尝试静态访问队列类的实例成员.
5)你不能拼写! :laugh:

如果您的问题不在此列表中,我们将需要查看导致该问题的代码片段,并指出将哪一行报告为错误.
There are a whole load of possible reasons here are just a few:
1) You have your queue class in a different project, and you have not referenced the project in the form. Add a reference.
2) You have not included a using statement to access the namespace the queue is in.
3) The queue is private, protected, or internal, or a method is private, protected or internal and you are trying to access it from outside.
4) You are trying to access an instance member of the queue class statically.
5) You can''t spell! :laugh:

If your problem isn''t in this list, we will need to see the code fragment causing the problem, and an indication of which line is reported as an error.


这篇关于通过事件方法将值传递给控件.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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