Ler em inglês

Partilhar via


ControlEventHandler Delegar

Definição

Representa o método que manipulará os eventos ControlAdded e ControlRemoved da classe Control.

C#
public delegate void ControlEventHandler(object sender, ControlEventArgs e);
C#
public delegate void ControlEventHandler(object? sender, ControlEventArgs e);

Parâmetros

sender
Object

A fonte do evento.

e
ControlEventArgs

Um ControlEventArgs que contém os dados do evento.

Exemplos

O exemplo de código a seguir cria um Binding, adiciona um ConvertEventHandler delegado aos Parse eventos e Format e adiciona o BindingBindingsCollection ao de um TextBox controle por meio da DataBindings propriedade . O DecimalToCurrencyString delegado do evento, adicionado ao Format evento, formata o valor associado (um Decimal tipo) como moeda usando o ToString método . O CurrencyStringToDecimal delegado do evento, adicionado ao Parse evento, converte o valor exibido pelo controle de volta para o Decimal tipo .

C#
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);
}

Comentários

Ao criar um ControlEventArgs delegado, você identifica o método que manipulará o evento. Para associar o evento ao manipulador de eventos, adicione uma instância do delegado ao evento. O manipulador de eventos é chamado sempre que o evento ocorre, a menos que você remova o representante. Para obter mais informações sobre representantes do manipulador de eventos, consulte Manipulando e gerando eventos.

Métodos de Extensão

GetMethodInfo(Delegate)

Obtém um objeto que representa o método representado pelo delegado especificado.

Aplica-se a

Produto Versões
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9, 10

Confira também