1
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{ 
    string userColourString = value.ToString();
    Debug.WriteLine(userColourString);
    long userColourNumeric = 0; 
    Int64.TryParse(userColourString, out userColourNumeric); 
    var colourToUse = userColourNumeric;
    return (Color)ColorConverter.ConvertFromString(string.Format("#{0:x6}", colourToUse));
}

I'm trying to convert the following two values into colours by using the converter method above but it's not working,. -2147483630 16777215

Infinite Recursion
  • 6,511
  • 28
  • 39
  • 51
user3044294
  • 205
  • 1
  • 15

1 Answers1

1

The value 16777215 decimal converts to FFFFFF hexadecimal. I tested your code, and the value of colourToUse is indeed "#ffffff". That will easily convert to the color White.

The value -2147483630 decimal ends up being converted to FFFFFFFF80000012 hexadecimal. I'm not sure what color you're hoping that'll convert to. No wonder the ConvertFromString method throws a format exception.


You added that you're referencing an old chart of VB6 color constants.

In order to generate the colors in that chart, you'll need to use ColorTranslator.FromWin32:

public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
    var userColourString = value.ToString();
    int userColourNumeric = 0;
    int.TryParse(userColourString, out userColourNumeric);
    var colourToUse = userColourNumeric;
    return ColorTranslator.FromWin32(colourToUse);
}
Grant Winney
  • 65,241
  • 13
  • 115
  • 165
  • The -2147483630=&H80000012 this is the value list. http://wiki.robotz.com/index.php/Color_Constants_/_Color_Forms_/_Controls_/_Transparency_VB6 – user3044294 Jul 22 '14 at 05:50
  • the reason is i am working on old vb6 project and rewrite that project into c#. both project are need to be work simultaneously. that -2147483630 value are still use in vb6 user.and i have to use same database for get value :( – user3044294 Jul 22 '14 at 06:05
  • i am using System.Windows.Media because TargetType is "System.Windows.Media". Your "ColorTranslator" is from System.Drawing. if you can give one small hint to do this color converter using System.Windows.Media it's more helpfull. thank you. – user3044294 Jul 22 '14 at 06:53