Thursday, September 24, 2009

Error management on a windows forms application form

I've been working on this as to help keep my error status straight on my windows form


//begin EPM
        private class ErrorProviderManager
        {
            private ErrorProvider _ep;
            Dictionary<Control, List<KeyValuePair<Func<bool>, String>>> dic =
                new Dictionary<Control, List<KeyValuePair<Func<bool>, String>>>();
            Dictionary<Control, List<String>> dicPossibleErrorStrings = new Dictionary<Control, List<String>>();
            List<Control> verifyWithErrorProviderList = new List<Control>();

            public ErrorProviderManager(ErrorProvider ep)
            {
                _ep = ep;
            }
            public bool hasErrors()
            {
                var result = false;
                foreach (var item in dic.Keys)
                {

                    var itemHasErrors = hasErrors(item);
                    if (itemHasErrors) result = true;
                }
                foreach (var item in verifyWithErrorProviderList)
                {
                    if (_ep.GetError(item).IsNullOrEmpty() == false)
                        result = true;
                }
                return result;
            }

            public bool hasErrors(Control item)
            {


                var itemHasErrors = false;
                if (_ep.GetError(item).IsNullOrEmpty() == false && dicPossibleErrorStrings[item].Contains(_ep.GetError(item)) == false)
                    itemHasErrors = true;
                else
                {
                    foreach (var kvp in dic[item])
                    {
                        if (itemHasErrors == false && kvp.Key.Invoke())
                        {
                            _ep.SetError(item, kvp.Value);
                            itemHasErrors = true;
                            break;
                        }

                    }

                    if (itemHasErrors == false)
                        _ep.SetError(item, string.Empty);
                }
                return itemHasErrors;
            }

            public void AddErrorCondition(Control c, Func<bool> errorCondition, String errorMessage)
            {
                if (errorMessage.IsNullOrWhitespace() == false)
                {
                    if (verifyWithErrorProviderList.Contains(c))
                        verifyWithErrorProviderList.Remove(c);
                    if (dic.ContainsKey(c) == false)
                        dic.Add(c, new List<KeyValuePair<Func<bool>, string>>());
                    dic[c].Add(new KeyValuePair<Func<bool>, String>(errorCondition, errorMessage));
                    if (dicPossibleErrorStrings.ContainsKey(c) == false)
                        dicPossibleErrorStrings.Add(c, new List<string>());
                    dicPossibleErrorStrings[c].Add(errorMessage);
                }
            }
            public void AddCheckForCustomErrorProviderText(Control c)
            {
                if (dicPossibleErrorStrings.ContainsKey(c) == false && verifyWithErrorProviderList.Contains(c) == false)
                    verifyWithErrorProviderList.Add(c);
            }

            private void _ClearError(Control c)
            {
                _ep.SetError(c, string.Empty);
            }

            public void refreshErrors()
            {
                hasErrors();
            }

        }
        //end EPM

No comments:

Post a Comment