c# - 在表单之间传递数据(事件未触发)

标签 c# winforms

我正在编写一个应用程序,它以恒定的时间间隔(使用计时器)将 GPS 数据从主窗体传递到 GPS 窗体。

我使用以下教程进行了快速测试:

http://www.codeproject.com/Articles/17371/Passing-Data-between-Windows-Forms

但是,当我启动代码时,没有触发任何事件。首先我得到了一个空指针。添加以下行后我将其删除:

if (GpsUpdated != null)
{
    GpsUpdated(this, args);
}

主表单代码:

public partial class Form1 : Form
{
    // add a delegate
    public delegate void GpsUpdateHandler(object sender, GpsUpdateEventArgs e);

    // add an event of the delegate type
    public event GpsUpdateHandler GpsUpdated;

    int lat = 1;

    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        Form_GPS form_gps = new Form_GPS();
        form_gps.Show();
        timer1.Enabled = true;
    }

    private void timer1_Tick(object sender, EventArgs e)
    {
        Debug.WriteLine("Timer Tick");
        // instance the event args and pass it each value
        GpsUpdateEventArgs args = new GpsUpdateEventArgs(lat);

        // raise the event with the updated arguments
        if (GpsUpdated != null)
        {
            GpsUpdated(this, args);
        }
    }
}

public class GpsUpdateEventArgs : EventArgs
{
    private int lat;

    // class constructor
    public GpsUpdateEventArgs(int _lat)
    {
        this.lat = _lat;
    }

    // Properties - Viewable by each listener
    public int Lat
    {
        get
        {
            return lat;
        }
    }
}

GPS 表单代码:

public partial class Form_GPS : Form
{
    public Form_GPS()
    {
        InitializeComponent();
    }

    private void Form_GPS_Load(object sender, EventArgs e)
    {
        Debug.WriteLine("GPS Form loaded");
        Form1 f = new Form1();

        // Add an event handler to update this form
        // when the ID form is updated (when
        // GPSUpdated fires).

        f.GpsUpdated += new Form1.GpsUpdateHandler(gps_updated);
    }

    // handles the event from Form1
    private void gps_updated(object sender,GpsUpdateEventArgs e)
    {
        Debug.WriteLine("Event fired");
        Debug.WriteLine(e.Lat.ToString());
    }
}

有人能指出我正确的方向吗?我做错了什么?

提前致谢并致以最诚挚的问候。

最佳答案

您应该将 Form1 的实例传递给 Form_GPS 才能正常工作。请参阅以下更改:

public partial class Form_GPS : Form
{
    public Form_GPS(Form1 owner)
    {
        InitializeComponent();
        owner.GpsUpdated += new Form1.GpsUpdateHandler(gps_updated);
    }

    private void Form_GPS_Load(object sender, EventArgs e)
    {
        Debug.WriteLine("GPS Form loaded");
    }

    // handles the event from Form1
    private void gps_updated(object sender,GpsUpdateEventArgs e)
    {
        Debug.WriteLine("Event fired");
        Debug.WriteLine(e.Lat.ToString());
    }
}

现在您还需要对 Form1 进行一些小更改:

private void Form1_Load(object sender, EventArgs e)
{
    Form_GPS form_gps = new Form_GPS(this);
    form_gps.Show();
    timer1.Enabled = true;
}

注意如何使用自引用 thisForm_GPS 的构造函数中将 Form1 的实例传递给 Form_GPS >.

关于c# - 在表单之间传递数据(事件未触发),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25401549/

相关文章:

c# - 如何使用 EF Core 聚合数百万行

c# - 在 WPF MaterialDesign 中悬停自定义控件

c# - 将多行数据导出到 csv 中的同一单元格

.net - 是否可以防止 DataGridView 中出现多行 HeaderText?

c# - FlowLayoutPanel中的点击事件

c# - 传递 orderBy 或 OrderByDescending 作为参数

c# - 无法访问自定义类堆栈实现中的类

c# - Entity Framework 代码首先多对多关系不起作用

c# - 将公式单元格应用于 DataGridview

c# - 如何从所有目录、其他目录中获取所有文件并检查每个文件大小?