Friday, January 21, 2011

Whats the best way to do throbber in C#?

Specifically what I am looking to do is make the icons for the Nodes in my System.Windows.Forms.TreeView control to throb while a long loading operation is taking place.

  • If you load each frame into an ImageList, you can use a loop to update to each frame. Example:

        bool runThrobber = true;
        private void AnimateThrobber(TreeNode animatedNode)
        {
            BackgroundWorker bg = new BackgroundWorker();
            bg.DoWork += new DoWorkEventHandler(delegate
            {
                while (runThrobber)
                {
                    this.Invoke((MethodInvoker)delegate
                    {
                        animatedNode.SelectedImageIndex++;
                        if (animatedNode.SelectedImageIndex >= imageList1.Images.Count) > animatedNode.SelectedImageIndex = 0;
                    });
                    Thread.Sleep(100);
                }
            });
            bg.RunWorkerAsync();
        }
    

    Obviously there's more than a few ways to implement this, but here's the basic idea.

    Factor Mystic : Looking at this again, you should really check and see if the image index is in the bounds of the imagelist.images count before you increment it.

0 comments:

Post a Comment