Click here to Skip to main content
15,899,124 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
I have a in VB6 and I converted to C# but it is not work and I do not understand How it work. I hope that you can help me.
thanks?

this is code of usercontrol in Vb6:
VB
Event CloseClick()
Private Sub btnClose_Click()
   RaiseEvent CloseClick                                   
End Sub


and this is code in C#:
C#
public event CloseClickEventHandler CloseClick;
        public delegate void CloseClickEventHandler(System.Object Sender, System.EventArgs e);
        private void btnClose_Click(System.Object eventSender, System.EventArgs eventArgs)
        {
            if (CloseClick != null) {
                CloseClick(this, null);
            }
           
        }
Posted

1 solution

Do you want to make it working or want to know how it works?

You attempt to code this shows you need both.

You don't need delegate declaration. As Click even is already defined in the library, appropriate delegate type is already defined. A standard recommendations for event definition say that first argument should of the the type System.Object, a second one — if the type System.EventArgs or derived. For controls, a run-time type passes as the first argument is of the same type as the control firing the event. It can be something else, like MenuItem (which is not derived from System.Windows.Forms.Control).

C#
MyButton.Click += delegate(object sender, System.EventArgs eventArgs) {
    System.Windows.Forms.MessageBox.Show(
        string.Format("{0} clicked!", (Button)sender.Text));
} //MyButton.Click


Most likely, you're using C# v.3 or later, so lambda syntax is simpler, you need not specify the types of arguments as they are automatically inferred from delegate type known to the compiler from event type.

C#
MyButton.Click += (sender, eventArgs) => {
    //same thing
} //MyButton.Click


These code fragment should be called anytime before your form is shown; I usually do it in the constructor of the form, at the very end, as well as all other settings and layout adjustments.

Explaining all that could take a whole article. Look for overview of delegates, events and anonymous delegates in regular help/MSDN, it should be enough. I answered to similar CodeProject Questions number of times, see, for example: What are the advantages of delegates in C#.NET?[^], difference between Lambda expression and Action[^], look at other answers and recommended reading as well.

—SA
 
Share this answer
 
v4
Comments
Yusuf 20-Feb-11 23:01pm    
Good answer
Sergey Alexandrovich Kryukov 20-Feb-11 23:25pm    
Thank you, Yusuf,
--SA
Albin Abel 20-Feb-11 23:06pm    
Good explanation my 5
Sergey Alexandrovich Kryukov 20-Feb-11 23:26pm    
Thank you, Albin.
--SA
Abhinav S 20-Feb-11 23:07pm    
Good answer.5.

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