Link to home
Start Free TrialLog in
Avatar of moellert
moellert

asked on

Color Dialog in VB Express Edition

Hi,

when using this code in VB Express Edition:
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button2.Click

Dim MyDialog As New ColorDialog()
MyDialog.AllowFullOpen = False
MyDialog.ShowHelp = True
MyDialog.Color = TextBox1.ForeColor

If (MyDialog.ShowDialog() = Windows.Forms.DialogResult.OK) Then
TextBox1.ForeColor = MyDialog.Color
End If

End Sub

the variable TextBox1.ForeColor gets the value of "{Name=ffff8040,
ARGB=(255, 255, 128, 64)}".
How do I extract only the last three numbers in a string in a format like
"255 128 064"?
I need this result stored in a string.

Thanks for help
Thomas
Avatar of Rimvis
Rimvis
Flag of Lithuania image

Try this:

    Dim iColor As Long
    Dim iRed As Byte, iGreen As Byte, iBlue As Byte
   
    CommonDialog1.ShowColor
   
    iColor = CommonDialog1.Color
   
    iRed = iColor Mod 256
    iGreen = ((iColor And &HFF00) / 256&) Mod 256&
    iBlue = (iColor And &HFF0000) / 65536
   
    MsgBox iRed & " " & iGreen & " " & iBlue
SOLUTION
Avatar of Rimvis
Rimvis
Flag of Lithuania image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of moellert
moellert

ASKER

I have used this code now:

        Dim iColor As Long
        Dim iRed As Byte, iGreen As Byte, iBlue As Byte

        ColorDialog1.ShowDialog()

        iColor = ColorDialog1.Color

        iRed = iColor Mod 256
        iGreen = ((iColor And &HFF00) / 256&) Mod 256&
        iBlue = (iColor And &HFF0000) / 65536

        MsgBox(String.Format("{0} {1} {2}", ColorDialog1.Color.R, ColorDialog1.Color.G, ColorDialog1.Color.B))

I get an error message (line "   iColor = ColorDialog1.Color") stating that it is not possible to convert type of 'System.Drawing.Color' to type 'Long'
ASKER CERTIFIED SOLUTION
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
This code works perfectly. Thanks a lot.
@Idle Mind: Your code looks very different from the one Rimvis posted. I am not very good in VB programming so I could not decide if the solution Rimvis posted already solves my problem. Your does without any modification.

So who should get the points?
The important part is that the Color() class exposes the values you need via the R(), G(), and B() properties...which is what Rimvis demonstrated in his line of code:

     MsgBox(String.Format("{0} {1} {2}", ColorDialog1.Color.R, ColorDialog1.Color.G, ColorDialog1.Color.B))

I just took that already provided information and placed into a snippet of code that shows it in a different form (easier to read IMHO).

I didn't really provide any NEW information to the thread, just a variation of the same info.

This is why I think the points should go to Rimvis...    =)
I have splitted the points to be fair to both of you. Thanks a lot again.