Solved

C# WinForms: My picturebox image will not update?

Posted on 2009-05-20
9
4,228 Views
Last Modified: 2013-12-17
C# Windows form. Picture box not updating.

I basically built a timer with gif numbers. I can set the pictureboxes from a buttonclick or from load events but when I try in a method the picturebox never updates? I am so confused.

Code is below.

Thanks,
KarlS2010
public partial class frmCountdown : Form
    {
        public frmCountdown()
        {
            InitializeComponent();
            picDay1.Image = Image.FromFile("images/4c.gif"); // WORKS GREAT!
        }
 
       public void Countdown()
        {
            //Start counting down from start to finish
            for (int i = 0; i < 3; i++)
            {
                this.picDay1.Image = Image.FromFile("images/4c.gif"); // WORKS FROM INIT BUT NOT HERE!?
                this.Refresh(); // NO HELP HERE?! Tried everything with and without this.
            }
            this.Refresh(); // NOPE
        }

Open in new window

0
Comment
Question by:KarlS2010
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
9 Comments
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 24435069
It's probably going by so fast that you can't even see it changing.

...plus, are you wanting to change the filename with the loop?

Something like:

        public void Countdown()
        {
            //Start counting down from start to finish
            for (int i = 0; i < 3; i++)
            {
                this.picDay1.Image = Image.FromFile("images/" + i.ToString() + "c.gif"); // <-- change image via "i"
                Application.DoEvents();  // allow the picturebox to refresh itself before continuing
                System.Threading.Thread.Sleep(1000); // one second delay to allow user to see it before it changes
            }
        }

Open in new window

0
 
LVL 9

Expert Comment

by:tculler
ID: 24435815
If the above isn't what you're thinking, the Invalidate() method may be what you're looking for. It basically forces a re-draw of the Control. Refresh seems to be stingy with me, so I use that when it comes to forcing a re-draw.
0
 

Author Comment

by:KarlS2010
ID: 24436452
Do not worry about the loop it is sjust there in fact remove it i your mind.
I also tried this with no luck:

       public void Countdown()
        {
            this.picDay1.Image = Image.FromFile("images/4c.gif"); // WORKS FROM INIT BUT NOT HERE!?
            this.Invalidate();    
            // this.Refresh(); // NO HELP HERE?!Karls2010
0
How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

 

Author Comment

by:KarlS2010
ID: 24436599
The only thing I do i call another form and then that form sends control back to the main form and then it will not update. Here is the complete code (driving me nuts!) I am sure it is simple but doh!
namespace Countdown
{
    public partial class frmCountdown : Form
    {
        public frmCountdown()
        {
            InitializeComponent();
            
        }
 
        // SETUP VARIABLES
        
        private void pictureBox15_Click(object sender, EventArgs e)
        {
            // SHOW THE OPTIONS FORM
            frmOptions frmOptionsDialog = new frmOptions();
            frmOptionsDialog.Show();
        }
 
        private void picShowElapsed_Click(object sender, EventArgs e)
        {
            // SHOW STATUS FORM
            frmStatus frmStatusDialog = new frmStatus();
            frmStatusDialog.Show();            
        }
 
        public void Countdown()
        {
            //Start counting down from start to finish
            this.picSec2.Image = Image.FromFile("images/2c.gif");
            picSec3.Image = Image.FromFile("images/1c.gif"); // ******************
            DoIt();
        }
 
        public void DoIt()
        {
            picSec3.Image = Image.FromFile("images/1c.gif"); // ******************\
        }
        
    }
}

Open in new window

0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 24436852
"..and then that form sends control back to the main form..."

How are you giving control "back" to the main form?...I suspect that is where you are going wrong.
0
 

Author Comment

by:KarlS2010
ID: 24436885
errr... on the options form I just do a this.Hide(); and then ...

            frmCountdown Actor = new frmCountdown(); // Create an object!
            Actor.picDay1.Image = Image.FromFile("images/4c.gif"); // NOPE WONT CHANGE IT HERE EITHER
            Actor.Countdown(); // this is where I call the method on the main form that should change the image
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 24436968
...and there is the problem!

In this line:

    frmCountdown Actor = new frmCountdown(); // Create an object!

You are creating ANOTHER instance of frmCountdown and changing the image on that one...this instnace is actually invisible since it is never even shown.

What you need is a reference to the ALREADY OPEN instance of frmCountdown.

Explain to me the "flow" of the app and I can help you with a good solution.

One big question I have is does frmOptions itself have to make the change?...or should frmCountdown be making the change in response to an event/condition on or received from frmOptions?
0
 

Author Comment

by:KarlS2010
ID: 24438043
Thanks Idle -

Basically I have a countdown screen with an option button - someone clicks options and sets the start and stop dates and then clicks save (or whatever) and the form should start counting. I had trouble with static ad instance variables so I was trying to find a simple way to call Coutdown() (on the main form)  from my options form. I thought making an Actor object would work but you are prob right!

Do you need more what can I do to  make a a reference to the ALREADY OPEN instance of frmCountdown?

Thanks!
KarlS2010
0
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 500 total points
ID: 24438617
What you can do is pass in the "main" form when you Show() the "options" form like this:

    frmOptions frmOptionsDialog = new frmOptions();
    frmOptionsDialog.Show(this); // <--- passing the main form to Show()

Then, in frmOptions, you can cast the "Owner" property back to the main form type:

    frmCountdown fcd = (frmCountdown)this.Owner;
    fcd.picDay1.Image = Image.FromFile("images/4c.gif");
    fcd.Countdown();

*You may have to change the Modifiers() property for "picDay1" to public.
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Upgrading my SSIS package in VS 2012 6 84
Visual Studio hangs on running project 6 57
Exception when using Alea null reference in fsharp.core.dll 3 52
IDE for Python 5 64
Summary Displaying images in RichTextBox is a common requirement with limited solutions available. Pasting through clipboard or embedding into RTF content only support static images.  This article describes how to insert Windows control objects int…
In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

740 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question