Export (0) Print
Expand All
Expand Minimize
This topic has not yet been rated - Rate this topic

ControlEventHandler Delegate

Represents the method that will handle the ControlAdded and ControlRemoved events of the Control class.

Namespace: System.Windows.Forms
Assembly: System.Windows.Forms (in system.windows.forms.dll)

public delegate void ControlEventHandler (
	Object sender,
	ControlEventArgs e
)
/** @delegate */
public delegate void ControlEventHandler (
	Object sender, 
	ControlEventArgs e
)
JScript supports the use of delegates, but not the declaration of new ones.

Parameters

sender

The source of the event.

e

A ControlEventArgs that contains the event data.

When you create a ControlEventArgs delegate, you identify the method that will handle the event. To associate the event with your event handler, add an instance of the delegate to the event. The event handler is called whenever the event occurs, unless you remove the delegate. For more information about event handler delegates, see Events and Delegates.

The following code example creates a Binding, adds a ConvertEventHandler delegate to both the Parse and Format events, and adds the Binding to the BindingsCollection of a TextBox control through the DataBindings property. The DecimalToCurrencyString event delegate, added to the Format event, formats the bound value (a Decimal type) as currency using the ToString method. The CurrencyStringToDecimal event delegate, added to the Parse event, converts the value displayed by the control back to the Decimal type.

private void BindControl()
{
   // Create the binding first. The OrderAmount is typed as Decimal.
   Binding b = new Binding
      ("Text", ds, "customers.custToOrders.OrderAmount");
   // Add the delegates to the events.
   b.Format += new ConvertEventHandler(DecimalToCurrencyString);
   b.Parse += new ConvertEventHandler(CurrencyStringToDecimal);
   text1.DataBindings.Add(b);
}

private void DecimalToCurrencyString(object sender, ConvertEventArgs cevent)
{
   // Check for the appropriate DesiredType.
   if(cevent.DesiredType != typeof(string)) return;

   // Use the ToString method to format the value as currency ("c").
   cevent.Value = ((decimal) cevent.Value).ToString("c");
}

private void CurrencyStringToDecimal(object sender, ConvertEventArgs cevent)
{
   // Check for the appropriate DesiredType. 
   if(cevent.DesiredType != typeof(decimal)) return;

   // Convert the string back to decimal using the static Parse method.
   cevent.Value = Decimal.Parse(cevent.Value.ToString(),
   NumberStyles.Currency, null);
}


private void BindControl()
{
    // Create the binding first. The OrderAmount is typed as Decimal.
    Binding b = new Binding("Text",ds,"customers.custToOrders.OrderAmount");
    // Add the delegates to the events.
    b.add_Format(new ConvertEventHandler(DecimalToCurrencyString));
    b.add_Parse(new ConvertEventHandler(CurrencyStringToDecimal));
    text1.get_DataBindings().Add(b);
} //BindControl

private void DecimalToCurrencyString(Object sender, ConvertEventArgs cevent)
{
    // Check for the appropriate DesiredType.
    if (!(cevent.get_DesiredType().Equals(String.class.ToType()))) {
        return;
    }
    // Use the ToString method to format the value as currency ("c").
    cevent.set_Value(((System.Decimal)cevent.get_Value()).ToString("c"));
} //DecimalToCurrencyString

private void CurrencyStringToDecimal(Object sender, ConvertEventArgs cevent)
{
    // Check for the appropriate DesiredType. 
    if (!(cevent.get_DesiredType().Equals(System.Decimal.class.ToType()))) {
        return;
    }
    // Convert the string back to decimal using the static Parse method.
    cevent.set_Value(Decimal.Parse(cevent.get_Value().ToString(), 
        NumberStyles.Currency, null));
} //CurrencyStringToDecimal

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.