Hi all,

I am trying to do an action by clicking a button.

So if I press the button 1, it will do action1 (showing an image), and if I press it again, it should go do action 2 (which is reverting action one, so disable the image), press again for the tird time, it will go back and do action1, and so on.. I tried to use e.ClickCount , but ClickCount is not on WPF. How should I implement it in the block?

 private void button1_Click(object sender, RoutedEventArgs e)
        {
         /*I already manage to do an action (call an image) here by clicking the button once, but after the second , third, and so on, nothing happened. I need to revert an action if I press the same button again, and call the action1 again if it pressed again.*/
        }

Thank you for the guidance

Edited 3 Years Ago by mike_2000_17: Fixed formatting

You could check the image state on each click and do the appropriate action depending on that. A kind of if shown, hide or if hidden, show.

Or use a boolean flag.
If it is true show picture and set the flag to false.
If it is false, hide picture and set the flag to true.

I tried this below, but still if I pressed it once, it will show the image, and if press it again, still the image is there. Maybe first help me figuring out whether this logic is somehow correct or not (using the boolean)
To be honest, I am quite confused, how does it relates between "if i press the button once, go to action A, and if i press it again, go to action B, and again , to action A, and so on" and the boolean function?
Please guide.

Thank you

{
            bool i = true;
            //1 click show image,2nd click hide image
            if (i=true)
            {
           string imageFile = "image1.jpg";
           string basePath = AppDomain.CurrentDomain.BaseDirectory + "media\\";
           try
           {
            //path from image
            image1.Source = new BitmapImage(new Uri(basePath + imageFile));
           }
           catch
           {
               MessageBox.Show("Not Found!");
           }

            }
//the else is for pressing the 2nd time, 4th time, 6th time, and so on. I put an empty value for imageFile, hoping that it will not show anything (as if hiding)?
            else
            {
           string imageFile = "";
           string basePath = AppDomain.CurrentDomain.BaseDirectory + "media\\";
            }

Edited 6 Years Ago by reza.adinata: n/a

I'm very happy you solved this one on your own, by doing some thinking!
You may be proud of yourself!
Happy programming! :)

This question has already been answered. Start a new discussion instead.