Feels like too many If's!
I am developing an app for a mobile device and it feels like I am using way too many If / else statements.
The scenario is I have a number of picture boxes which change colour on being clicked in. I check the coolour of each picture box via the colour whether or not a number is stored into an int array.
i.e:
for (int i = 0; i < 6; i++)
{
if (PictureBox1.BackColor == Color.Red)
{
Number[i] = 1;
}
else if (PictureBox2.BackColor == Color.Red)
{
Number[i] = 2;
}
else if (PictureBox3.BackColor == Color.Red)
{
Number[i] = 3;
}
else if (PictureBox4.BackColor == Color.Red)
{
Number[i] = 4;
}
}
Is there any other way I could go about this? or is this my only bet?
Thanks.
P.S - the if list is a lot bigger than what is pasted here :)
Re: Feels like too many If's!
P.S - I just noticed my code will not work properly, and simply flood the array with the first value it goes too.. hehe oops.. either way the question still remains. Thanks.
Re: Feels like too many If's!
What do You want to think of doing here please explain us better...
Re: Feels like too many If's!
The first, second third..... even the last will not work at all :( :p
Microfone:>>>> please try a dynamic collection of pictures to see what is going to happen -
Oppp: whispering: is this feature allowable in C# ? :(:
Re: Feels like too many If's!
Quote:
Originally Posted by
demise
I check the coolour of each picture box via the colour whether or not a number is stored into an int array.
:ehh: do you really understand your code?
I have no idea what you are going to do. to me it looks like that you are setting the value in the "Number" array accordingly to the BackColor of you picture boxes.
@demise: is it really so hard to use the code tags?
Code:
for (int i = 0; i < 6; i++)
{
if (PictureBox1.BackColor == Color.Red)
{
Number[i] = 1;
}
else if (PictureBox2.BackColor == Color.Red)
{
Number[i] = 2;
}
else if (PictureBox3.BackColor == Color.Red)
{
Number[i] = 3;
}
else if (PictureBox4.BackColor == Color.Red)
{
Number[i] = 4;
}
}
Re: Feels like too many If's!
The code does the following:
There is a number of picture boxes, when some are selected they will change their back colour to red. Then I need to know the positions of which were changed to red. So the following code would look to see if seperately each picturebox does have the red colour. If it does, it stores it's number (position) into the number array.
Note: Yes, I know this does not work, I posted it very late. It does work however when I amend it as so:
Code:
for (int i = 0; i < 6; i++)
{
if (PictureBox1.BackColor == Color.Red)
{
Number[i] = 1;
PictureBox1.BackColor = Color.Black;
}
else if (PictureBox2.BackColor == Color.Red)
{
Number[i] = 2;
PictureBox2.BackColor = Color.Black;
}
else if (PictureBox3.BackColor == Color.Red)
{
Number[i] = 3;
PictureBox3.BackColor = Color.Black;
}
else if (PictureBox4.BackColor == Color.Red)
{
Number[i] = 4;
PictureBox4.BackColor = Color.Black;
}
}
I just feel the method is messy.. hence asking for a better solution and advice in any form.
Thanks.
Re: Feels like too many If's!
Quote:
Originally Posted by
demise
There is a number of picture boxes, when some are selected they will change their back colour to red.
ok, so you handle some event to change the back color to red?
Quote:
Originally Posted by
demise
Then I need to know the positions of which were changed to red.
I don't understand this part. do you just want to know which picture box is selected (red) ?
Re: Feels like too many If's!
Quote:
Originally Posted by
memeloo
ok, so you handle some event to change the back color to red?
Yes, there is a click event, which will change the backcolour to red if it is black. Or black if it is red.
Quote:
Originally Posted by
memeloo
I don't understand this part. do you just want to know which picture box is selected (red) ?
Yes, I want to know which picture boxes are selected. For example:
pb1 pb2 pb3
pb4 pb5 pb6
Assuming the above are picture boxes.
If pb1 and pb6 were red, I would want to remember these picture boxes were red for a future use. Therefore I want to create the number array and store which are red. In this case, pb1 and pb6 (1 and 6).
Re: Feels like too many If's!
here's my suggestion:
Code:
// create a helper list
HashSet<PictureBox> selectedPictureBoxes = new HashSet<PictureBox>();
// and update it in your selected event handler
void your_picturebox_selected_eventhandler(...)
{
if(PictureBox1.BackColor == Color.Red)
selectedPictureBoxes.Add(PictureBox1)
else
selectedPictureBoxes.Remove(PictureBox1);
}
then you can use it somewhere else with the "for each" loop
Re: Feels like too many If's!
That seems like a good idea :) I will give that a go. Thank you very much.
Re: Feels like too many If's!
I have just tried to find the HashSet in its namespace using System.Collections; but it seems to only find HashTable. (I dont know if HashSet is part of the .Net Compact Framework?)
Re: Feels like too many If's!
Quote:
Originally Posted by
demise
I have just tried to find the HashSet in its namespace using System.Collections; but it seems to only find HashTable. (I dont know if HashSet is part of the .Net Compact Framework?)
Hi there.
The class is defined in System.Collections.Generic. I've not come across it before so I'll have to look into it. It appears to be new in .NET 3.5. It doesn't seem to be supported in the Compact Framework though. At least it's not stated on MSDN (HashSet of T).
Re: Feels like too many If's!
Quote:
Originally Posted by
nelo
I've not come across it before so I'll have to look into it.
it's very usefull if you have to do merge and difference etc. operations on collections.
Quote:
Originally Posted by
nelo
It appears to be new in .NET 3.5. It doesn't seem to be supported in the Compact Framework though. At least it's not stated on MSDN (
HashSet of T).
oops, I forgot that this is a mobile device with the compact version of the framework. then you have to use some other collection to store the selected picture boxes but the concept stays the same ;)
Re: Feels like too many If's!
I have a slight problem.. (again :p).
I implemented an ArrayList to hold the picture boxes.
However.. when the form closes and reopen later on. I try to restore the picture boxes colour via:
Code:
foreach (PictureBox pbox in ptemp)
{
pbox.BackColor = Color.Red;
}
ptemp being the ArrayList retrieved from another class.
I recieve the error message: Object Disposed Exception was unhandled.
Am I right to assume once the form closed, the ArrayList no longer points to existing picture boxes and therefore cannot be used? How can I go about this?
Thanks.
Re: Feels like too many If's!
Quote:
Originally Posted by
demise
Am I right to assume once the form closed, the ArrayList no longer points to existing picture boxes and therefore cannot be used?
that's right.
Quote:
Originally Posted by
demise
How can I go about this?
you'll have to recognize them by names for example: PictureBox.Name and you'll have to store their names in the array instead of object references.,