|
Here's the real problem: I have 81 pictureBoxes and I want to change their BackgroundImage at once. I'm looking for a solution to stay away from writing 81 statements. I need a code snippet to do so.
|
|
|
|
|
Are you trying to convert them into the same image?
|
|
|
|
|
Yes. I'm trying to reach something like this:
for (Counter = 1; Counter <= 81; Counter++)
{
picturebox|COUNTER|.BackgroundImage = Resource1.Img1;
}
|
|
|
|
|
Off the top of my head, I would possibly use something like this:
private void ChangePictureBoxes(Control parentControl, Image img)
{
foreach (Control control in parentControl.Children)
{
PictureBox picture = control as PictureBox;
if (picture != null)
{
picture.BackgroundImage = img;
continue;
}
ChangePictureBoxes(control);
}
}
|
|
|
|
|
The Visual Studio says " ~Control does not contain a definition for Children"!?
|
|
|
|
|
Replace Children with Controls . I just knocked this snippet up in the CP editor.
|
|
|
|
|
Mission Done! Thanks a lot
|
|
|
|
|
No problem. The key to this trick is the fact that your control could be nested inside any one of a number of control containers - this recursive method is a handy way to iterate over a form and find ALL instances of a particular type.
|
|
|
|
|
It's Controls[^], not Children, in .Net. But yes, this is roughly what I just posted.
|
|
|
|
|
Thanks - I just knocked this up in the CP editor.
|
|
|
|
|
Okay, I've read this whole thread and you're horribly confused about classes and instances. Your user name indicates you come from non-class-based procedural languages, and perhaps you need to keep the Object Oriented chapter of your C# book open while you learn!
The failed code that you are trying here is trying to find a type, and create a new instance of that type. Activator.CreateInstance(sometype) is logically equivalent to new sometype(), and in fact the snippet you posted can be written exactly as
PictureBox picture = new pictureBox99.BackgroundImage();
picture.BackgroundImage = Resource1.island;
Hopefully you can see why that doesn't work!
You are not trying to create a copy of something whose final type is not known at compile time, which is about the only time I've needed to use reflection based instantiation. No, what you want to do is find all the instances and do the same thing to them:
foreach(Control c in Controls){
PictureBox picture = c as PictureBox;
if(picture != null)
picture.BackgroundImage = Resource1.island;
}
or with Linq:
this.Controls.Select(c => c as PictureBox).Where(c => c != null).ToList().ForEach(pb => pb.BackgroundImage = Resource1.island);
(the ToList being necessary because ForEach isn't defined except on Lists).
|
|
|
|
|
look here:
private void ResetPictureBoxes()
{
pictureBox9.BackgroundImage = Resource1.sea;
pictureBox40.BackgroundImage = Resource1.sea;
pictureBox1.BackgroundImage = Resource1.sea;
pictureBox100.BackgroundImage = Resource1.sea;
pictureBox101.BackgroundImage = Resource1.sea;
pictureBox102.BackgroundImage = Resource1.sea;
pictureBox103.BackgroundImage = Resource1.sea;
pictureBox104.BackgroundImage = Resource1.sea;
pictureBox105.BackgroundImage = Resource1.sea;
pictureBox13.BackgroundImage = Resource1.sea;
pictureBox14.BackgroundImage = Resource1.sea;
pictureBox15.BackgroundImage = Resource1.sea;
pictureBox16.BackgroundImage = Resource1.sea;
pictureBox17.BackgroundImage = Resource1.sea;
pictureBox18.BackgroundImage = Resource1.sea;
pictureBox19.BackgroundImage = Resource1.sea;
pictureBox2.BackgroundImage = Resource1.sea;
pictureBox20.BackgroundImage = Resource1.sea;
pictureBox21.BackgroundImage = Resource1.sea;
}
I try your code snippet, too but it didn't work.
private void ResetPictureBoxes()
{
foreach (Control c in this.Controls)
{
PictureBox picture = c as PictureBox;
if (picture != null)
picture.BackgroundImage = Resource1.island;
}
}
|
|
|
|
|
Define 'didn't work'.
Are all your picture boxes children of the same control? If so, you should use 'parentOfPBs.Controls' not 'this.Controls'. If not, you will have to traverse the whole control tree; I suggest you do this once and stash them in a list as it can be quite slow on a big form. You can use a standard recursive tree traversal similar to
public static List<T> GetControlsInTree<T>(Control parent) where T: Control {
List<T> result = new List<T>
AddControlsInTree(parent, result);
return result;
}
static void AddControlsInTree<T>(Control parent, List<T> list) where T: Control {
foreach (Control c in parent.Controls){
T t = c as T;
if (t != null) list.Add(t);
AddControlsInTree(c, list);
}
}
You can stash the list of picture boxes after form initialisation (after you call InitializeComponent in the constructor, for example):
List<PictureBox> pictures = GetControlsInTree<PictureBox>(this);
Then you can iterate over them at will:
private void ResetPictureBoxes()
foreach(PictureBox picture in pictures)
picture.BackgroundImage = Resource1.island;
}
|
|
|
|
|
Thanks
|
|
|
|
|
Anybody have a better date time parse algorithm? I've tried DateTime.TryParse, but it doesn't pick up a lot of conditions. For example 1:05P != 1:05PM because it doesn't know what P is.
|
|
|
|
|
I know it's not C#, but take a look at date.js[^]
It's open source, so if you like what you can do in the sandbox on that page, maybe you could hack it to c#. Or join the project.
Cheers,
Peter
Software rusts. Simon Stephenson, ca 1994. So does this signature. me, 2012
|
|
|
|
|
Define 'better'. Date parsing in particular suffers from ambiguity if you try to be too vague (i.e. what date is 6-3? what about 6-3-12?). It's generally better to mandate a format and make users enter in that format.
|
|
|
|
|
Values can have up to 1000.
How to represent numbers up to 1000 within the 2 alphanumeric placeholder?
Eg., 100, 976, 1000 should all be converted to 2 alphanumeric placeholders.
There will be a C# class that converts the values back and forth.
Has anyone had any need to do this in the past?
You can never try. You either do it or you don't.
|
|
|
|
|
No - but it isn't at all difficult.
If you allow A-Z and 0-9 only, you can fit 36 x 36 items in two characters - 1296 different values.
So divide your number into two parts : integer divide by 36, and modulo 36 and use them as an offset into a character wide translation table.
Reassembly is the reversal of disassembly.
[edit]Phrasing change - the original sounded like the table should be two character wide - OriginalGriff[/edit]
Ideological Purity is no substitute for being able to stick your thumb down a pipe to stop the water
|
|
|
|
|
Thank you...
I just came across this. Works like magic!
public String ConvertToBase(int num, int nbase)
{
String chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
if (nbase < 2 || nbase > chars.Length)
return "";
int r;
String newNumber = "";
while (num >= nbase)
{
r = num % nbase;
newNumber = chars[r] + newNumber;
num = num / nbase;
}
newNumber = chars[num] + newNumber;
return newNumber;
}
You can never try. You either do it or you don't.
|
|
|
|
|
A StringBuilder might be a better overall technique if you need more digits in the future.
|
|
|
|
|
You could simplify that quite a bit, assuming you don't plan on increasing the field size later...
const string chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
static readonly int num_chars = chars.Length;
static string NumberToCode(int number)
{
if (number < 0 || number > num_chars * num_chars)
throw new ArgumentOutOfRangeException();
return chars[number / num_chars].ToString() + chars[number % num_chars];
}
|
|
|
|
|
You only need base 32, so you can throw some letters out. In case there are some letters that you just don't like or something like that.
As a bonus, the math is easier for 32 (not that it matters in this case..)
|
|
|
|
|
In practice, I would probably throw out "O" and "I", possibly "Z" as well just to make it clearer to people reading it.
But then, I try to avoid this kind of thing anyway!
Ideological Purity is no substitute for being able to stick your thumb down a pipe to stop the water
|
|
|
|
|
If you convert your numeric values to base 36 you can display the values using two characters.
BDF
I often make very large prints from unexposed film, and every one of them turns out to be a picture of myself as I once dreamed I would be.
-- BillWoodruff
|
|
|
|