System.Drawing.Color and RGB entries

T

tshad

I have a problem setting the background color of textbox on the fly.

I tried using:

applicantID.backcolor = "F6F6F6"

and

applicantID.backcolor = "#F6F6F6"

But get an error message:

Compiler Error Message: BC30311: Value of type 'String' cannot be converted
to 'System.Drawing.Color'.

I don't want to use a color name, as I am using this color as the color of
my table.

How do I do this or can I do this?

Thanks,

Tom.
 
M

Mark Fitzpatrick

You don't need to specify the name. set the backcolor to
System.Drawing.Color.FromArgb(246,246,246). This overload of the method
takes the red/green/blue color channels as integers (246 is the decimal
equivalent of the hex F6).

Hope this helps,
Mark Fitzpatrick
Microsoft MVP - FrontPage
 
T

tshad

Mark Fitzpatrick said:
You don't need to specify the name. set the backcolor to
System.Drawing.Color.FromArgb(246,246,246). This overload of the method
takes the red/green/blue color channels as integers (246 is the decimal
equivalent of the hex F6).

That doesn't work either. I also can't use this:

applicantID.BorderStyle = "none"

I get the message:

Exception Details: System.FormatException: Input string was not in a correct
format.

I can do this and it works fine:

<asp:textbox id="applicantID" Style=" border-style:none;
background-color:#F6F6F6;" TextMode="SingleLine" Columns="32" runat="server"

The problem is I need to do this at runtime. I have a page that is going to
be for input in one case and for display for another. When it is display I
just want to be able to get rid of the borders on the textbox and set the
background to be the same as the table (which the above works if I set it
directly - the problem is that Style is not able to be set at runtime).

This is really driving me crazy.

Thanks,

Tom.
 
K

Ken Cox [Microsoft MVP]

Here's a way to handle the colour issue:

applicantID.BackColor = ColorTranslator.FromHtml("#66ccff")

However, since you are using a style try:

