display all colors

M

Mark

Is there a programatic way to iterate through all the named colors?
Something like:

HtmlTable ht = new HtmlTable();
HtmlTableRow htr;
HtmlTableCell htc;
foreach (Color c in [collection of all colors]) //I CAN'T FIND COLLECTION OF
ALL COLORS
{
htr = new HtmlTableRow();
htc = new HtmlTableCell();
htc.InnerText = c.Name;
htr.Cells.Add(htc);
ht.Rows.Add(htr);
}
phColors.Controls.Add(ht); //Display table in a placeholder control

Thanks in advance!
Mark
 
K

Ken Cox [Microsoft MVP]

Here's some code I use that might give you a hand:

Private Sub Page_Load _
(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles MyBase.Load
If Not IsPostBack Then
Dim strSelectedColor As String
Dim li As New ListItem
Dim enumColor As New KnownColor
Dim Colors As Array = _
[Enum].GetValues(enumColor.GetType())
DropDownList1.DataSource = Colors
DropDownList1.DataBind()
strSelectedColor = DropDownList1.Items(0).Text
li.Value = 0
li.Text = "--ALL"
DropDownList1.Items.Insert(0, li)
End If
End Sub 'Page_Load


Private Sub DropDownList1_SelectedIndexChanged _
(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles DropDownList1.SelectedIndexChanged
Label1.Text = DropDownList1.SelectedItem.Text
End Sub

Ken
Microsoft MVP [ASP.NET]
 
C

Curt_C [MVP]

collection of colors from where?
Server? These would be irrelevant since they are interpreted by the client.
Client? nope....can't get em, besides, named colors can be "adjusted" I
believe. Get the #00000 values. Probably easier to just build a list of
standards though.
 
M

Mark

Good points. However, let's assume that I had the same client on every
desktop, which means the colors would render the same. We're not shooting
for perfection - just a display of the available colors and their names
and/or numbers. What's the best method of iterating through the "typical"
available colors?

Thanks again.

Mark

Curt_C said:
collection of colors from where?
Server? These would be irrelevant since they are interpreted by the client.
Client? nope....can't get em, besides, named colors can be "adjusted" I
believe. Get the #00000 values. Probably easier to just build a list of
standards though.

--
Curt Christianson
Owner/Lead Developer, DF-Software
www.Darkfalz.com


Mark said:
Is there a programatic way to iterate through all the named colors?
Something like:

HtmlTable ht = new HtmlTable();
HtmlTableRow htr;
HtmlTableCell htc;
foreach (Color c in [collection of all colors]) //I CAN'T FIND
COLLECTION
OF
ALL COLORS
{
htr = new HtmlTableRow();
htc = new HtmlTableCell();
htc.InnerText = c.Name;
htr.Cells.Add(htc);
ht.Rows.Add(htr);
}
phColors.Controls.Add(ht); //Display table in a placeholder control

Thanks in advance!
Mark
 
M

Mark

Thanks!! This worked well. I ended up using the following code to create
an HtmlTable instance and add it to a placeholder control on my page:

HtmlTable ht = new HtmlTable();
HtmlTableRow htr;
HtmlTableCell htc;

KnownColor enumColor = new KnownColor();
Array colors = Enum.GetValues(enumColor.GetType());

bool bFirstColorFound = false;
for (int i = 0; i < colors.Length; i++)
{
string strColor = colors.GetValue(i).ToString();
if ( strColor == "AliceBlue")
{
bFirstColorFound = true;
}
if (bFirstColorFound == true)
{
htr = new HtmlTableRow();

//FIRST CELL
htc = new HtmlTableCell();
htc.InnerText = strColor ;
htr.Cells.Add(htc);

//SECOND CELL
htc = new HtmlTableCell();
htc.BgColor = strColor;
htc.InnerHtml = @"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
htr.Cells.Add(htc);

//Add row to table.
ht.Rows.Add(htr);
}
}

phColors.Controls.Add(ht);




Ken Cox said:
Here's some code I use that might give you a hand:

Private Sub Page_Load _
(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles MyBase.Load
If Not IsPostBack Then
Dim strSelectedColor As String
Dim li As New ListItem
Dim enumColor As New KnownColor
Dim Colors As Array = _
[Enum].GetValues(enumColor.GetType())
DropDownList1.DataSource = Colors
DropDownList1.DataBind()
strSelectedColor = DropDownList1.Items(0).Text
li.Value = 0
li.Text = "--ALL"
DropDownList1.Items.Insert(0, li)
End If
End Sub 'Page_Load


Private Sub DropDownList1_SelectedIndexChanged _
(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles DropDownList1.SelectedIndexChanged
Label1.Text = DropDownList1.SelectedItem.Text
End Sub

Ken
Microsoft MVP [ASP.NET]


Mark said:
Is there a programatic way to iterate through all the named colors?
Something like:

HtmlTable ht = new HtmlTable();
HtmlTableRow htr;
HtmlTableCell htc;
foreach (Color c in [collection of all colors]) //I CAN'T FIND COLLECTION
OF
ALL COLORS
{
htr = new HtmlTableRow();
htc = new HtmlTableCell();
htc.InnerText = c.Name;
htr.Cells.Add(htc);
ht.Rows.Add(htr);
}
phColors.Controls.Add(ht); //Display table in a placeholder control

Thanks in advance!
Mark
 

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,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top