custom event handling for gui
-
My mdi app has a navigation screen on the left hand side with link labels that when clicked will decide which form will get displayed on the right hand side. I want to pass a parameter to the eventhandler whenever a link label is clicked but I don't know how to do this. Please, can any one show me how? So if I have the code below, how can I pass formName to the OnClick method where the formName depends on the link label they click. public class Home { private void InitializeComponenet() { button1.Click += new System.EventHandler(OnClick); } private void OnClick(string formName) { eventHandler(formName) } }
-
My mdi app has a navigation screen on the left hand side with link labels that when clicked will decide which form will get displayed on the right hand side. I want to pass a parameter to the eventhandler whenever a link label is clicked but I don't know how to do this. Please, can any one show me how? So if I have the code below, how can I pass formName to the OnClick method where the formName depends on the link label they click. public class Home { private void InitializeComponenet() { button1.Click += new System.EventHandler(OnClick); } private void OnClick(string formName) { eventHandler(formName) } }
Look at delegates and EventArgs. That will show you how to pass parameters on events. If you have a main MDI window which creates your MDI child windows and the navigation is a MDI child window that you can only have one of (I am thinking of a left side tab strip type of navigation while the right side hosts all the MDI windows). If this is the case, your main window will have the reference to your navigation window (since it creates it). You could have your main window pass a reference to your navigation window to your MDI children windows. They would then be able to pass the event information directly back to the navigation window would having to raise events. MainForm NavigationWindow navWindow = new NavigationWindow(); ... // Some event happens and child window is bring created MyReportChildWindow child = new MyReportChildWindow(navWindow); NavWindow public void OnSpecialClient(string formName) { // Do something } MyReportChildWindow private NavigationWindow navWindow = null; public MyReportChildWindow(NavigationWindow navWindow) { this.navWindow = navWindow } public OnClicked(object Sender, EventArgs e) { navWindow.OnSpecialClient(this.Text); } There are other ways to handle this situation. With delegates you will have to have either the Nav window or the Child window know each other in order to register the delegate unless you had the main window set the delegate for the child window when it was created. Would be easier with the above than that. Another possiblity, is if there will only ever be one Navigation window, you can have it have a static member that you can call from all your child windows. That way you do no thave to pass anything, you could just call NavigationWindow.OnSpecialClient(this.Text) when the event occured. This kind of make the NavigationWindow method global to your application. Rocky Moore <><
-
My mdi app has a navigation screen on the left hand side with link labels that when clicked will decide which form will get displayed on the right hand side. I want to pass a parameter to the eventhandler whenever a link label is clicked but I don't know how to do this. Please, can any one show me how? So if I have the code below, how can I pass formName to the OnClick method where the formName depends on the link label they click. public class Home { private void InitializeComponenet() { button1.Click += new System.EventHandler(OnClick); } private void OnClick(string formName) { eventHandler(formName) } }
Thanks for your reply, but I am still not clear on how to implement what I am seeking. I know I have to create a class that derives from the System.EventArgs base class, but I am not sure how creating this class ties in with everything else. For example, where do I instaniate this class, how does that relate to onclick and the type the method takes. Any help is appreciated.