private void DomainUpDown_ParseError(object sender, UpDownParseErrorEventArgs e) { DomainUpDown dud = (DomainUpDown)sender; // get the text that was unable to parse. string text = e.Text; SolidColorBrush backgroundColor = null; // is might be a known color string, like "Yellow" // get by looking at the Colors class. PropertyInfo colorPropertyInfo = typeof(Colors).GetProperty(text, BindingFlags.Static | BindingFlags.Public); if (colorPropertyInfo != null) { backgroundColor = new SolidColorBrush((Color)colorPropertyInfo.GetValue(null, new object[] { })); } else { // it might be rgba code, like #aarrggbb if (text.StartsWith("#", StringComparison.OrdinalIgnoreCase) && text.Length == 9) { // rrggbbaa text = text.Substring(1); // test to see if it can be parsed to an int int result; if (Int32.TryParse(text, out result)) { byte[] rgba = new byte[4]; for (int i = 0; i < 4; i++) { rgba[i] = Byte.Parse(text.Substring(i * 2, 2), CultureInfo.CurrentCulture); } backgroundColor = new SolidColorBrush(new Color { A = rgba[0], B = rgba[3], G = rgba[2], R = rgba[1] }); } } } if (backgroundColor != null) { dud.Items.Add(new Border { Width = 120, Height = 80, Background = backgroundColor, BorderBrush = new SolidColorBrush(Colors.Yellow), BorderThickness = new Thickness(4) }); dud.CurrentIndex = dud.Items.Count - 1; } }
/// <summary> /// Raises the ParserError event when there is an error in parsing user /// input. /// </summary> /// <param name="e">Event args.</param> protected override void OnParseError(UpDownParseErrorEventArgs e) { ParseErrorActions.DoPreTest(e); base.OnParseError(e); ParseErrorActions.DoTest(e); }