Private Sub Page_Load _
(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles MyBase.Load
applicantID.Attributes.Add("style", _
"border-style:none;background-color:#F6F6F6;")
End Sub

You could also create a CSS class that uses those properties and assign the
class name as the value of CssClass.

Does this help?

Ken
Microsoft MVP [ASP.NET]
 
T

tshad

Ken Cox said:
Here's a way to handle the colour issue:

applicantID.BackColor = ColorTranslator.FromHtml("#66ccff")

No that didn't do it.
However, since you are using a style try:

Private Sub Page_Load _
(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles MyBase.Load
applicantID.Attributes.Add("style", _
"border-style:none;background-color:#F6F6F6;")
End Sub

However, this one did work.

Why can you add a style, but you can't just use the style?
You could also create a CSS class that uses those properties and assign
the class name as the value of CssClass.

Can't get this one to work, if you look at my post just before this.

This is very frustrating - at least this is a solution I can use.

Thanks,

Tom.
Does this help?

Ken
Microsoft MVP [ASP.NET]




tshad said:
That doesn't work either. I also can't use this:

applicantID.BorderStyle = "none"

I get the message:

Exception Details: System.FormatException: Input string was not in a
correct format.

I can do this and it works fine:

<asp:textbox id="applicantID" Style=" border-style:none;
background-color:#F6F6F6;" TextMode="SingleLine" Columns="32"
runat="server"

The problem is I need to do this at runtime. I have a page that is going
to be for input in one case and for display for another. When it is
display I just want to be able to get rid of the borders on the textbox
and set the background to be the same as the table (which the above works
if I set it directly - the problem is that Style is not able to be set at
runtime).

This is really driving me crazy.

Thanks,

Tom.
 
K

Ken Cox [Microsoft MVP]

My, you've got one screwed up system. Do you have the namespaces imported?
If not, you have to give the fully qualified name:

Private Sub Page_Load _
(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles MyBase.Load
applicantID.Attributes.Add("style", _
"border-style:none;")
applicantID.BackColor =
System.Drawing.ColorTranslator.FromHtml("#66ccff")
End Sub
 
M

MWells

Mark and Ken have nailed this, but a small explanation might help clear up
any confusion.

There are effectively two different sets of controls in ASP.NET, organized
into distinct namespaces;

using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

All of the WebControls are represented in the HTML by tags that begin with
"<asp:";

<asp:TextBox id="TextBox1" runat="server"></asp:TextBox>

By contrast, HtmlControls are represented by standard HTML tags;

<input type="text" id="applicantID" runat="server" NAME="applicantID">

Note the extra stuff; runat="server" and a unique id are required in order
to access this control from your codebehind source; just like a WebControl.

You're using a WebControl, which protects you from many of the proprietary
inconveniences of raw HTML. Control properties like colors and border
styles are examples of this. Rather than using string constructions to set
these values, you use other .NET classes to control these settings.

Colors are represented through System.Drawing.Color, which has many
predefined colors for convenience. To create special colors, either use
System.Drawing.Color.FromArgb (int red, int green, int blue), or use
ColorTranslater.FromHtml ("#ffeedd") to convert Html hex strings.

Border styles are represented through an enum; in your case you'd want;

applicantID.BorderStyle = BorderStyle.None;

Learning the ASP.NET control object model will save you a lot in the long
run. However if you want to take a shortcut, you can use HtmlControls
instead. Create a standard HTML tag like;

<input type="text" id="applicantID" runat="server"></input>

And in your codebehind class;

protected System.Web.UI.HtmlControls.HtmlInputText applicantID;

This lets you access the standard HTML tag properties and set them all as
strings, just like you're used to. Since HTML text controls don't have
color or border properties (i.e. you have to set the style or class), you
would use;

applicantID.Style.Add ( "cssstyle_you_want", "value_you_want" );

This makes for less readable code, and lower portability between WebForms
and WinForms. The code is also much more prone to typograpic errors. For
practical purposes, you should consider this approach as a hack.

Hope this explains a bit of the "why".

/// M



Ken Cox said:
Here's a way to handle the colour issue:

applicantID.BackColor = ColorTranslator.FromHtml("#66ccff")

However, since you are using a style try:

Private Sub Page_Load _
(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles MyBase.Load
applicantID.Attributes.Add("style", _
"border-style:none;background-color:#F6F6F6;")
End Sub

You could also create a CSS class that uses those properties and assign the
class name as the value of CssClass.

Does this help?

Ken
Microsoft MVP [ASP.NET]




tshad said:
That doesn't work either. I also can't use this:

applicantID.BorderStyle = "none"

I get the message:

Exception Details: System.FormatException: Input string was not in a
correct format.

I can do this and it works fine:

<asp:textbox id="applicantID" Style=" border-style:none;
background-color:#F6F6F6;" TextMode="SingleLine" Columns="32"
runat="server"

The problem is I need to do this at runtime. I have a page that is going
to be for input in one case and for display for another. When it is
display I just want to be able to get rid of the borders on the textbox
and set the background to be the same as the table (which the above works
if I set it directly - the problem is that Style is not able to be set at
runtime).

This is really driving me crazy.

Thanks,

Tom.
 
T

tshad

Ken Cox said:
My, you've got one screwed up system. Do you have the namespaces imported?
If not, you have to give the fully qualified name:

Probably so.

Here is the top of the page:

****************************************************************************************
<%@ Page Language="VB" trace="false" debug="true" ContentType="text/html"
ResponseEncoding="iso-8859-1" %>
<%@ Register TagPrefix="fts" TAgName="header"
src="..\includes\staffingHeaders.ascx" %>
<%@ Register TagPrefix="fts" TAgName="footer"
src="..\includes\staffingFooters.ascx" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Web.Mail" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<%@ Import Namespace="System.Drawing" %>
******************************************************************************************

And the Page_Load:
********************************************************************
Sub Page_Load(sender as Object, e as EventArgs)

if not IsPostBack then
applicantID.enabled = "false"
'
applicantID.Attributes.Add("style","border-style:none;background-color:#F6F6F6;")
applicantID.Attributes.Add("style","border-style:none;")
System.Drawing.ColorTranslator.FromHtml("#66ccff")
fullName.enabled = "false"
Call SelectRecord()
end if
End Sub
********************************************************************

The commented out line works fine, but the following 2 lines (with the first
".Add" line commented), don't work.

BTW,

Why do you use 66ccff instead of F6F6F6?

Thanks,

Tom
 
T

tshad

MWells said:
Mark and Ken have nailed this, but a small explanation might help clear up
any confusion.

There are effectively two different sets of controls in ASP.NET, organized
into distinct namespaces;

using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

All of the WebControls are represented in the HTML by tags that begin with
"<asp:";

<asp:TextBox id="TextBox1" runat="server"></asp:TextBox>

By contrast, HtmlControls are represented by standard HTML tags;

<input type="text" id="applicantID" runat="server" NAME="applicantID">

Note the extra stuff; runat="server" and a unique id are required in order
to access this control from your codebehind source; just like a
WebControl.

You're using a WebControl, which protects you from many of the proprietary
inconveniences of raw HTML. Control properties like colors and border
styles are examples of this. Rather than using string constructions to
set
these values, you use other .NET classes to control these settings.

Colors are represented through System.Drawing.Color, which has many
predefined colors for convenience. To create special colors, either use
System.Drawing.Color.FromArgb (int red, int green, int blue), or use
ColorTranslater.FromHtml ("#ffeedd") to convert Html hex strings.

Border styles are represented through an enum; in your case you'd want;

applicantID.BorderStyle = BorderStyle.None;

Learning the ASP.NET control object model will save you a lot in the long
run. However if you want to take a shortcut, you can use HtmlControls
instead. Create a standard HTML tag like;

<input type="text" id="applicantID" runat="server"></input>

And in your codebehind class;

protected System.Web.UI.HtmlControls.HtmlInputText applicantID;

This lets you access the standard HTML tag properties and set them all as
strings, just like you're used to. Since HTML text controls don't have
color or border properties (i.e. you have to set the style or class), you
would use;

applicantID.Style.Add ( "cssstyle_you_want", "value_you_want" );

But why is this working. ApplicantID is an asp control. Yet, I can't get
it to work with the CSSClass tag, but I can get it to work with the above
"applicantID.Style.Add". This is what has been so confusing. Some of the
Class/Style tags seem to work and some don't (at least not in my pages).
This makes for less readable code, and lower portability between WebForms
and WinForms. The code is also much more prone to typograpic errors. For
practical purposes, you should consider this approach as a hack.

Hope this explains a bit of the "why".

Yes, it does.

Thanks,

Tom
/// M



Ken Cox said:
Here's a way to handle the colour issue:

applicantID.BackColor = ColorTranslator.FromHtml("#66ccff")

However, since you are using a style try:

Private Sub Page_Load _
(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles MyBase.Load
applicantID.Attributes.Add("style", _
"border-style:none;background-color:#F6F6F6;")
End Sub

You could also create a CSS class that uses those properties and assign the
class name as the value of CssClass.

Does this help?

Ken
Microsoft MVP [ASP.NET]




tshad said:
You don't need to specify the name. set the backcolor to
System.Drawing.Color.FromArgb(246,246,246). This overload of the
method
takes the red/green/blue color channels as integers (246 is the
decimal
equivalent of the hex F6).

That doesn't work either. I also can't use this:

applicantID.BorderStyle = "none"

I get the message:

Exception Details: System.FormatException: Input string was not in a
correct format.

I can do this and it works fine:

<asp:textbox id="applicantID" Style=" border-style:none;
background-color:#F6F6F6;" TextMode="SingleLine" Columns="32"
runat="server"

The problem is I need to do this at runtime. I have a page that is going
to be for input in one case and for display for another. When it is
display I just want to be able to get rid of the borders on the textbox
and set the background to be the same as the table (which the above works
if I set it directly - the problem is that Style is not able to be set at
runtime).

This is really driving me crazy.

Thanks,

Tom.

Hope this helps,
Mark Fitzpatrick
Microsoft MVP - FrontPage

I have a problem setting the background color of textbox on the fly.

I tried using:

applicantID.backcolor = "F6F6F6"

and

applicantID.backcolor = "#F6F6F6"

But get an error message:

Compiler Error Message: BC30311: Value of type 'String' cannot be
converted to 'System.Drawing.Color'.

I don't want to use a color name, as I am using this color as the color
of my table.

How do I do this or can I do this?

Thanks,

Tom.
 
M

MWells

But why is this working. ApplicantID is an asp control. Yet, I can't get
it to work with the CSSClass tag, but I can get it to work with the above
"applicantID.Style.Add". This is what has been so confusing. Some of the
Class/Style tags seem to work and some don't (at least not in my pages).

These should all work fine. It's best to view source on the rendered code
and verify that its generating what you expect. Once it's rendered as HTML,
ASP.NET's job is done and it's up to the browser to do the rest.

On occasion, it's helpful to view source, then save the source as a .html
page that you can hack up in notepad. At least that way you can determine
where your problem lies (invalid class names, css not being located, bad
javascript, etc.) without ASP.NET in the equation.

Also, in the old days, IE used to have a rather sticky cache. Make sure to
manually refresh (Ctrl+F5) when you're seeing unexpected results; it could
be a cache issue.

Hah, I typo'd "typographic". Too funny. I think I'll pretend it was an
intentional pun.
 
T

tshad

MWells said:
These should all work fine. It's best to view source on the rendered code
and verify that its generating what you expect. Once it's rendered as
HTML,
ASP.NET's job is done and it's up to the browser to do the rest.

On occasion, it's helpful to view source, then save the source as a .html
page that you can hack up in notepad. At least that way you can determine
where your problem lies (invalid class names, css not being located, bad
javascript, etc.) without ASP.NET in the equation.

Also, in the old days, IE used to have a rather sticky cache. Make sure
to
manually refresh (Ctrl+F5) when you're seeing unexpected results; it could
be a cache issue.

I have been doing all the above - which is how I found some of my problems,
as you mention.

In my other post on a similar issue, I found that I had the css file linked
twice (which seemed to cause the problem), but I am going to do another test
to see if that was the problem.
Hah, I typo'd "typographic". Too funny. I think I'll pretend it was an
intentional pun.

Take credit when you can :)

Thanks,

Tom.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top