Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7633509
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T06:54:19+00:00 2026-05-31T06:54:19+00:00

im having some issues with some text im writing on a progress bar, it

  • 0

im having some issues with some text im writing on a progress bar, it displays but then will disappear once the GUI thread has finished drawing, and the separate thread running the loop commences again. Im assuming this is because its out of scope but im not sure how to fix it.

the general layout is as follows:

namespace namespace1
{
    public partial class Form1:Form
    {
     ....
    }

    public class ProgressBar
    {
        public void SubscribeToUpdateEvent()
        {
            //subscribe incrementPB to event
        }

        public void IncrementPB(object sender, EventArgs e)
        {
            //Update progress bar's value
            DrawPercentage(value);        
        }

        public void DrawPercentage(Value)
        {

        using (Graphics Draw = statusBar.CreateGraphics())
        {
            Draw.DrawString(percentage.ToString() + "%", ProgressBar.DefaultFont, Brushes.Black, new PointF((statusBar.Width / 2) - ((Draw.MeasureString(percentage.ToString() + "%", ProgressBar.DefaultFont)).Width / 2.0F),
                (statusBar.Height / 2) - ((Draw.MeasureString(percentage.ToString() + "%", ProgressBar.DefaultFont)).Height / 2.0F)));
        }
    }
}

//Second file which processes a bunch of data on a separate thread and raises the event after each loop.

namespace namespace2
{
    public class MyClass
    {
        public void iterator()
        {
            for(int i=0;i<10;i++)
            {
                //raise event to update the progress bar here
            }
        }
     }
 }

Thanks for your help.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T06:54:21+00:00Added an answer on May 31, 2026 at 6:54 am

    The percentage label probably gets erased when the next Paint event occurs on the progress bar. Try implementing the progress bar as a user control, this way you can override the OnPaint method and render the text there (after base.OnPaint(e)).
    I made a few simple classes to simulate your situation. Here is the approach that works for me:

    1) The progress bar control (simple implementation, just to outline the basic idea with OnPaint):

    public partial class CustomProgressBar : UserControl
    {
        public int Percentage { get; set; }
    
        public CustomProgressBar()
        {
            InitializeComponent();
        }
    
        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            Graphics g = e.Graphics;
            Rectangle bounds = new Rectangle(ClientRectangle.X, ClientRectangle.Y, ClientRectangle.Width - 1, ClientRectangle.Height - 1);
    
            // Paint a green bar indicating the progress.
            g.FillRectangle
            (
                Brushes.LimeGreen,
                new Rectangle
                (
                    bounds.X,
                    bounds.Y,
                    (int)(bounds.Width * Percentage / 100.0),
                    bounds.Height
                )
            );
    
            // Draw a black frame around the progress bar.
            g.DrawRectangle(Pens.Black, bounds);
    
            // Draw the percentage label.
            TextRenderer.DrawText(g, Percentage + "%", Font, bounds, ForeColor);
        }
    }
    

    2) Class that represents the job running in a background thread:

    public class BackgroundJob
    {
        public event Action<object, int> StepCompleted;
    
        public void Execute()
        {
            const int TotalSteps = 10;
            for (int step = 1; step <= TotalSteps; step++)
            {
                // Execute some heavy work here.
                Thread.Sleep(1000);
    
                // Calculate percentage (0..100).
                int percentage = (int)(step * 100.0 / TotalSteps);
    
                // Notify the subscribers that the step has been completed.
                OnStepCompleted(percentage);
            }
        }
    
        protected virtual void OnStepCompleted(int percentage)
        {
            if (StepCompleted != null)
            {
                StepCompleted(this, percentage);
            }
        }
    }
    

    3) Form class. Contains the custom progress bar control and starts the background job when a button is pressed.

    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }
    
        private void btnStartJob_Click(object sender, EventArgs e)
        {
            Thread backgroundThread = new Thread
            (
                () =>
                {
                    var job = new BackgroundJob();
                    job.StepCompleted += job_StepCompleted;
                    job.Execute();
                }
            );
            backgroundThread.Start();
        }
    
        private void job_StepCompleted(object sender, int percentage)
        {
            progressBar.Percentage = percentage;
    
            // Force the progress bar to redraw itself.
            progressBar.Invalidate();
        }
    }
    

    You can call Invalidate() internally (inside the Percentage property setter), I called it separately just to emphasize how the painting process is triggered.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having some issues aligning the header text of a datagrid. I'm using an
LIVE CODE: http://jsfiddle.net/vy4nY/ I'm following this challenge , but I'm having some issues. I'm
I'm having some issues with Javascript content on Internet Explorer, specifically, a page will
I am having some issues with matching text to extract data from an HTML
I am having some issues with writing and reading with android. here is my
I was recently having some issues with a bigger project, but in an effort
I've been working on an HTML5 video implementation, but I'm having some issues. I've
Having some issues getting my head around the differences between UTF-8, UTF-16, ASCII and
Having some issues with the ... in ObjectiveC. I'm basically wrapping a method and
We've been having some issues with a SharePoint instance in a test environment. Thankfully

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.