Click here to Skip to main content
15,886,080 members
Please Sign up or sign in to vote.
1.44/5 (2 votes)
See more:
hi,
i have this function that doesnt seem to work right,
i want to make a shape change the colour when i click on a button,
i click the first time ,the colour has to stay pink until i click another time so it will change to black.

but after the first click the program stop responding at all,and i can't click any more.

C#
int on=0;
private void start_Click(object sender, EventArgs e)
        {
            if (on == 0)
                on = 1;
            else
                on = 0;
            //MessageBox.Show(on.ToString());
            while (on==1)
            {
                ind1.FillColor = System.Drawing.Color.Pink;
            }
            ind1.FillColor = System.Drawing.Color.Black;
            //colour_adjustement("10.175.22.234");
        }

thank you for your help
Posted

while (on==1)
{
     ind1.FillColor = System.Drawing.Color.Pink;
}

This is a very bad idea.
You have put yourself into an infinite loop and thus the entire program hangs.

I guess you want to use an if condition instead of a while loop.
 
Share this answer
 
Comments
fjdiewornncalwe 20-Mar-13 16:37pm    
+5. Absolutely.
Abhinav S 20-Mar-13 22:57pm    
Thank you.
Member 9870058 20-Mar-13 17:26pm    
the problem is that's not the program that i want to realise,it's just a test.
Because in my program i would need a loop,the actions according to the test have to be repeated until the user click on start again.
i want to get a value every 5 sec and according to it change the colour of a button.
thanks for replying
Abhinav S 20-Mar-13 22:58pm    
Then use animations if you are using WPF or a Timer with a 5 sec interval to flash a color change.
While Loop is not the solution for this problem.
Member 9870058 21-Mar-13 10:15am    
could you please clearify this to me.Because i am a real begginner in this type of develloping.
thanks
You don't need a while loop here.

C#
int on=0;

private void start_Click(object sender, EventArgs e)
{
   if (on == 0)
   {
      on = 1;
      ind1.FillColor = System.Drawing.Color.Black;
   }
   else
   {
      on = 0;
      //MessageBox.Show(on.ToString());
      ind1.FillColor = System.Drawing.Color.Pink;
   }
}


Why do you want to use a while loop here ?
 
Share this answer
 
Comments
Member 9870058 20-Mar-13 17:26pm    
the problem is that's not the program that i want to realise,it's just a test.
Because in my program i would need a loop,the actions according to the test have to be repeated until the user click on start again.
i want to get a value every 5 sec and according to it change the colour of a button.
thanks for replying
You're getting an infinite loop.

C#
while (on==1)
{
        ind1.FillColor = System.Drawing.Color.Pink;
        //your program stays in a loop here.
}


Your code is very ugly, try this:
C#
bool on=false;

private void start_Click(object sender, EventArgs e)
{
            on = !on;
            ind1.FillColor = 
                 (on ? System.Drawing.Color.Pink : System.Drawing.Color.Black);
            //colour_adjustement("10.175.22.234");
}


Good luck.
 
Share this answer
 
Comments
Member 9870058 20-Mar-13 17:25pm    
the problem is that's not the program that i want to realise,it's just a test.
Because in my program i would need a loop,the actions according to the test have to be repeated until the user click on start again.
i want to get a value every 5 sec and according to it change the colour of a button.
thanks for replying
José Amílcar Casimiro 20-Mar-13 17:32pm    
Use a Timer object to launch the task every 5 seconds.
MuhammadUSman1 21-Mar-13 0:04am    
Dear [José Amílcar Ferreira Casimiro] i have posted one question on CP i Need Your Help about my Question.
Question Link is

http://www.codeproject.com/Questions/564735/Openpluswebpluspage-2cplusloginplusandplusthenplus

I know it is not right place to ask something but. I like your way of coding. So if possible then please Help.

Thanks.
José Amílcar Casimiro 21-Mar-13 5:53am    
I can see that your problem is solved. keep coding.
Member 9870058 21-Mar-13 10:18am    
i look at that,thank you very much for your help.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900