【问题标题】:Pass data between forms (event not triggered)在表单之间传递数据(未触发事件)
【发布时间】:2014-08-20 17:55:59
【问题描述】:

我正在编写一个应用程序,它以恒定的间隔(使用计时器)将 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 反过来加载一个 Form1
  • 好的,我明白了。但是如何从触发事件的地方访问 Form1?
  • 最好的办法是在其他地方设置 GPS 计时器,可能是 Singleton 类或将其传递到 Forms 的构造函数中,然后您可以连接到那里的事件处理程序。

标签: c# winforms


【解决方案1】:

您应该将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

【讨论】:

    【解决方案2】:

    如下声明事件解决了问题:

    public static event GpsUpdateHandler GpsUpdated;
    

    代替:

    public event GpsUpdateHandler GpsUpdated;
    

    这样Form1事件可以被称为静态的,所以不需要新的实例。

    【讨论】:

      猜你喜欢
      • 2014-02-23
      • 2017-02-03
      • 1970-01-01
      • 2019-03-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-10
      • 2012-06-18
      相关资源
      最近更新 更多