Toggling Picture Box visibility C#
NickName:Nishant Ask DateTime:2011-05-22T04:47:10

Toggling Picture Box visibility C#

Why is the picture box control's visibility property not working here. I have initially set them to false, so that when the screen loads they are not visible. But then I wish to toggle this. I have done the following but does not seem to work. This is a windows forms application.

private void Action()
    {
        while (true)
        {

            Random r1 = new Random();
            int num = r1.Next(1,3);

            switch (num)
            {
                case 1:
                    pictureBoxLeft.Visible = true;
                    pictureBoxRight.Visible = true;
                    break;
                case 2:
                    pictureBoxLeft.Visible = true;
                    pictureBoxRight.Visible = false;
                    break;
                case 3:
                    pictureBoxLeft.Visible = false;
                    pictureBoxRight.Visible = true;
                    break;

            }

            Thread.Sleep(200);
            pictureBoxLeft.Visible = false;
            pictureBoxRight.Visible = false;
            Thread.Sleep(500);
        }
    }

Also to add, this is working properly with a text box!!! Any Ideas???

Many thanks in advance

Copyright Notice:Content Author:「Nishant」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/6084367/toggling-picture-box-visibility-c-sharp

Answers
Guffa 2011-05-21T21:19:30

Setting the Visible property to true doesn't show the control, it only creates a message that will show it. As long as you are keeping the main thread busy with a Sleep, it will not process any messages, and the controls won't be shown.\n\nYou should show the picture boxes and then set up a timer with code that will hide the picture boxes when the timer's tick event is triggered. Then you exit your method so that the main thread can handle the messages that will show the picture boxes.",


Haris Hasan 2011-05-21T20:55:29

This is your UI thread. UI thread is so busy that it is not getting time to refresh the display. UI thread is busy in endless while loop, so how can it update UI?",


Mario 2011-05-21T21:21:11

In addition to Thread.Sleep(); call Application.DoEvents() to trigger event handling/ui updating. However ensure you're not calling Action() again somehow. A simple Thread.Sleep() won't allow the UI to update as you're still in the handler and the framework won't be able fire any new events on its own as it won't know if you're finished when waiting (due to the thread still being busy).\n\nOverall your approach might be a bad idea (depending on other code etc.). Consider using timers or a background worker instead.",


More about “Toggling Picture Box visibility C#” related questions

Toggling Picture Box visibility C#

Why is the picture box control's visibility property not working here. I have initially set them to false, so that when the screen loads they are not visible. But then I wish to toggle this. I have...

Show Detail

Visibility toggling

In my XAML, there's one button and one label. At launch, the button is visible and the label is not. When clicking the install_btn, the label should becomes visible instead of the button. Here's a ...

Show Detail

Toggling classes or visibility

I'm using this code for toggling visibility of a class, but once hidden, elements are not becoming visible again. Look at my demo. Click on "1", and then click on "All" for hidinh and showing all i...

Show Detail

Toggling visibility of divs using javascript

I have the following code, for enabling a div from toggling between visible and hidden: function toggle_visibility(id,$this) { var e = document.getElementById(id); if(e.style.display == 'bloc...

Show Detail

Picture box On Picture Box

This is in regards to C# coding, im quite new to this programming language, and do not know much about any other as well, but what i would like to achieve is to have a picture box as the background...

Show Detail

What is an elegant way of toggling the visibility of UI sections?

I build Android apps using the MVP pattern and I'm often breaking up my UI into various sections like this: <ConstraintLayout android:id="@+id/recyclerSection" <ConstraintLayout ...

Show Detail

Toggling visibility of multiple texts is not working

I am trying to toggle between a couple of texts, where the only one that shows is the one that was "turn on" most recently. For example, the default would look something like this: Click A Click B...

Show Detail

Toggling the window visibility instantiates new window

I have a modularized application with Prism. A simple window (shell) is displayed. The shell contains a taskbar icon that invokes a command to toggle windows visibility. Clicking the TaskbarIcon cr...

Show Detail

CSS : picture instead of backface-visibility = hidden

I am trying to make 3D picture tables with three.js, and all is working fine, however, I would be able to have two pictures back-to-back with only 1 div : in my element, I have a background, and I...

Show Detail

Change the picture in picture box in C#

I am not sure what is my mistake but i am telling you it works before. I want to change the picture in picture box based on the user's choice. The 1st picture i put is in the picture control box. ...

Show Detail