Click here to Skip to main content
15,868,120 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:
C#
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:
C#
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.
Posted

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:
C#
public frm_Main()
{
    InitializeComponent();
                cls_I_am_waiting ciaw = new cls_I_am_waiting();
}
And see if that makes a difference...
 
Share this answer
 
Comments
Pecilije 21-Jul-14 6:37am    
No. Even worse, I can`t see main form.
OriginalGriff 21-Jul-14 6:59am    
What can you see? Anything?
What happens when you us the debugger?
Pecilije 21-Jul-14 16:10pm    
I am using debugger all the time. When I use non-static event and add "cls_I_am_waiting ciaw = new cls_I_am_waiting();" as you suggested, the program is running but there is nothing on the screen. Also happens without debugger. With the line of your code added and using the static event, program runs O.K. but, again, without the response on event. And this short program is for the forum because I found that problem in the other, much bigger application.
Can you suggest something else?
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.
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900