Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Hive.Azure/HeuristicLab.Clients.Hive.CloudManager/3.3/Validation.cs @ 13897

Last change on this file since 13897 was 7424, checked in by spimming, 13 years ago

#1680:

  • ErrorProvider added to form
  • Implemented Validation methods for controls
  • Check Validation methods for controls in form when btn is clicked
File size: 3.0 KB
Line 
1using System;
2using System.ComponentModel;
3using System.Reflection;
4using System.Windows.Forms;
5
6namespace HeuristicLab.Clients.Hive.CloudManager {
7  // for more info see: http://stackoverflow.com/questions/769184/winform-ui-validation
8  public class Validation {
9    public static bool hasValidationErrors(System.Windows.Forms.Control.ControlCollection controls) {
10      bool hasError = false;
11
12      // Now we need to loop through the controls and deterime if any of them have errors
13      foreach (Control control in controls) {
14        // check the control and see what it returns
15        bool validControl = IsValid(control);
16        // If it's not valid then set the flag and keep going.  We want to get through all
17        // the validators so they will display on the screen if errorProviders were used.
18        if (!validControl)
19          hasError = true;
20
21        // If its a container control then it may have children that need to be checked
22        if (control.HasChildren) {
23          if (hasValidationErrors(control.Controls))
24            hasError = true;
25        }
26      }
27      return hasError;
28    }
29
30    // Here, let's determine if the control has a validating method attached to it
31    // and if it does, let's execute it and return the result
32    private static bool IsValid(object eventSource) {
33      string name = "EventValidating";
34
35      Type targetType = eventSource.GetType();
36
37      do {
38        FieldInfo[] fields = targetType.GetFields(
39             BindingFlags.Static |
40             BindingFlags.Instance |
41             BindingFlags.NonPublic);
42
43        foreach (FieldInfo field in fields) {
44          if (field.Name == name) {
45            EventHandlerList eventHandlers = ((EventHandlerList)(eventSource.GetType().GetProperty("Events",
46                (BindingFlags.FlattenHierarchy |
47                (BindingFlags.NonPublic | BindingFlags.Instance))).GetValue(eventSource, null)));
48
49            Delegate d = eventHandlers[field.GetValue(eventSource)];
50
51            if ((!(d == null))) {
52              Delegate[] subscribers = d.GetInvocationList();
53
54              // ok we found the validation event,  let's get the event method and call it
55              foreach (Delegate d1 in subscribers) {
56                // create the parameters
57                object sender = eventSource;
58                CancelEventArgs eventArgs = new CancelEventArgs();
59                eventArgs.Cancel = false;
60                object[] parameters = new object[2];
61                parameters[0] = sender;
62                parameters[1] = eventArgs;
63                // call the method
64                d1.DynamicInvoke(parameters);
65                // if the validation failed we need to return that failure
66                if (eventArgs.Cancel)
67                  return false;
68                else
69                  return true;
70              }
71            }
72          }
73        }
74
75        targetType = targetType.BaseType;
76
77      } while (targetType != null);
78
79      return true;
80    }
81
82  }
83}
Note: See TracBrowser for help on using the repository browser.