Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 2008 > C# 2008 aka C# 3.0
|
C# 2008 aka C# 3.0 Discuss the Visual C# 2008 (aka C# 3.0) language
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# 2008 aka C# 3.0 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old April 22nd, 2008, 01:56 PM
Authorized User
 
Join Date: Apr 2008
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
Default Changing a label's color (a.k.a. colour) ?

Hell All

Ok, not quite so elementary this time : I have written a dozen pages or so of code and am therefore getting used to Visual C# --- of which I continue to find both the editor and in particular the debugger superb, no fooling.

Here's my current preoccupation.

I have a central chunk of 'pure C' which contains nested "for . . ." loops thus:

for (v=0; v<100; v++)
   for (w=0; w<200; w+=5)
      for (x=0; x<100; x+=2)
         for (y=0; y<50; y++)
             for(z=0; z<100; z++)
                {
                  (do some stuff . . .)
                }
             }
          }
       }
    }
 }
 ;

Now; ("some stuff") isn't enormously complex, but just the same we're looking at what, a billion or two iterations here?

Even with today's superchips, this takes long enough to make some kind of progress indicator a good idea, or the user may think he machine has gone to sleep. I know there's a progress bar, but what I thought I'd prefer is a small rectangle --- a label, for instance, up in one corner --- which simply switches from (say, a nice Salmon to a nice pale blue and back again, every time we reach the end of maybe the x loop?

It seems to me that the code for doing this is, colloquially:-

if label.Background = Salmon
     { label.Background = PaleBlue; goto yloopnext; }
     label.Background = Salmon;

yloopnext:


--- or something in that price-range?

But I don't think you can just use a straight assignment in this fashion, can you?

Please can somebody suggest a neat way of doing this? That is, switching a small rectangle from one colour to another and back,
programmatically?

Cheers

Martin Woodhouse


 
Old April 22nd, 2008, 04:48 PM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

Code:
if( label.BackColor == Colors.Salmon )
  label.BackColor = Colors.PaleBlue;
else
  label.BackColor = Colors.Salmon;
or even on one line:

Code:
label.BackColor = (label.BackColor==Colors.Salmon?Colors.PaleBlue:Colors.Salmon);
That is of course assuming Salmon and PaleBlue are real colours.



/- Sam Judson : Wrox Technical Editor -/
 
Old April 23rd, 2008, 12:38 PM
Authorized User
 
Join Date: Apr 2008
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hello Sam

Thank you very much. Now if I could just figure out how to make some actual colors accessible by name . . . (I imagine this means putting their names somewhere into the system ---- though not even the colors grouped under the actual heading of "System" in the 'background' portion of the label's properties seem accessible by name programmatically)

Cheers and thanx again, though,

Martin Woodhouse

 
Old April 23rd, 2008, 03:01 PM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

Colors:

http://msdn2.microsoft.com/en-us/lib...roperties.aspx

System colors (KnownColors enumeration):

http://msdn2.microsoft.com/en-us/lib...nowncolor.aspx

How to list lal known colours:

http://www.java2s.com/Code/CSharp/2D...rinasystem.htm

You could do something similar for System.Drawing.Color by using

Type colorType = typeof(Color);
FieldInfo[] fields = colorType.GetFields();

foreach(FieldInfo field in fields)
{
  Color c = Color.FromName(field.Name);
}




/- Sam Judson : Wrox Technical Editor -/
 
Old April 24th, 2008, 01:24 AM
Authorized User
 
Join Date: Apr 2008
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
Default


I am impressed with your encyclopaedic knowledge.

(I'm even a tiny bit impressed with myself, mind you --- I've only been at Visual C# for three weeks, the books I've bought from you amount to three inches in thickness, and already I'm proposing to edit the System files?

Next month: Grand Theft Auto for the PC, eh what?)

Meanwhile, however, I can't find the block of code for "System.Drawing.Color" --- where should I look for it?

Cheers,

Martin Woodhouse

 
Old April 24th, 2008, 01:32 AM
Authorized User
 
Join Date: Apr 2008
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
Default


( I can see "System.Drawing" listed in the headers for my .cs file, but I can't work out how to get at it ? )

Martin

 
Old April 24th, 2008, 02:01 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

If at the top of your file it says "using System.Drawing;" then thats all you need, you can then just refer to the class as "Color".

If you are still getting a compile error then let me know what the exact error is.

/- Sam Judson : Wrox Technical Editor -/
 
Old May 8th, 2008, 09:45 PM
Friend of Wrox
 
Join Date: Mar 2008
Posts: 133
Thanks: 15
Thanked 1 Time in 1 Post
Send a message via ICQ to iceman90289 Send a message via AIM to iceman90289
Default

going back to your first post, i havent really used enums to much in stuff i made on my own time, but i seem to recal being able to iterate through an enum, unless im confused with another language, maybe that could work... i havent really tried this before the post, but i just want to spark an idea. let me know how it worked, if it did at all, i would test it to see if what i am talking about can be done but i have some schoolwork that has to be done

hopefully i will be more helpful in future post, i hope i helped, one thing i know is that an array can be indexed based on the foor loops count, i dont know, its up to you.






Similar Threads
Thread Thread Starter Forum Replies Last Post
Changing Datagrid Colour based on the Value vuyiswamb VB.NET 2002/2003 Basics 0 May 22nd, 2008 02:24 AM
Changing Page Color darkestangel1980 CSS Cascading Style Sheets 6 February 6th, 2008 12:40 PM
Table row background colour changing meetravig Java GUI 0 September 21st, 2007 05:06 AM
Changing Fore colour and back colour darrenb Access VBA 2 May 25th, 2007 07:10 AM
Changing color conny Visual C++ 0 April 14th, 2005 07:57 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.