edgarmtze
edgarmtze

Reputation: 25048

Convert string to color VB.NET

I am Using colordialog to let user pick a color, which is then saved into a db. when loading the color to fill a lable it comes as a string. How to convert:

Color `[A=255, R=128, G=128, B=255]` to color

Is there a way to save user selected values

If I use these options, the alpha value will be lost

Dim c As Color
c = Color.FromName("red")
c = Color.FromArgb(255, 0, 0)  
c = Color.FromKnownColor(KnownColor.Red)

Upvotes: 1

Views: 3482

Answers (1)

Edgar
Edgar

Reputation: 489

As the comments say you can store the 32-bit ARGB value. It's basically a numeric value. If you code this way then there won't be any need to write a function to extract the color value.

'your string selected from a color dialog
Dim clrDialog As New ColorDialog
If clrDialog.ShowDialog = Windows.Forms.DialogResult.OK Then
        TextBox1.Text = clrDialog.Color.ToArgb.ToString 
End If
clrDialog.Dispose()

' to get back ARGB value from string
TextBox1.BackColor = Color.FromArgb(CInt(TextBox1.Text))

Upvotes: 1

Related Questions