Passing either RBG or HEX value to Server Control in VS.Net 2003

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I've created a server control that builds a table. I've exposed a public property that can change the background color of the table. When I compile the control and try to use it in a project, I am able to change the color by using the properties menu in Visual Studio.Net 2003. The problem that I'm having is that anytime that I want to enter a HEX value (rather than selecting a color from the color picker), the HEX value is converted to RGB and never displays the correct color.

Here is the code that I use to expose the color property in the server control

Color tablebgcolor = Color.Empty
public Color TableBGColo

get{return tablebgcolor;
set{tablebgcolor = value;


I've used other server controls that will accept either a HEX value or allow you select from the color picker on the properties tab in Visual Studio. Does anyone know how this is done?
 
This might get you going towards a solution. In my case I wanted to retrieve
HEX values stored as strings in a database and then assign them to a custom
class at runtime. In the following code, currentRow["BackColor"] contains a
HEX color value.

System.Web.UI.WebControls.WebColorConverter ColorToUse = new
WebColorConverter();

nextStyle.BackColor = (System.Drawing.Color)
ColorToUse.ConvertFrom(currentRow["BackColor"].ToString());

-K


D Sheldon said:
I've created a server control that builds a table. I've exposed a public
property that can change the background color of the table. When I compile
the control and try to use it in a project, I am able to change the color by
using the properties menu in Visual Studio.Net 2003. The problem that I'm
having is that anytime that I want to enter a HEX value (rather than
selecting a color from the color picker), the HEX value is converted to RGB
and never displays the correct color.
Here is the code that I use to expose the color property in the server control:

Color tablebgcolor = Color.Empty;
public Color TableBGColor
{
get{return tablebgcolor;}
set{tablebgcolor = value;}
}

I've used other server controls that will accept either a HEX value or
allow you select from the color picker on the properties tab in Visual
Studio. Does anyone know how this is done?
 
I found the following code, which seems to be working (ie it converts the RBG into HEX, in the code), but I still can't get the Visual Studio properties window to keep the HEX value (rather than automatically converting it to RBG).

private string ColorToHtml(Color c)

string html = ""
html = c.R.ToString("X")
if (html.Length < 2) html+="0"
html += c.G.ToString("X")
if (html.Length < 4) html+="0";
html += c.B.ToString("X")
if (html.Length < 6) html+="0"
return "#" + html


At this point, I guess I should be happy that the control is working correctly, but the O.C.D. in me can't stand it when VS.Net converts to RBG.
 
Back
Top