How to store color picker selection as a value?
How to store color picker selection as a value?
How to store color picker selection as a value?
How to store color picker selection as a value?
How to store color picker selection as a value?
How to store color picker selection as a value? How to store color picker selection as a value? How to store color picker selection as a value? How to store color picker selection as a value? How to store color picker selection as a value? How to store color picker selection as a value? How to store color picker selection as a value? How to store color picker selection as a value?
How to store color picker selection as a value? How to store color picker selection as a value?
How to store color picker selection as a value?
Go Back  Xtreme Visual Basic Talk > > > How to store color picker selection as a value?


Reply
 
Thread Tools Display Modes
  #1  
Old 05-12-2008, 10:02 AM
147 147 is offline
Newcomer
 
Join Date: May 2008
Location: Eastern Shore
Posts: 20
Default How to store color picker selection as a value?


I've searched the MSDN site, help systems for Visual Studio and this site, but I'm coming up empty on this one.

I'm trying to create a feature where the user is able to dynamically change colors for text and background in particular text boxes based on certain variables. Setting the variables and pulling the necessary info from a data table isn't a problem, though.

Where I'm having trouble is that I want the user to be able to pre-select which colors display for which variables. I am able to generate the code to bring up the color picker dialog box (see below) and I am able to transfer the user's selection back to the text and background color properties of a text box to display the new color.

My problem is saving that color as a data type that I can recall later and dynamically assign to the text color or background color on another form. My problem, I think, is that I don't know what data type the dialog.color really is. When I try to CType the color as a string or color I get an error saying it can't be converted to a string. If I try to convert it to an integer, I'm told it can't convert to an integer.

Is this possibly because I am trying to set the text property of the text box to this color? If so, is there a way to set the property to another control and then grab the color as a string from that control? Or, should I be re-evaluating what data type I'm using in the base table?

Code:
Private Sub btnTextColorPicker_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTextColorPicker.Click
        Dim MyDialog As New ColorDialog()
        MyDialog.AllowFullOpen = True
        MyDialog.ShowHelp = True
        MyDialog.Color = Color.White

        If (MyDialog.ShowDialog() = Windows.Forms.DialogResult.OK) Then
            Me.txtTextColor.Text = MyDialog.Color
        End If
    End Sub
If the data type in the table is wrong (currently set to varchar), which data type should I use, and will I be able to pull that data back out and use it to set the color properties later on?

Thanks for any help in advance!
Reply With Quote
  #2  
Old 05-12-2008, 10:20 AM
147 147 is offline
Newcomer
 
Join Date: May 2008
Location: Eastern Shore
Posts: 20
Talking

I just found the following thread:

convert string to color ?

I am now converting the MyDialog.Color to an ARGB color and then converting from the ARGB to a color.

Code:
            Dim strColor1 As String = MyDialog.Color.ToArgb()
            Dim tmpColor1 As Color = MyDialog.Color
            Me.txtTextColor.BackColor = tmpColor1
            Me.txtTextColor.Text = strColor1
            Me.txtColorTest.ForeColor = Color.FromArgb(txtTextColor.Text)

Last edited by 147; 05-12-2008 at 10:43 AM.
Reply With Quote
  #3  
Old 05-13-2008, 08:12 AM
clweeks clweeks is offline
Freshman
 
Join Date: Mar 2008
Posts: 33
Default

I'm missing something. Why not just store it as a system.drawing.color? Your code above shows you doing that but also storing as a string -- what's up with that?
Reply With Quote
  #4  
Old 05-22-2008, 07:49 AM
michael_spence michael_spence is offline
Newcomer
 
Join Date: Dec 2007
Posts: 12
Default hmmm Hope this helps

I've not done this with a database but I have used a text file to store user selected colours before, the colours were broken up into RGB after the user selected it from the colour picker dialog and written to the text file. Then the program used the following code as it read through the file to reproduce the colours using RGB: (the blue bits are what I think might interest you)

Dim foreColour As Color
Dim R, G, B As Integer 'colour rgb's


'get caption label fore colour
ElseIf record.StartsWith("CaptionText_R") Then
index = record.IndexOf("|")
returnText = record.Substring(index + 1)
R = returnText
foreColour = Color.FromArgb(R, G, B)
ElseIf record.StartsWith("CaptionText_G") Then
index = record.IndexOf("|")
returnText = record.Substring(index + 1)
G = returnText
foreColour = Color.FromArgb(R, G, B)
ElseIf record.StartsWith("CaptionText_B") Then
index = record.IndexOf("|")
returnText = record.Substring(index + 1)
B = returnText
foreColour = Color.FromArgb(R, G, B)
'get caption fore colour complete
'set caption fore colour
Me.ui_WelcomeScreenPicture_Label.ForeColor = foreColour

