CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 23
  1. #1
    Join Date
    Sep 2009
    Posts
    53

    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

  2. #2
    Join Date
    Sep 2009
    Posts
    53

    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.

  3. #3
    Join Date
    Dec 2009
    Location
    Kathmandu,Nepal
    Posts
    168

    Re: Feels like too many If's!

    What do You want to think of doing here please explain us better...

  4. #4
    Join Date
    Aug 2008
    Posts
    112

    Re: Feels like too many If's!

    The first, second third..... even the last will not work at all
    Microfone:>>>> please try a dynamic collection of pictures to see what is going to happen -

    Oppp: whispering: is this feature allowable in C# ? :
    hi,,,

  5. #5
    Join Date
    Oct 2008
    Location
    Cologne, Germany
    Posts
    756

    Re: Feels like too many If's!

    Quote Originally Posted by demise View Post
    I check the coolour of each picture box via the colour whether or not a number is stored into an int array.
    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;
    	}
    }
    win7 x86, VS 2008 & 2010, C++/CLI, C#, .NET 3.5 & 4.0, VB.NET, VBA... WPF is comming

    remeber to give feedback you think my response deserves recognition? perhaps you may want to click the Rate this post link/button and add to my reputation

    private lessons are not an option so please don't ask for help in private, I won't replay

    if you use Opera and you'd like to have the tab-button functionality for the texteditor take a look at my Opera Tab-UserScirpt; and if you know how to stop firefox from jumping to the next control when you hit tab let me know

  6. #6
    Join Date
    Sep 2009
    Posts
    53

    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.

  7. #7
    Join Date
    Oct 2008
    Location
    Cologne, Germany
    Posts
    756

    Re: Feels like too many If's!

    Quote Originally Posted by demise View Post
    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 View Post
    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) ?
    win7 x86, VS 2008 & 2010, C++/CLI, C#, .NET 3.5 & 4.0, VB.NET, VBA... WPF is comming

    remeber to give feedback you think my response deserves recognition? perhaps you may want to click the Rate this post link/button and add to my reputation

    private lessons are not an option so please don't ask for help in private, I won't replay

    if you use Opera and you'd like to have the tab-button functionality for the texteditor take a look at my Opera Tab-UserScirpt; and if you know how to stop firefox from jumping to the next control when you hit tab let me know

  8. #8
    Join Date
    Sep 2009
    Posts
    53

    Re: Feels like too many If's!

    Quote Originally Posted by memeloo View Post
    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 View Post
    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).

  9. #9
    Join Date
    Oct 2008
    Location
    Cologne, Germany
    Posts
    756

    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
    win7 x86, VS 2008 & 2010, C++/CLI, C#, .NET 3.5 & 4.0, VB.NET, VBA... WPF is comming

    remeber to give feedback you think my response deserves recognition? perhaps you may want to click the Rate this post link/button and add to my reputation

    private lessons are not an option so please don't ask for help in private, I won't replay

    if you use Opera and you'd like to have the tab-button functionality for the texteditor take a look at my Opera Tab-UserScirpt; and if you know how to stop firefox from jumping to the next control when you hit tab let me know

  10. #10
    Join Date
    Sep 2009
    Posts
    53

    Re: Feels like too many If's!

    That seems like a good idea I will give that a go. Thank you very much.

  11. #11
    Join Date
    Sep 2009
    Posts
    53

    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?)

  12. #12
    Join Date
    Nov 2002
    Location
    .NET 3.5 VS2008
    Posts
    1,039

    Re: Feels like too many If's!

    Quote Originally Posted by demise View Post
    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).

  13. #13
    Join Date
    Oct 2008
    Location
    Cologne, Germany
    Posts
    756

    Re: Feels like too many If's!

    Quote Originally Posted by nelo View Post
    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 View Post
    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
    win7 x86, VS 2008 & 2010, C++/CLI, C#, .NET 3.5 & 4.0, VB.NET, VBA... WPF is comming

    remeber to give feedback you think my response deserves recognition? perhaps you may want to click the Rate this post link/button and add to my reputation

    private lessons are not an option so please don't ask for help in private, I won't replay

    if you use Opera and you'd like to have the tab-button functionality for the texteditor take a look at my Opera Tab-UserScirpt; and if you know how to stop firefox from jumping to the next control when you hit tab let me know

  14. #14
    Join Date
    Sep 2009
    Posts
    53

    Re: Feels like too many If's!

    I have a slight problem.. (again ).

    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.

  15. #15
    Join Date
    Oct 2008
    Location
    Cologne, Germany
    Posts
    756

    Re: Feels like too many If's!

    Quote Originally Posted by demise View Post
    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 View Post
    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.,
    win7 x86, VS 2008 & 2010, C++/CLI, C#, .NET 3.5 & 4.0, VB.NET, VBA... WPF is comming

    remeber to give feedback you think my response deserves recognition? perhaps you may want to click the Rate this post link/button and add to my reputation

    private lessons are not an option so please don't ask for help in private, I won't replay

    if you use Opera and you'd like to have the tab-button functionality for the texteditor take a look at my Opera Tab-UserScirpt; and if you know how to stop firefox from jumping to the next control when you hit tab let me know

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured