Firing code from events in code behind of .NET user control

A PowerBuilder Developers’ perspective

[important]If you are not a PowerBuilder developer you may choose to skip this paragraph. [/important]

Something as simple as causing some code to be fired for an event in a .NET program is not entirely intuitive for PowerBuilder developers particularly for events of controls that you are not familiar with like “datagrid”.

The difference in .NET is that you need to create the event code method declaration with proper parameters, and return values manually, or have it created automatically via the Visual Studio IDE.

Common between PowerBuilder and .NET

  • The event will be named
  • The parameters can be used in the code block.
  • The return value is set upon exiting the code block.
  • The event code runs when the corresponding event of the user object occurs.

The PowerBuilder 12.5 Classic example below shows a simple command button “clicked” event with code added that will be fired if the command button is clicked.

PowerBuilder Event Code Behind

Firing code in .NET when a user control event is invoked

There are several ways of coding an event for a user object in .NET, four that come to mind are listed below. You can drill into more detail by referring to .NET Framework reference.

Overall, the process of tying events to objects and coding them is similar to mainstream OOP languages.

  • Option 1) You can double click the button in the Visual Studio designer, which will launch the code editor for the most common event type, like click for a button.  The event method is created using a naming convention of object name underscore and event name.
  • Option 2) You can select the button in Visual Studio designer, go to Properties, then switch the tab to events, find the event you want to code, then enter a name for the event. Visual Studio will create the event wrapper for you to enter your code.
  • Option 3) You can manually enter the event stub, name it, and enter the XAML code assigning the actual button Click event to the newly created event method you created.
  • Option 4) You can tie events to controls at run-time in code, even assigning multiple events to a single event handler.   Basically you create an instance of the EventHandler delegate, passing to it the address of the method to bind to.

 

The following C# example shows how to bind the Click event of the Button1 control to a method named myEventHandler:

 

Button1.Click += new System.EventHandler(this.myEventHandler);

Sample of a Button in .NET and the event handler along with code that ties the event handler and the control together..NET Coding User Object Events

 

The process of capturing events and running code when the event is fired in .NET is relatively simple once you figure it out. Some of the less common events on less common controls can be more challenging but you can usually find everything you need in the MSDN documentation.

 

 

Tags:

No responses yet

Leave a Reply

Your email address will not be published. Required fields are marked *