C# event Handles

N

Nina

I have the code in VB with Handles event that creates clickable labels on a
form. I’m trying to write same code in C#. However it doesn’t work. Please
help me to find a solution for event Handles in C#.

VB Handles
---------------------
Public Class clsLabel
Inherits Control

Event labelClick(ByVal sender As Object, ByVal e As System.EventArgs)

Private Sub clsLabelClick(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.Click
RaiseEvent labelClick(sender, e)
End Sub
End Class


Public Class Form1
Inherits System.Windows.Forms.Form
Friend WithEvents lblEnTick As clsLabel

Private Sub InitializeLabels()
lblEnTick = New clsLabel
End Sub

Private Sub lblclick(ByVal lbl As Object, ByVal e As System.EventArgs)
Dim l As clsLabel = CType(lbl, clsLabel)
End Sub
End Class



C# Handles?
------------------------------------------
class clsLabel : Control
{
public delegate void labelClickEventHandler(object sender, System.EventArgs
e);
public event labelClickEventHandler labelClick;

private void clsLabelClick(object sender, System.EventArgs e)
{
if (labelClick != null)
labelClick(sender, e);
}
}


public partial class Form1 : System.Windows.Forms.Form
{
private void InitializeLabels()
{
lblEnTick = new clsLabel();
lblEnTick.labelClick += lblclick;
}

private void lblclick(object lbl, System.EventArgs e)
{
clsLabel l = (clsLabel)lbl;
}
}
 
C

Chris Tacke, eMVP

Sure, you never wire up the base Click event in the C# code like you do in
the VB code. (I also don't like your use of antiquated hungarian notation,
but that's anotehr story).

class clsLabel : Control
{
public delegate void labelClickEventHandler(object sender, System.EventArgs
e);
public event labelClickEventHandler labelClick;

public clsLabel()
{
base.Click += clsLabelClick;
}

private void clsLabelClick(object sender, System.EventArgs e)
{
if (labelClick != null)
labelClick(sender, e);
}
}


--

Chris Tacke, Embedded MVP
OpenNETCF Consulting
Giving back to the embedded community
http://community.OpenNETCF.com
 
N

Nina

Thank you, Chris
It works now.

Chris Tacke said:
Sure, you never wire up the base Click event in the C# code like you do in
the VB code. (I also don't like your use of antiquated hungarian notation,
but that's anotehr story).

class clsLabel : Control
{
public delegate void labelClickEventHandler(object sender, System.EventArgs
e);
public event labelClickEventHandler labelClick;

public clsLabel()
{
base.Click += clsLabelClick;
}

private void clsLabelClick(object sender, System.EventArgs e)
{
if (labelClick != null)
labelClick(sender, e);
}
}


--

Chris Tacke, Embedded MVP
OpenNETCF Consulting
Giving back to the embedded community
http://community.OpenNETCF.com
 
S

Simon Hart [MVP]

Talk about this hungarian notation Chris, I have just spent the last week or
so looking at our clients VB.NET 1.1 apps that use just that style. Classes
prefixed with cls, object variables prefixed with o etc etc. It drives me
*mad* it's bloody awful to read, it doesn't fit at all into the .NET
framework and it gives no value at all in a OO strongly typed language like
C#/VB.NET.

If anyone still writes code this way, please read this before writing
another line of code! :)
http://www.amazon.co.uk/Framework-D...bs_sr_1?ie=UTF8&s=books&qid=1200000064&sr=8-1
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top