Colours are made up of R G and B which each have a range of 0 - 255, so you may have to use a byte as your data type.

Hope this helps
regards
Michael
Reply With Quote
  #5  
Old 05-22-2008, 08:48 AM
AtmaWeapon's Avatar
AtmaWeaponHow to store color picker selection as a value? AtmaWeapon is offline
Fabulous Florist

Forum Leader
* Guru *
 
Join Date: Feb 2004
Location: Austin, TX
Posts: 9,500
Default

Quote:
Originally Posted by clweeks View Post
I'm missing something. Why not just store it as a system.drawing.color? Your code above shows you doing that but also storing as a string -- what's up with that?
Because you can't. I've had to do this in the past when I was much less experienced, and I did it without using ARGB values and the result was a lot of embarrassing bugs.

The format for "Color.ToString()" is as follows:
Color [Name|argb]

If the color is specifically set using one of the known colors (such as Color.Red), then the output looks like "Color [Red]". However, if you set the color any other way, the output looks like "Color [A=xx R = xx...". The next logical thing to do is use Color.Name and Color.FromName, but this can cause problems as well:
Code:
Imports System.Drawing

Module Module1

    Sub Main()
        Dim colors(1) As Color
        Dim newColors(1) As Color

        colors(0) = Color.Red
        colors(1) = Color.FromArgb(255, 255, 0, 0) ' Red

        Console.WriteLine("Original colors:")
        For i As Integer = 0 To colors.Length - 1
            Console.WriteLine("{0}: ToString: {1} Name: {2} ARGB: {3}", _
                                i, colors(i).ToString(), colors(i).Name, colors(i).ToArgb())
            newColors(i) = Color.FromName(colors(i).Name)
        Next

        Console.WriteLine()
        Console.WriteLine("From names:")
        For i As Integer = 0 To colors.Length - 1
            Console.WriteLine("{0}: ToString: {1} Name: {2} ARGB: {3}", _
                    i, newColors(i).ToString(), newColors(i).Name, newColors(i).ToArgb())
            newColors(i) = Color.FromArgb(colors(i).ToArgb)
        Next

        Console.WriteLine()
        Console.WriteLine("From ARGB:")
        For i As Integer = 0 To colors.Length - 1
            Console.WriteLine("{0}: ToString: {1} Name: {2} ARGB: {3}", _
                    i, newColors(i).ToString(), newColors(i).Name, newColors(i).ToArgb())
        Next

    End Sub

End Module
Both colors are red, so the output should be the same for each color, right? Wrong. Here's the output on my machine:
Code:
Original colors:
0: ToString: Color [Red] Name: Red ARGB: -65536
1: ToString: Color [A=255, R=255, G=0, B=0] Name: ffff0000 ARGB: -65536

From names:
0: ToString: Color [Red] Name: Red ARGB: -65536
1: ToString: Color [ffff0000] Name: ffff0000 ARGB: 0

From ARGB:
0: ToString: Color [A=255, R=255, G=0, B=0] Name: ffff0000 ARGB: -65536
1: ToString: Color [A=255, R=255, G=0, B=0] Name: ffff0000 ARGB: -65536
Press any key to continue . . .
Notice that in the first case, ToString and Name are different, but the ARGB value is identical. When we take the reported name and use Color.FromName to create another color, notice that the ARGB changes for the second color. When we use ARGB values, notice that we lose the name of the first color, but the ARGB remains constant. The name is a label; the ARGB determines how the color will be displayed. I'd rather have my ARGB values remain constant than the names.

If you want to save a color to a file, ARGB is the only reliable way to do so.
__________________
.NET Resources
My FAQ threads | Tutor's Corner | Code Library
I would bet money 2/3 of .NET questions are already answered in one of these three places.

Last edited by AtmaWeapon; 05-22-2008 at 08:53 AM. Reason: stupid copy/paste problems
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
How to store color picker selection as a value?
How to store color picker selection as a value?
How to store color picker selection as a value? How to store color picker selection as a value?
How to store color picker selection as a value?
How to store color picker selection as a value?
How to store color picker selection as a value? How to store color picker selection as a value? How to store color picker selection as a value? How to store color picker selection as a value? How to store color picker selection as a value? How to store color picker selection as a value? How to store color picker selection as a value?
How to store color picker selection as a value?
How to store color picker selection as a value?
 
How to store color picker selection as a value?
How to store color picker selection as a value?
 
-->