Call a function from a dynamically created ImageButton

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Is it possible to call a function from a ImageButton wich I create
at runtime?

That would be really interesting

Many thanks for any suggest.

Greetings Patrick Marti
 
Hi Patrick

I would do it like this:

// this is created at runtime... maybe in onLoad or by another function....
Button someButton = new Button();
someButton.Name = "OnlyCallFunctionOne";
someButton.Click += new EventHandler( DynamicButton_Click );

Button someOtherButton = new Button();
someOtherButton.Name = "OnlyCallFunctionTwo";
someOtherButton.Click += new EventHandler( DynamicButton_Click );

// then you have the Click function
private void DynamicButton_Click( object sender, EventArgs e )
{
Button btn = (Button)sender;
switch( btn.Name )
{
case "OnlyCallFunctionOne": this.FunctionOne(); break;
case "OnlyCallFunctionTwo": this. FunctionTwo(); break;
}
}

private void FunctionOne()
{
// Some code...
}

private void FunctionTwo()
{
// Some code...
}


I just noticed you said ImageButton..... it should work just the same.

Hope it helps... did it in C# since u did not specify any other
language....then again i only use c# (^_^)
 
Back
Top