10 Replies - 8651 Views - Last Post: 27 May 2009 - 08:32 PM Rate Topic: -----

#1 papuccino1   User is offline

  • His name was Robert Paulson.
  • member icon

Reputation: 63
  • View blog
  • Posts: 1,121
  • Joined: 02-March 08

Trouble using the ProgressBar control.

Post icon  Posted 25 May 2009 - 07:30 PM

Hey there guys. You seem to always help me out when I'm in need, so here goes. :P

I have a method:
public void Example()
{
     MyWebClient.DownloadData(http://www.example.com);
     //Do some wierd mumbo-jumbo here.
     //already got it working how i want.
}



Now, all this mumbo jumbo takes approx 4 seconds on my PC. I had to refactor (is that what it's called?) my code because it was working too slowly for me. So from 7-8 seconds, shaved down to 4, not bad for an amateur.

Point is, during this 4 second search (which is HIGHLY dependent on the users internet speed) my entire GUI freezes. I think I know how to correct this, but that's not my question.

My question is, how can I update my progress bar so it looks more snazzy and gives some funny info to the users, e.g. "Summoning Old Ones." "Retrieving broken companion cube", etc.

The most important part of my question: Is it possible for me to update my progress bar according to each line of code in my methods?

Is This A Good Question/Topic? 0
  • +

Replies To: Trouble using the ProgressBar control.

#2 SixOfEleven   User is offline

  • Planeswalker
  • member icon

Reputation: 1055
  • View blog
  • Posts: 6,643
  • Joined: 18-October 08

Re: Trouble using the ProgressBar control.

Posted 25 May 2009 - 07:50 PM

I have an idea on how you can make a neat little progress bar. Give me a minute to give it a try.
Was This Post Helpful? 0
  • +
  • -

#3 papuccino1   User is offline

  • His name was Robert Paulson.
  • member icon

Reputation: 63
  • View blog
  • Posts: 1,121
  • Joined: 02-March 08

Re: Trouble using the ProgressBar control.

Posted 25 May 2009 - 08:18 PM

Jamie you're a saint. I'll check it out in the morning. I have a big day tomorrow. :D
Was This Post Helpful? 0
  • +
  • -

#4 SixOfEleven   User is offline

  • Planeswalker
  • member icon

Reputation: 1055
  • View blog
  • Posts: 6,643
  • Joined: 18-October 08

Re: Trouble using the ProgressBar control.

Posted 25 May 2009 - 09:27 PM

View Postpapuccino1, on 25 May, 2009 - 08:30 PM, said:

My question is, how can I update my progress bar so it looks more snazzy and gives some funny info to the users, e.g. "Summoning Old Ones." "Retrieving broken companion cube", etc.

The most important part of my question: Is it possible for me to update my progress bar according to each line of code in my methods?


I was working on something. Then I remembered, there is a tutorial in the C# section about adding the percent into the progress bar. If you don't like that, I have a sort of custom control you might like. Here's a screen shot:

Attached Image

I made the control using a picture box and a timer. The possibilities of what you can do with it are endless. If you are interested leave a note and I will try and get back to you. Unfortunately, my mother had a stroke and a heart attack Saturday night. They don't expect her to make it through the week. I will be going to stay with my dad for a while. Don't know when I will be able to get back to you.
Was This Post Helpful? 0
  • +
  • -

#5 papuccino1   User is offline

  • His name was Robert Paulson.
  • member icon

Reputation: 63
  • View blog
  • Posts: 1,121
  • Joined: 02-March 08

Re: Trouble using the ProgressBar control.

Posted 26 May 2009 - 04:22 AM

That's horrible horrible news Jamie. :(
I feel bad asking for help during this time.
Was This Post Helpful? 0
  • +
  • -

#6 SixOfEleven   User is offline

  • Planeswalker
  • member icon

Reputation: 1055
  • View blog
  • Posts: 6,643
  • Joined: 18-October 08

Re: Trouble using the ProgressBar control.

Posted 26 May 2009 - 04:58 AM

View Postpapuccino1, on 26 May, 2009 - 05:22 AM, said:

That's horrible horrible news Jamie. :(
I feel bad asking for help during this time.


Thanks, bro, but doing things like this is helping keeping my mind off things. I will try and post the stuff on the control tonight.

Peace
Was This Post Helpful? 0
  • +
  • -

#7 papuccino1   User is offline

  • His name was Robert Paulson.
  • member icon

Reputation: 63
  • View blog
  • Posts: 1,121
  • Joined: 02-March 08

Re: Trouble using the ProgressBar control.

Posted 26 May 2009 - 05:08 AM

All the best Six, seriously.
Was This Post Helpful? 0
  • +
  • -

#8 searockruz   User is offline

  • D.I.C Regular

Reputation: 24
  • View blog
  • Posts: 460
  • Joined: 07-March 09

Re: Trouble using the ProgressBar control.

Posted 26 May 2009 - 05:19 AM

sorry to hear that
may God heal your Mom
Was This Post Helpful? 0
  • +
  • -

#9 SixOfEleven   User is offline

  • Planeswalker
  • member icon

Reputation: 1055
  • View blog
  • Posts: 6,643
  • Joined: 18-October 08

Re: Trouble using the ProgressBar control.

Posted 26 May 2009 - 08:12 PM

Well, I have a cool little class for you. It implements a very cool progress bar. All you have to do is have a picture box control on the form. I would make it not too thin but rater long. The class has a lot of functionality. I'll show you the class and a small program I wrote to test it. With the class, you can change the text, the color of the bar, the color of the text, pause it, restart it and stop it.

This is the class:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

// using statements that had to be added to the class
using System.Windows.Forms;
using System.Drawing;

namespace WindowsFormsApplication1
{
    class MyProgressBar
    {
        // Fields of the class
        PictureBox pictureBox;
        int percent;
        string text;
        Color fgColor;
        Color bgColor;
        Color tColor;
        Graphics graphics;
        Font font;
        bool paused = false;

         // Constructor for the class - takes a picture box control, the fore ground color, the back ground color
         // and the text color
        public MyProgressBar(PictureBox picturebox, Color fgColor, Color bgColor, Color tColor)
        {
            pictureBox = picturebox;
            percent = 0;
            this.fgColor = fgColor;
            this.bgColor = bgColor;
            this.tColor = tColor;
            graphics = pictureBox.CreateGraphics();
            font = new Font("Arial", 8);
            Reset();
        }

        // property to get the pause state and set the pause state
        public bool Paused
        {
            get { return paused; }
            set { paused = value; }
        }

       // property to set the string to be displayed in the control
        public string Text
        {
            get { return text; }
            set { text = value; }
        }

        // property to get and set the foreground color of the control
        public Color ForeColor
        {
            get { return fgColor; }
            set { fgColor = value; }
        }

        // property to get and set the background color of the control
        public Color BackColor
        {
            get { return bgColor; }
            set { bgColor = value; }
        }

        // property to get and set the text color of the progress bar
        public Color TextColor
        {
            get { return tColor; }
            set { tColor = value; }
        }

        // property used to get if the progress is 100%
        public bool Finished
        {
            get { if (percent == 100) return true; else return false; }
        }

        // property to get what percent the progress is at
        public int Percent
        {
            get { return percent; }
        }

        // Reset the progress bar
        public void Reset()
        {
            paused = false;
            percent = 0;
            text = "";
            Draw();
        }
        
        // used to update the percentage by 1 percent at a time
        public void Update()
        {
            if (!paused)
            {
                percent++;
                if (percent > 100)
                    percent = 100;
            }
        }

        // used to update the percentage by an amount
        public void Update(int increment)
        {
            if (!paused)
            {
                percent += increment;
                if (percent > 100)
                    percent = 100;
            }
        }
        
        // is used to draw the progress bar and the text in the progress bar
        public void Draw()
        {
            Rectangle rect = new Rectangle();
            rect = pictureBox.ClientRectangle;
            graphics.FillRectangle(new SolidBrush(bgColor), rect);
            rect.X += 2;
            rect.Y += 2;
            rect.Width -= 4;
            rect.Height -= 4;
            int width = (int)(rect.Width * percent / 100);
            rect.Width = width;
            graphics.FillRectangle(new SolidBrush(fgColor), rect);
            SizeF textSize = graphics.MeasureString(text, font);
            PointF point = new PointF( (pictureBox.ClientRectangle.Width / 2) - (textSize.Width / 2),
                (pictureBox.ClientRectangle.Height / 2) - (textSize.Height / 2));
            graphics.DrawString(text, font, new SolidBrush(tColor), point);
        }
    }
}



To test the class I made a simple form, with four visible controls and one timer. Three buttons and a picture box.

This is a screen shot of the program running:

Attached Image

This is the program I used to demonstrate the class for you. Instead of relying on buttons and the tick event, you will have to find a way to insert the code into your big four second method.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        MyProgressBar progressBar;
        Random random = new Random();
        string oldText;

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            progressBar.Reset();
            progressBar.Draw();
            button1.Enabled = false;
            timer1.Start();
            button2.Enabled = true;
            button3.Enabled = true;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //  These lines are VERY important for the control to appear on the screen.
            pictureBox1.BorderStyle = BorderStyle.FixedSingle;
            pictureBox1.BackColor = Color.White;

            timer1.Interval = 250;
            this.Show();
            progressBar = new MyProgressBar(pictureBox1, Color.FromArgb(0, 255, 0), Color.White, Color.Black);
            progressBar.Draw();
            button2.Enabled = false;
            button3.Enabled = false;
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            string[] strings = new string[] { "Something very Cool", "Another awesome quote", "Different text" };
            Color[] colors = new Color[] { Color.LightBlue, Color.CornflowerBlue, Color.PowderBlue };

          
            progressBar.Update(5);

            if (progressBar.Percent < 33)
            {
                progressBar.ForeColor = colors[0];
                progressBar.Text = strings[0];
            }
            else if (progressBar.Percent < 66)
            {
                progressBar.ForeColor = colors[1];
                progressBar.Text = strings[1];
            }
            else
            {
                progressBar.ForeColor = colors[2];
                progressBar.Text = strings[2];
            }
            
            progressBar.Draw();

            if (progressBar.Finished)
            {
                progressBar.Reset();
                progressBar.Text = "Finished";
                progressBar.Draw();
                timer1.Stop();
                button1.Enabled = true;
                button2.Enabled = false;
                button3.Enabled = false;
            }

        }

        private void button2_Click(object sender, EventArgs e)
        {

            if (!progressBar.Paused)
            {
                timer1.Stop();
                progressBar.Paused = true;
                oldText = progressBar.Text;
                button2.Text = "Continue";
                progressBar.Text = "PAUSED";
                progressBar.Draw();
            }
            else
            {
                button2.Text = "Pause Process";
                progressBar.Text = oldText;
                progressBar.Paused = false;
                timer1.Start();
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            timer1.Stop();
            button1.Enabled = true;
            progressBar.Reset();
            button2.Enabled = false;
            button3.Enabled = false;
        }
    }
}


Was This Post Helpful? 1
  • +
  • -

#10 papuccino1   User is offline

  • His name was Robert Paulson.
  • member icon

Reputation: 63
  • View blog
  • Posts: 1,121
  • Joined: 02-March 08

Re: Trouble using the ProgressBar control.

Posted 27 May 2009 - 06:41 AM

Thanks for the massive help Jamie. I'll sneak this into my code :P
Was This Post Helpful? 0
  • +
  • -

#11 mistyfyed   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 13
  • Joined: 18-March 09

Re: Trouble using the ProgressBar control.

Posted 27 May 2009 - 08:32 PM

Can't you just use a label, progress bar, and background worker? I think that would probably be the most efficient method. It would also improve responsiveness as you mentioned in the first post.
Was This Post Helpful? 1

Page 1 of 1