index > Visual C# General > handling events from EventControl by Form

handling events from EventControl by Form

How the form can see that I clicked a button on UserControl (so I woud like to see it on Label for example...). I tried to write a special event for it because I think that I do not have any access to Buttons. I thout that by writting event which return a button name I can get it by using Click event on Control and than pass to it the event wroted for UserControl, but when I clicked the button I realized that click event for UserControl is not used (it is handled when I click a Control - not button)

Thanks a lot for solution. Michal.




beginner with c#
klunk7

You can create a custom event on your usercontrol, which you invoke when this button on your UserControl has been pressed.

For instance, create a user-control with a custom event that is called 'ButtonAClicked'.

Then, in your form, you can hook up an event-handler for this event:


myUserControl.ButtonAClicked += new EventHandler ( .... );
 

In your user-control, you will need to write some code so that this event is raised when Button A has been clicked:


private void ButtonA_Click( object sender, EventArgs e )
{
   OnButtonAClick(sender, e);
}

 

The OnButtonAClick method just looks like this:


protected void OnButtonAClick( object sender, EventArgs e )
{
   if( ButtonAClicked != null )
   {
         ButtonAClicked (sender, e);
   }
}

 

(Well, this OnButtonAClick method is not that safe / good, note that you should also check whether you'll need to invoke the delegate, or if you can just execute it).




http://fgheysels.blogspot.com
Frederik Gheysels
reply 2

You can use google to search for other answers

 

More Articles

• Recently viewed items
• How to pass reference parameter to unmanaged dll
• Unable to write data to HttpWebRequest...
• Making a new event handler -- errors?
• Get System Information
• How to create msi setup?
• SoundPlayer won't play whole .wav file
• Issue looping through For Loop
• SqlBulkCopy problem
• The best way
Bookmark and Share
Welcome to Bokebb   New Update  
 

New Articles

• Create a new control to use at design time
• Having a problem w/RequiredFieldValidati
• How to change Form control fields from a
• abstract class
• How to creat array of bitmap?
• proper way to close a form using toolStr
• stand alone
• How can I execute a local application?
• Opening Database Record on a Separate Form
• I have to click twice to give an item fo
• Splitting Tiff Files...HELP!!!
• HtmlDocument and Object Tag
• Calling c# function in c++
• Integer TextBox
• DllImport - EntryPointNotFoundException

Hot Articles

• Server controls (textbox) does not work
• show backspace and CR in a textbox
• Decimal Problem...
• Visual C# 2005 versus Visual Basic 2005
• Video + Audio Confrence, Need Help
• How to change the column name in datagrid?
• Sockets programming
• Changing Date format
• Personal website - save username
• Accessing a Windows Integrated SSL secur
• running an application from resources
• msmq slow preformance
• Title Bar Color
• Customizing FileDialog window
• C# InputBox to use with a Console Applic

Recommend Articles

• accessing a function of windows applicat
• What si Marshall Class
• Problem of Accessibility?
• Visual Studio Academic Editions
• Matrix C#
• How to make INSERT INTO value's variable
• How can I use files .snippet ?
• Escaping decimal points in regular expre
• When and how the instance of a singleton
• How do I Retrieve MMS messages from a No
• Inserting multiple lines in textbox
• Closing a Web Form using Visual C#
• .Parent
• Encrypt a wmv file
• Get items from listview in MDIChilt to M