AddHandler in C# doesn't work

B

Bjorn Sagbakken

With ASP.NET 2.0:
In VB code I have added controls dynamically, then added a handler using
AddHandler(Object, Method)
It all works fine.

Now I am trying to do the same in a C# module, but the AddHandler() is not
recognized at all, even though I have tried to add "using
System.ComponentModel;"

I fell like I am missing some basic stuff here, references or ..

Anyone with an idea or a simple example?

Bjorn
 
G

Guest

Hi Bjorn,

In C# we use += operator to attach event handlers:

TextBox textBox = new TextBox();
textBox.ID = "txt1";
textBox.Text = "Howdy";
textBox.AutoPostBack = true;
tetxBox.TextChanged += new EventHandler(this.TextChangedHandler);

private void TextChangedHandler(object sender, EventArgs e)
{
// do something here
TextBox textBox = (TextBox) sender;
string text = textBox.Text;
//
}

hope this helps
 
B

Bjorn Sagbakken

Thanks a lot for the great help.

Bjorn

Milosz Skalecki said:
Hi Bjorn,

In C# we use += operator to attach event handlers:

TextBox textBox = new TextBox();
textBox.ID = "txt1";
textBox.Text = "Howdy";
textBox.AutoPostBack = true;
tetxBox.TextChanged += new EventHandler(this.TextChangedHandler);

private void TextChangedHandler(object sender, EventArgs e)
{
// do something here
TextBox textBox = (TextBox) sender;
string text = textBox.Text;
//
}

hope this helps
 
C

Cowboy \(Gregory A. Beamer\)

AddHandler() in VB is equivalent to:

controlToAddTo.EventToHandle += new EventHandler();

If you want to look up anything, check wiring delegates, or similar, as this
is fairly basic work with delegates (events are delegates in C# -- a bit of
an oversimplification, but it works until you get more advanced).
 

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

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,226
Members
46,815
Latest member
treekmostly22

Latest Threads

Top