CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    Sep 2005
    Posts
    21

    Question Refresh label (windows forms)

    I am new to the dot net framework and currently trying to develop an application in it. My problem is that this application download several files decompress them...

    But I want to show the user what the application is doing, I create a new form and launch it from the download thread (which is a different thread then the main forms thread), when this class is launched it executes the UpdateProc function. This launches a new form that SHOULD display the progress...

    It is a simple form created with VS 2003, containing a progess bar & 2 labels. But after I show the dialog it won't be closed anymore trough the code. And I am also unable to refresh the label's to display updated text! It should display "After entering" but it remains at "Before entering".

    My updateproc looks like this:
    Code:
    /// <summary>
    /// The Update thread function
    /// </summary>
    
                    private progress PG = new progress();
    
    		public void UpdateProc()
    		{
    
    			// create thread
    			PG.link=this;
    			PG.label1.Text = "Before entering";
    		
    			PG.ShowDialog();
    
    			PG.label1.Text = "After entering";
    			PG.label1.Refresh();
    
                            Thread.Sleep(10000);
    			PG.Close();
    
    		}
    And my form class progress looks like:
    Code:
    	public class progress : System.Windows.Forms.Form
    	{
    		private System.Windows.Forms.ProgressBar progressBar1;
    		public System.Windows.Forms.Label label1;
    		public System.Windows.Forms.Label label2;
    		private System.ComponentModel.Container components = null;
    		public XML link; //link to class that launched the form!
    
    		public progress()
    		{
    			InitializeComponent();
    		}
    
    		protected override void Dispose( bool disposing )
    		{
    			if( disposing )
    			{
    				if(components != null)
    				{
    					components.Dispose();
    				}
    			}
    			base.Dispose( disposing );
    		}
    
    		#region Windows Form Designer generated code
    		public void InitializeComponent()
    		{
    			this.progressBar1 = new System.Windows.Forms.ProgressBar();
    			this.label1 = new System.Windows.Forms.Label();
    			this.label2 = new System.Windows.Forms.Label();
    			this.SuspendLayout();
    			// 
    			// progressBar1
    			// 
    			this.progressBar1.Location = new System.Drawing.Point(8, 56);
    			this.progressBar1.Name = "progressBar1";
    			this.progressBar1.Size = new System.Drawing.Size(464, 24);
    			this.progressBar1.TabIndex = 0;
    			// 
    			// label1
    			// 
    			this.label1.Location = new System.Drawing.Point(8, 8);
    			this.label1.Name = "label1";
    			this.label1.Size = new System.Drawing.Size(464, 16);
    			this.label1.TabIndex = 1;
    			// 
    			// label2
    			// 
    			this.label2.Location = new System.Drawing.Point(8, 32);
    			this.label2.Name = "label2";
    			this.label2.Size = new System.Drawing.Size(464, 16);
    			this.label2.TabIndex = 2;
    			// 
    			// progress
    			// 
    			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
    			this.ClientSize = new System.Drawing.Size(480, 83);
    			this.Controls.Add(this.label2);
    			this.Controls.Add(this.label1);
    			this.Controls.Add(this.progressBar1);
    			this.Name = "progress";
    			this.Text = "progress";
    			this.ResumeLayout(false);
    
    		}
    		#endregion
    	}
    As you can see it is just a standard code generated by VS 2003.

  2. #2
    Join Date
    Sep 2005
    Posts
    21

    Re: Refresh label (windows forms)

    Sorry to post a *bump*, but I really can't figure out how to get this to work...

  3. #3
    Join Date
    Jun 2005
    Posts
    121

    Re: Refresh label (windows forms)

    Hi there,
    Well the way you wrote it is not going to work ever as your code PG.label1.Text = "After entering"; will be reached after the form will become hidden as a result of ShowDialog returning(showdialog will block the execution there). What i suggest you is implement PG.Shown which occurs the first time the form is being first shown...but then you won't see before entering as u change the text in this method.

  4. #4
    Join Date
    Sep 2005
    Posts
    21

    Re: Refresh label (windows forms)

    Isn't it possible modify the variables and redraw the form? The form needs to be changed a few times, as the progress bar also needs to be updated...

  5. #5
    Join Date
    Oct 2004
    Location
    Rocket City
    Posts
    220

    Re: Refresh label (windows forms)

    After you update the labels try this:
    Code:
    Application.DoEvents();

  6. #6
    Join Date
    Dec 2003
    Location
    http://map.search.ch/zuerich.en.html
    Posts
    1,074

    Re: Refresh label (windows forms)

    PG is on it's own message loop via ShowDialog, so to update it it you need to be on the same thread.

    Rather than access its properties directly, PG should implement a public method eg. Progress(string text)

    This method should use the Control.Invoke method to get onto the same thread as the PG dialog.
    Useful? Then click on (Rate This Post) at the top of this post.

  7. #7
    Join Date
    Sep 2005
    Posts
    21

    Re: Refresh label (windows forms)

    I got it working... But i am using show() instead of showdialog() now...

    Altough i would liek to use a methode that is compatible with showdialog... But i am not sure waht you mean by getting in the same thread, does showdialog start a new thread for the dialog?

  8. #8
    Join Date
    Dec 2003
    Location
    http://map.search.ch/zuerich.en.html
    Posts
    1,074

    Re: Refresh label (windows forms)

    But i am not sure waht you mean by getting in the same thread, does showdialog start a new thread for the dialog?
    Yes.
    Useful? Then click on (Rate This Post) at the top of this post.

  9. #9
    Join Date
    Sep 2005
    Posts
    21

    Re: Refresh label (windows forms)

    Ok i see... But i am really lost at the invoke part....

    Could anyoen post a sample (C#) about how to use the invoke?

  10. #10
    Join Date
    Dec 2003
    Location
    http://map.search.ch/zuerich.en.html
    Posts
    1,074

    Re: Refresh label (windows forms)

    A lot of people already have: Google "InvokeRequired"
    Useful? Then click on (Rate This Post) at the top of this post.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured