Search code examples
vb.netcolorsvb6ocx

cannot get a vb6 control with a color function to work in vb.net


I have a vb6 ocx control that has a function that takes, among other things, a long to set the color of a label component of the control. This all works fine until you use the control in a vb.net 64bit environment where longs are ints and color is a different type anyway.

I tried changing the function to take 3 ints for rgb and then using the rgb function to get the color to use but the form will not load in the vb.net environment with the control on it.

Does anyone know how I can change the control's code in vb6 so that it can be used in vb.net?


Solution

  • You can use a built in method to get the equivalent color value to use in your application:

    Dim myColor As Color = Color.Red
    
    ' Translate myColor to an OLE color. 
    Dim winColor As Integer = ColorTranslator.ToWin32(myColor)
    

    The result is an Int32 which is a Long in VB6

    So...

    In your VB6 control you can expose a Color property like this:

    Public Property Get ColorValue as Long
        ColorValue = lblColor.BackColor
    End Property
    
    Public Property Let ColorValue(value as Long)
        lblColor.BackColor = value
    End Property
    

    Then in you VB.NET application you can set it like this:

    MyControl.ColorValue = ColorTranslator.ToWin32(Color.Red)