Definiowanie zdarzeń w formantach formularzy systemu Windows
Aby uzyskać szczegółowe informacje na temat definiowania zdarzeń niestandardowych, zobacz Zdarzenia. Jeśli zdefiniujesz zdarzenie, które nie ma żadnych skojarzonych danych, użyj typu podstawowego dla danych EventArgszdarzenia , i użyj go EventHandler jako delegata zdarzenia. Wystarczy zdefiniować element członkowski zdarzenia i chronioną On
metodę EventName , która zgłasza zdarzenie.
Poniższy fragment kodu pokazuje, jak kontrolka FlashTrackBar
niestandardowa definiuje zdarzenie niestandardowe, ValueChanged
. Pełny kod przykładu FlashTrackBar
można znaleźć w temacie How to: Create a Windows Forms Control That Shows Progress (Instrukcje: tworzenie kontrolki Formularzy systemu Windows pokazującej postęp).
Option Explicit
Option Strict
Imports System
Imports System.Windows.Forms
Imports System.Drawing
Public Class FlashTrackBar
Inherits Control
' The event does not have any data, so EventHandler is adequate
' as the event delegate.
' Define the event member using the event keyword.
' In this case, for efficiency, the event is defined
' using the event property construct.
Public Event ValueChanged As EventHandler
' The protected method that raises the ValueChanged
' event when the value has actually
' changed. Derived controls can override this method.
Protected Overridable Sub OnValueChanged(e As EventArgs)
RaiseEvent ValueChanged(Me, e)
End Sub
End Class
using System;
using System.Windows.Forms;
using System.Drawing;
public class FlashTrackBar : Control {
// The event does not have any data, so EventHandler is adequate
// as the event delegate.
private EventHandler onValueChanged;
// Define the event member using the event keyword.
// In this case, for efficiency, the event is defined
// using the event property construct.
public event EventHandler ValueChanged {
add {
onValueChanged += value;
}
remove {
onValueChanged -= value;
}
}
// The protected method that raises the ValueChanged
// event when the value has actually
// changed. Derived controls can override this method.
protected virtual void OnValueChanged(EventArgs e)
{
onValueChanged?.Invoke(this, e);
}
}
Zobacz też
.NET Desktop feedback
Opinia
https://aka.ms/ContentUserFeedback.
Dostępne już wkrótce: W 2024 r. będziemy stopniowo wycofywać zgłoszenia z serwisu GitHub jako mechanizm przesyłania opinii na temat zawartości i zastępować go nowym systemem opinii. Aby uzyskać więcej informacji, sprawdź:Prześlij i wyświetl opinię dla