用户类中未收到事件。 [英] Event is not received in user class.

查看:42
本文介绍了用户类中未收到事件。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨!

有人能告诉我我的代码有什么问题吗?通过评论/取消注释适当的代码行来尝试静态和非静态事件。

项目名称是试一试,这是主要形式:

< pre lang =c#> 使用系统;
使用 System.Collections.Generic;
使用 System.ComponentModel;
使用 System.Data;
使用 System.Drawing;
使用 System.Linq;
使用 System.Text;
使用 System.Threading.Tasks;
使用 System.Windows.Forms;

命名空间 Try_It
{
public delegate void dlgt_Delegate(); // 这是一个委托(或应该是: - ))...
public partial class frm_Main:Form
{
public static event dlgt_Delegate evnt_Event; // ...这应该是静态事件。
// 公共事件dlgt_Delegate evnt_Event; // ...这应该是非静态事件。
public frm_Main()
{
InitializeComponent ();
}

private void btn_Send_Event_Click( object sender,EventArgs e)
{
if (evnt_Event!= null // 如果有人订阅...
evnt_Event(); // ...告诉他们。
}

< span class =code-keyword> private void btn_Exit_Click( object sender,EventArgs e )
{
this .Close();
}
}
}





这是我的课程,由Project->添加。 Add-> Class:

 使用系统; 
使用 System.Collections.Generic;
使用 System.Linq;
使用 System.Text;
使用 System.Threading.Tasks;
使用 System.Windows.Forms;

命名空间 Try_It
{
public partial class cls_I_am_waiting
{
frm_Main Main = new frm_Main();

public cls_I_am_waiting() // 这应该是类的构造函数,
{
frm_Main.evnt_Event + = frm_Main_evnt_Event; // 我想在主窗体上单击按钮时收到通知(静态事件)...
// Main.evnt_Event + = Main_evnt_Event; //我希望在主窗体上单击按钮时收到通知(非静态事件)...
}

void frm_Main_evnt_Event() // ... with static event。
{
MessageBox.Show( 好的我收到了它!); // ...并告诉它!
}

// void Main_evnt_Event()// ...带有非静态事件。
< span class =code-comment> // {
// MessageBox.Show(我收到了!); // ...并告诉它!
// }
}
}





谢谢。

解决方案

可能是因为你没有添加任何 cls_I_am_waiting 的实例,所以没有事件被添加到处理程序链中。



试试这个:

  public  frm_Main()
{
InitializeComponent ();
cls_I_am_waiting ciaw = new cls_I_am_waiting();
}

看看是否有所作为......


你好!

嗯,在搜索互联网之后我(也许)找到解决方案。

结论可能是这样的:主要形式是最顶级的父级,其他类是子级。并且,通常,孩子们向父母发送消息,通知他们发生了某些事情(异步),父母订阅了这些事件,并有一个方法来回应他们。

当然,根据这个结论,主窗体自己处理它内部发生的事件。

相反,当父想要向任何(子)类发送消息时,它通过调用方法来实现它那个(小孩)班直接没有活动!

这对我来说很有意义,如果我错了,请纠正我。

所以,我只是抛弃代表和事件在主窗体中执行下一个:在类cls_I_am_waiting.cs中我创建了一个方法void frm_Main_event_Event()一个公共方法,在主类中我创建了一个cls_I_am_waiting.cs的实例并调用它的方法public void frm_Main_event_Event()直接和 - 它有效!

抱歉,过了一会儿,我回来问自己:好的这对几个孩子(班级)有好处,但是如果我有30个,50个或更多的课程必须回应来自主表格的信息怎么办?

好​​吧,它在一开始就把问题重新开始了。

Hi!
Can someone tell me what is wrong in my code? Try it with static and non-static event by commenting/uncommenting appropriate lines of code.
The name of project is "Try It" and this is main form:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Try_It
{
	public delegate void dlgt_Delegate(); // This is a delegate (or supposed to be :-))...
	public partial class frm_Main : Form
	{
		public static event dlgt_Delegate evnt_Event; //... and this should be static event.
		//public event dlgt_Delegate evnt_Event; //... and this should be non-static event.
		public frm_Main()
		{
			InitializeComponent();
		}

		private void btn_Send_Event_Click(object sender, EventArgs e)
		{
			if (evnt_Event != null) // If someone is subscribed...
				evnt_Event(); //... tell them.
		}

		private void btn_Exit_Click(object sender, EventArgs e)
		{
			this.Close();
		}
	}
}



This is my class which is added by Project->Add->Class:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Try_It
{
	public partial class cls_I_am_waiting
	{
		frm_Main Main = new frm_Main();

		public cls_I_am_waiting() // This should be a constructor of a class,
		{
			frm_Main.evnt_Event += frm_Main_evnt_Event; // I want to be notified when button is clicked on main form (static event)...
			//Main.evnt_Event += Main_evnt_Event; // I want to be notified when button is clicked on main form (non-static event)...
		}

		void frm_Main_evnt_Event() //... with static event.
		{
			MessageBox.Show("O.K. I received it!"); //... and tell it!
		}

		//	void Main_evnt_Event() //... with non-static event.
		//{
		//	MessageBox.Show("O.K. I received it!"); //... and tell it!
		//}
	}
}		



Thanks.

解决方案

Probably, because you haven't added any instances of cls_I_am_waiting so there are no events being added to the handler chain.

Try this:

public frm_Main()
{
    InitializeComponent();
                cls_I_am_waiting ciaw = new cls_I_am_waiting();
}

And see if that makes a difference...


Hi there!
Well, after searching the Internet I (maybe) find the solution.
The conclusion could be something like this: main form is top-most parent and other classes are children. And, usually, children send messages to parent to notify it that something happened to them (asynchronously) and parent is subscribed to those events and has a method(s) to respond to them.
Of course, according to this conclusion, the main form handles the events that happens inside it by itself.
In reverse, when parent want to send message to any (child) class, it does it by calling the method in that (child) class DIRECTLY without events!
It make sense to me and please, correct me if I am wrong.
So, I just throw out the delegate and event in main form and do the next: in a class "cls_I_am_waiting.cs" I made a method "void frm_Main_event_Event()" a public method, in main class I made an instance of "cls_I_am_waiting.cs" and call its method "public void frm_Main_event_Event()" DIRECTLY and - it works!
Sorry, after a while, I came back and ask myself: O.K. it is good for a few children (classes) but what if I have 30, 50 or more classes that have to respond to message from main form?
Well, it turns the problem back on the beginning.


这篇关于用户类中未收到事件。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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