Note that there are some explanatory texts on larger screens.

plurals
  1. POImplementing a single event handler for multiple controls with different event handler delegates
    primarykey
    data
    text
    <p>I have a function, <code>void Validate()</code>, that contains all my validation logic for a window.</p> <p>I can't simply register this as an event handler because it doesn't accept the parameters required by the event handler delegates. Additionally different types of controls have different signatures so I can't just have <code>Validate</code> match one signature while ignoring their contents.</p> <p>Here is a small example of what I've setup</p> <pre><code> txt1.TextChanged += Validate_TextChange; password1.PasswordChanged += Validate_RoutedEvent; txt2.TextChanged += Validate_TextChange; txt3.TextChanged += Validate_TextChange; password2.PasswordChanged += Validate_RoutedEvent; txt4.TextChanged += Validate_TextChange; void Validate_RoutedEvent(object sender, RoutedEventArgs e) { ValidateOptions(); } void Validate_TextChange(object sender, TextChangedEventArgs e) { ValidateOptions(); } public void ValidateOptions() { //Actual validation here } </code></pre> <p>This just shows 2 examples, more controls could have even more signatures. Is there any better way of getting all event handlers to call a function in a case where I don't care about the arguments being passed?</p> <hr> <p>EDIT: I like the option proposed by Jon to add the parameters and simply ignore them. That does solve most of the problem but anytime I want to call this function directly, such as to manually trigger validation, I then have to include dummy arguments to satisfy the compiler. ValidateOptions(this, new EventArgs())</p> <p>The suggestion Dan made, to use anonymous functions, would handle this but isn't as clean when associating the event handlers. </p> <p>It doesn't appear that either solution lets you register a function as an event handler while ignoring the signature while also preserving the ability to call the function without creating dummy arguments but there are multiple ways to get pretty close.</p> <hr> <p>EDIT:</p> <p>Here is the updated example implementing Jon's solution for generic event handling but keeping a 0 parameter function that can be called directly</p> <pre><code>txt1.TextChanged += ValidationEvent; password1.PasswordChanged += ValidationEvent; txt2.TextChanged += ValidationEvent; txt3.TextChanged += ValidationEvent; password2.PasswordChanged += ValidationEvent; txt4.TextChanged += ValidationEvent; //Single event handler accepting EventArgs, which is the base class //for all more-specific event classes void ValidationEvent(object sender, EventArgs e) { //Ignores arguments and calls 0 argument ValidateOptions ValidateOptions(); } //0 argument function that performs actual validation and can be called //directly from other functions without the need to pass in a fake sender //and eventargs parameter public void ValidateOptions() { //Actual validation here } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload