|
Subject:
|
Changing a label's color (a.k.a. colour) ?
|
|
Posted By:
|
Martin Woodhouse
|
Post Date:
|
4/22/2008 1:56:17 PM
|
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
|
|
Reply By:
|
samjudson
|
Reply Date:
|
4/22/2008 4:48:32 PM
|
if( label.BackColor == Colors.Salmon )
label.BackColor = Colors.PaleBlue;
else
label.BackColor = Colors.Salmon;
or even on one line:
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 -/
|
|
Reply By:
|
Martin Woodhouse
|
Reply Date:
|
4/23/2008 12:38:45 PM
|
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
|
|
Reply By:
|
samjudson
|
Reply Date:
|
4/23/2008 3:01:59 PM
|
Colors:
http://msdn2.microsoft.com/en-us/library/system.drawing.color_properties.aspx
System colors (KnownColors enumeration):
http://msdn2.microsoft.com/en-us/library/system.drawing.knowncolor.aspx
How to list lal known colours:
http://www.java2s.com/Code/CSharp/2D-Graphics/Listallknowncolorinasystem.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 -/
|
|
Reply By:
|
Martin Woodhouse
|
Reply Date:
|
4/24/2008 1:24:52 AM
|
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
|
|
Reply By:
|
Martin Woodhouse
|
Reply Date:
|
4/24/2008 1:32:02 AM
|
( 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
|
|
Reply By:
|
samjudson
|
Reply Date:
|
4/24/2008 2:01:51 AM
|
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 -/
|
|
Reply By:
|
iceman90289
|
Reply Date:
|
5/8/2008 9:45:40 PM
|
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.
|