Custom Controls and events
-
Im sorry for asking this as it is probably already been covered, but i cant seem to put my queary into a search phrase that returns any thing useful to me. Here is my custom control related problem. Just to get my head round making custom controls im mocking up a simple windows like menu bar and start menu within my program. myMenu1: This is a custom control made up of 2 image boxes. 1 is the "start" button the other is the actual bar. When the user places the mouse in the transparent hot region (basicly the blank background of the menu item while the actual images reside offscreen) the menu bar images slide in from right to left, while the mouse hovers over the start button it changes color. myMenu2 This is a the actual menu that will pop up when the start button is pressed. it is made up of MenuItem custom control items.... MenuItem Just a label and image box that expose thier image, text and font properties. What i want to do is have an event in a menu item be handled by the main form directly rather than the custom control. At the moment the main form can only tell if im clicking on myMenu1 not wether im clicking on the start button or bar. It can also only tell if im clicking on myMenu2 not which MenuItem. What im angling for is to be able to click on myMenu1 start button and be able to use that event to set the visable and locaiton property of myMenu2 and be able to click on menu items in myMenu2 and have them function like indervidual buttons on my main form so they can fire of methods, etc. Again sorry if this has been covered, I am rusty on Events and Delegates and it just seems like a matter of makingthe Click events public... but im struggling getting my head round it :( Cheers.
-
Im sorry for asking this as it is probably already been covered, but i cant seem to put my queary into a search phrase that returns any thing useful to me. Here is my custom control related problem. Just to get my head round making custom controls im mocking up a simple windows like menu bar and start menu within my program. myMenu1: This is a custom control made up of 2 image boxes. 1 is the "start" button the other is the actual bar. When the user places the mouse in the transparent hot region (basicly the blank background of the menu item while the actual images reside offscreen) the menu bar images slide in from right to left, while the mouse hovers over the start button it changes color. myMenu2 This is a the actual menu that will pop up when the start button is pressed. it is made up of MenuItem custom control items.... MenuItem Just a label and image box that expose thier image, text and font properties. What i want to do is have an event in a menu item be handled by the main form directly rather than the custom control. At the moment the main form can only tell if im clicking on myMenu1 not wether im clicking on the start button or bar. It can also only tell if im clicking on myMenu2 not which MenuItem. What im angling for is to be able to click on myMenu1 start button and be able to use that event to set the visable and locaiton property of myMenu2 and be able to click on menu items in myMenu2 and have them function like indervidual buttons on my main form so they can fire of methods, etc. Again sorry if this has been covered, I am rusty on Events and Delegates and it just seems like a matter of makingthe Click events public... but im struggling getting my head round it :( Cheers.
Create a custom event delegate and event inside the control that your main form can use. When the in-control button is clicked have it check the custom event for registrars and fire it off it they exits. Examples: (I wrote this in the web browser, not a compiler so there may be a few compiler mistakes)
public delegate void OnClickStart(object,SystemEventArgs); public event OnClickStart ClickStart; private void button1_Clicked(object sender, SystemEventArgs e) { if(this.ClickStart != null) { this.ClickStart(sender,e); } }
Now in your main form use the code like such:CustomControl cc = new CustomControl; cc.ClickStart += new cc.OnClickStart(Mainform_ClickStart); public void Mainform_ClickStart(object sender, SystemEventArgs e) { //Make sure that if you do anything in here that could cause the form to PAINT that you //Use the this.Invoke method to invoke your MainForm methods. If you do not do this //you could have major threading issues and your form could lock up }
Try this out and let me know if you have any other questions. -- modified at 11:41 Friday 7th October, 2005 -
Im sorry for asking this as it is probably already been covered, but i cant seem to put my queary into a search phrase that returns any thing useful to me. Here is my custom control related problem. Just to get my head round making custom controls im mocking up a simple windows like menu bar and start menu within my program. myMenu1: This is a custom control made up of 2 image boxes. 1 is the "start" button the other is the actual bar. When the user places the mouse in the transparent hot region (basicly the blank background of the menu item while the actual images reside offscreen) the menu bar images slide in from right to left, while the mouse hovers over the start button it changes color. myMenu2 This is a the actual menu that will pop up when the start button is pressed. it is made up of MenuItem custom control items.... MenuItem Just a label and image box that expose thier image, text and font properties. What i want to do is have an event in a menu item be handled by the main form directly rather than the custom control. At the moment the main form can only tell if im clicking on myMenu1 not wether im clicking on the start button or bar. It can also only tell if im clicking on myMenu2 not which MenuItem. What im angling for is to be able to click on myMenu1 start button and be able to use that event to set the visable and locaiton property of myMenu2 and be able to click on menu items in myMenu2 and have them function like indervidual buttons on my main form so they can fire of methods, etc. Again sorry if this has been covered, I am rusty on Events and Delegates and it just seems like a matter of makingthe Click events public... but im struggling getting my head round it :( Cheers.
I'm not a dot net guru, but I had a similar issue and will share. I created a control that creates a SQL statement based upon user selections. I needed an event that would fire when the SQL was generated. I first declared the following: public delegate void SQLCreatedHandler(); [Category("Action")] [Description("Fires when the SQL is created")] public event SQLCreatedHandler SQLCreated; When I wanted to fire the event I called this code: //Raise event notifying subscribers that the SQL has been created OnSQLCreated(); And the OnSQLCreated() method looked like this: // If an event has no subscribers registerd, it will // evaluate to null. The test checks that the value is not // null, ensuring that there are subsribers before // calling the event itself. if (SQLCreated != null) { SQLCreated(); // Notify Subscribers } From the hosting application I created a handler for the event: this.queryControl1.SQLCreated += new QueryControl.QueryControl.SQLCreatedHandler(this.queryControl1_SQLCreated); And the method for the handler: private void queryControl1_SQLCreated() { MessageBox.Show(this.queryControl1.SQL); } Hope this helps, don't mind the codeing. I found the articles here on CP that helped me create the above code. Search the articles, not messageboards www.lovethosetrains.com
-
Create a custom event delegate and event inside the control that your main form can use. When the in-control button is clicked have it check the custom event for registrars and fire it off it they exits. Examples: (I wrote this in the web browser, not a compiler so there may be a few compiler mistakes)
public delegate void OnClickStart(object,SystemEventArgs); public event OnClickStart ClickStart; private void button1_Clicked(object sender, SystemEventArgs e) { if(this.ClickStart != null) { this.ClickStart(sender,e); } }
Now in your main form use the code like such:CustomControl cc = new CustomControl; cc.ClickStart += new cc.OnClickStart(Mainform_ClickStart); public void Mainform_ClickStart(object sender, SystemEventArgs e) { //Make sure that if you do anything in here that could cause the form to PAINT that you //Use the this.Invoke method to invoke your MainForm methods. If you do not do this //you could have major threading issues and your form could lock up }
Try this out and let me know if you have any other questions. -- modified at 11:41 Friday 7th October, 2005That is awesome! thank you SO much! Why cant my books explain that as simply as you have. Thanks again for opening my eyes. :-D
-
That is awesome! thank you SO much! Why cant my books explain that as simply as you have. Thanks again for opening my eyes. :-D