事件null异常 [英] Event null exception

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

问题描述

我正在尝试将事件从一个控件触发到另一个控件,但是我一直在对该事件获取null值异常.我已经声明了一个委托和事件,并创建了一个方法来检查事件是否在第二个控件上不为null.该方法在button_click上调用 并且应该通知第一个控件执行一些操作(刷新网格数据)

I am trying to fire event from one control to another, but I keep getting null value exception for that event. I have declared a delegate and event, and created method that checks if event is not null on 2nd control. That method is called on button_click and it is supposed to notify 1st control to do some action (refreshing grid data)

//2nd control
public partial class InventuraForm : Controls.BaseUserControl
    { 
        public delegate void SomeDelegat();
        public event SomeDelegat  MyEvent;               

       
       public void SendEvent()
        {
            if (MyEvent != null) MyEvent(); //keeps showing MyEvent = null at runtime
        }

        //saving data and closing control
        public void tsbSpremiZatvori_Click(object sender, EventArgs e)
        {
            this.Cursor = Cursors.WaitCursor;
            if (SaveData())
            {
                this.Cursor = Cursors.Default;
                BusinessLogic.Inventura.pID = null;
                base.ContainerForm.DialogResult = DialogResult.OK;
                base.ContainerForm.Close();
            }
            this.Cursor = Cursors.Default;

            SendEvent();
        }
}


//1st control

public partial class InventuraFormPregled : UserControl, ISmartPartInfoProvider
    {                
  #region Methods
 public InventuraFormPregled()
 {
 InitializeComponent();  

 InventuraForm _inv = new InventuraForm();
 _inv.MyEvent += new InventuraForm.SomeDelegat(LoadGridData);            
          
 //some code
 }     

   private void LoadGridData()
   {
         //some code
   }
}
                

我不明白的是为什么 MyEvent 总是具有 null 值.

What I dont understand is why MyEvent keeps having null value.

感谢您的帮助.

推荐答案

InventuraFormPregled和InventuraForm都是某种UserControl?

InventuraFormPregled and InventuraForm are both some kind of UserControl?

您尚未显示实际创建这些控件的代码.您实际上是在InventuraFormPregled内部创建了一个InventuraForm实例,并将其放入"_inv"目录中.变量,但您确定是 特定实例实际上是在您单击按钮时使用的,而不是控件的其他一些实例(即不是直接放置在窗口上的那个实例)?

You haven't shown the code that actually creates these controls. You actually create an instance of InventuraForm inside InventuraFormPregled and place that into an "_inv" variable, but are you sure it is that particular instance that is actually getting used when you click a button and not some other instance of the control (i.e. not one that has been placed directly onto a window)?


这篇关于事件null异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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