Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Changing the text of label

Changing the text of label

Scheduled Pinned Locked Moved C#
18 Posts 7 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S sujithkumarsl

    hi i am trying to chage the text of a label, but the thing is that its not fast i am setting the text before some function calls but the text is changed after all the functions. Do you know any effective method to quickly change the text of a label

    My small attempt...

    G Offline
    G Offline
    Giorgi Dalakishvili
    wrote on last edited by
    #2

    Can you post the code snippet? Also run the application in debug mode to see what happens

    S 1 Reply Last reply
    0
    • G Giorgi Dalakishvili

      Can you post the code snippet? Also run the application in debug mode to see what happens

      S Offline
      S Offline
      sujithkumarsl
      wrote on last edited by
      #3

      like label1.Text = " method1"; method1(); label1.Text = "method2"; method2(); label1.Text = "method3"; method3(); this will disply method3 after all function call we will not get the other text i maen no refreshing

      My small attempt...

      G R D 3 Replies Last reply
      0
      • S sujithkumarsl

        like label1.Text = " method1"; method1(); label1.Text = "method2"; method2(); label1.Text = "method3"; method3(); this will disply method3 after all function call we will not get the other text i maen no refreshing

        My small attempt...

        G Offline
        G Offline
        Giorgi Dalakishvili
        wrote on last edited by
        #4

        This because it executes method1() and method2() quickly and you can't see the change. Write something like this label1.Text = " method1"; System.Threading.Thread.Sleep(5000); method1(); label1.Text = "method2"; System.Threading.Thread.Sleep(5000); method2(); label1.Text = "method3"; method3(); and you will see the process

        S S D 3 Replies Last reply
        0
        • G Giorgi Dalakishvili

          This because it executes method1() and method2() quickly and you can't see the change. Write something like this label1.Text = " method1"; System.Threading.Thread.Sleep(5000); method1(); label1.Text = "method2"; System.Threading.Thread.Sleep(5000); method2(); label1.Text = "method3"; method3(); and you will see the process

          S Offline
          S Offline
          sujithkumarsl
          wrote on last edited by
          #5

          You can check you sample it will not work,i mean will not give the textchange i had tried this earlier... actually the thing is that my methods itself taking more time to execute. You know anyother methods

          My small attempt...

          S 1 Reply Last reply
          0
          • S sujithkumarsl

            You can check you sample it will not work,i mean will not give the textchange i had tried this earlier... actually the thing is that my methods itself taking more time to execute. You know anyother methods

            My small attempt...

            S Offline
            S Offline
            Sandeep Akhare
            wrote on last edited by
            #6

            Search for the Asynchronous processing that will help you

            Thanks and Regards Sandeep If you want something you never had, do something you have never done!

            S 1 Reply Last reply
            0
            • S Sandeep Akhare

              Search for the Asynchronous processing that will help you

              Thanks and Regards Sandeep If you want something you never had, do something you have never done!

              S Offline
              S Offline
              sujithkumarsl
              wrote on last edited by
              #7

              Can you just expalin, what you mean by that?

              My small attempt...

              1 Reply Last reply
              0
              • S sujithkumarsl

                like label1.Text = " method1"; method1(); label1.Text = "method2"; method2(); label1.Text = "method3"; method3(); this will disply method3 after all function call we will not get the other text i maen no refreshing

                My small attempt...

                R Offline
                R Offline
                Rudolf Jan
                wrote on last edited by
                #8

                I think this cannot work. Setting the label text generates under water a message to the control. The label control then must update itself. If you change the label text three times inside the same method, you do not give control back to the message loop. So the message loop starts updating only after the third change and you will not notice the change. I'm not sure how to solve this. It may be a design error. Maybe you can solve this using an owner drawn label or by using timers.

                S 1 Reply Last reply
                0
                • R Rudolf Jan

                  I think this cannot work. Setting the label text generates under water a message to the control. The label control then must update itself. If you change the label text three times inside the same method, you do not give control back to the message loop. So the message loop starts updating only after the third change and you will not notice the change. I'm not sure how to solve this. It may be a design error. Maybe you can solve this using an owner drawn label or by using timers.

                  S Offline
                  S Offline
                  sujithkumarsl
                  wrote on last edited by
                  #9

                  i got it......... we can use doevents for repainting the form....... thanks to all

                  My small attempt...

                  G 1 Reply Last reply
                  0
                  • G Giorgi Dalakishvili

                    This because it executes method1() and method2() quickly and you can't see the change. Write something like this label1.Text = " method1"; System.Threading.Thread.Sleep(5000); method1(); label1.Text = "method2"; System.Threading.Thread.Sleep(5000); method2(); label1.Text = "method3"; method3(); and you will see the process

                    S Offline
                    S Offline
                    Soundman32 2
                    wrote on last edited by
                    #10

                    That won't work, it will just delay the code. You need to tell Windows to redraw the label with : label1.Text = " method1"; label1.Refresh(); method1(); label1.Text = "method2"; label1.Refresh(); method2(); label1.Text = "method3"; label1.Refresh(); method3() From the help for Control.Refresh: Forces the control to invalidate its client area and immediately redraw itself and any child controls.

                    1 Reply Last reply
                    0
                    • S sujithkumarsl

                      i got it......... we can use doevents for repainting the form....... thanks to all

                      My small attempt...

                      G Offline
                      G Offline
                      gumi_r msn com
                      wrote on last edited by
                      #11

                      You can call Refresh() after each text change. It will force your control to repaint itself before executing the next method. If you're not concerned about an unresponsive UI and the slight performance loss when calling Refresh() then its the easiest solution.

                      D 1 Reply Last reply
                      0
                      • S sujithkumarsl

                        like label1.Text = " method1"; method1(); label1.Text = "method2"; method2(); label1.Text = "method3"; method3(); this will disply method3 after all function call we will not get the other text i maen no refreshing

                        My small attempt...

                        D Offline
                        D Offline
                        Dave Kreskowiak
                        wrote on last edited by
                        #12

                        After you change the Text of the label, you're code doesn't go idle to allow the message pump to process the WM_PAINT message it receives because you changed the label. You need to call Application.DoEvents() to get your code to let the message pump do it's work. This is easily done if you move the code to change the label to a method:

                        UpdateStatusMessage("Calling method1...");
                        method1();
                        UpdateStatusMessage("Calling method2...");
                        method2();
                        UpdateStatusMessage("Calling method3...");
                        method3();
                        

                        .
                        .
                        .
                        private void UpdateStatusMessage(string message)
                        {
                        label1.Text = message;
                        Application.DoEvents();
                        }

                        Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                             2006, 2007

                        S 1 Reply Last reply
                        0
                        • G Giorgi Dalakishvili

                          This because it executes method1() and method2() quickly and you can't see the change. Write something like this label1.Text = " method1"; System.Threading.Thread.Sleep(5000); method1(); label1.Text = "method2"; System.Threading.Thread.Sleep(5000); method2(); label1.Text = "method3"; method3(); and you will see the process

                          D Offline
                          D Offline
                          Dave Kreskowiak
                          wrote on last edited by
                          #13

                          This won't fix the problem. Sleeping the UI for 5 seconds, after each update of a label, will only hang his app unnecessarily. Application.DoEvents() is what is needed in his situation.

                          Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                               2006, 2007

                          1 Reply Last reply
                          0
                          • G gumi_r msn com

                            You can call Refresh() after each text change. It will force your control to repaint itself before executing the next method. If you're not concerned about an unresponsive UI and the slight performance loss when calling Refresh() then its the easiest solution.

                            D Offline
                            D Offline
                            Dave Kreskowiak
                            wrote on last edited by
                            #14

                            Refresh won't work either because his code is not giving up control to allow the application's message pump to process the WM_PAINT messages and call the repaint code.

                            Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                                 2006, 2007

                            G 1 Reply Last reply
                            0
                            • D Dave Kreskowiak

                              Refresh won't work either because his code is not giving up control to allow the application's message pump to process the WM_PAINT messages and call the repaint code.

                              Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                                   2006, 2007

                              G Offline
                              G Offline
                              gumi_r msn com
                              wrote on last edited by
                              #15

                              Refresh() is equivalent to {Invalidate(true); Update();} and the Update() method forces the control to repaint itselft executing any pending WM_PAINT messages bypassing the application queue. Update() is equivalent to the UpdateWindow() function. More info in: http://msdn2.microsoft.com/en-us/library/ms534874.aspx[^]. I might be missing something, but try this code here. It works perfectly if you can bare an unresponsive UI. My label is refreshing perfectly. Its a simple form with a label and a button to start the test:

                                  public partial class Form1 : Form
                                  {
                                      public Form1()
                                      {
                                          InitializeComponent();
                                      }
                              
                              
                                      private void button1_Click(object sender, EventArgs e)
                                      {
                                          doTest();
                                      }
                              
                              
                                      private void doTest()
                                      {
                                          label1.Text = "Starting Method 1...";
                                          label1.Refresh();
                                          anyMethodCall();
                                          label1.Text = "Starting Method 2...";
                                          label1.Refresh();
                                          anyMethodCall();
                                          label1.Text = "Starting Method 3...";
                                          label1.Refresh();
                                          anyMethodCall();
                                          label1.Text = "Starting Method 4...";
                                          label1.Refresh();
                                          anyMethodCall();
                                      }
                              
                              
                                      private void anyMethodCall()
                                      {
                                          System.Threading.Thread.Sleep(5000);
                                      }
                                  }
                              

                              -- modified at 12:25 Thursday 3rd May, 2007

                              G 1 Reply Last reply
                              0
                              • G gumi_r msn com

                                Refresh() is equivalent to {Invalidate(true); Update();} and the Update() method forces the control to repaint itselft executing any pending WM_PAINT messages bypassing the application queue. Update() is equivalent to the UpdateWindow() function. More info in: http://msdn2.microsoft.com/en-us/library/ms534874.aspx[^]. I might be missing something, but try this code here. It works perfectly if you can bare an unresponsive UI. My label is refreshing perfectly. Its a simple form with a label and a button to start the test:

                                    public partial class Form1 : Form
                                    {
                                        public Form1()
                                        {
                                            InitializeComponent();
                                        }
                                
                                
                                        private void button1_Click(object sender, EventArgs e)
                                        {
                                            doTest();
                                        }
                                
                                
                                        private void doTest()
                                        {
                                            label1.Text = "Starting Method 1...";
                                            label1.Refresh();
                                            anyMethodCall();
                                            label1.Text = "Starting Method 2...";
                                            label1.Refresh();
                                            anyMethodCall();
                                            label1.Text = "Starting Method 3...";
                                            label1.Refresh();
                                            anyMethodCall();
                                            label1.Text = "Starting Method 4...";
                                            label1.Refresh();
                                            anyMethodCall();
                                        }
                                
                                
                                        private void anyMethodCall()
                                        {
                                            System.Threading.Thread.Sleep(5000);
                                        }
                                    }
                                

                                -- modified at 12:25 Thursday 3rd May, 2007

                                G Offline
                                G Offline
                                gumi_r msn com
                                wrote on last edited by
                                #16

                                Ok, I'm wrong, damn OS being smart and getting in the way :) My app fails to refresh if I try to move the window around. During the first 2 calls to anyMethodCall() my UI is unresponsive and my label is refreshing perfectly but then the window will start to move perfectly according to my mouse requests even if the app is still executing calls 3 and 4. And whats worse, my label stops refreshing. This is due (I guess) to Windows (Xp in this case, don't know if 2000 or previous work the same way) detecting my window as unresposive and taking action. DoEvents() will take care of this situation becuase it will inform the OS that my window is once again valid while Refresh() will not do that and the OS will keep on thinking its not responding. So yeah DoEvents() is the best solution because you can't rely on the user not trying to move the window around. My bad :) -- modified at 12:48 Thursday 3rd May, 2007

                                D 1 Reply Last reply
                                0
                                • D Dave Kreskowiak

                                  After you change the Text of the label, you're code doesn't go idle to allow the message pump to process the WM_PAINT message it receives because you changed the label. You need to call Application.DoEvents() to get your code to let the message pump do it's work. This is easily done if you move the code to change the label to a method:

                                  UpdateStatusMessage("Calling method1...");
                                  method1();
                                  UpdateStatusMessage("Calling method2...");
                                  method2();
                                  UpdateStatusMessage("Calling method3...");
                                  method3();
                                  

                                  .
                                  .
                                  .
                                  private void UpdateStatusMessage(string message)
                                  {
                                  label1.Text = message;
                                  Application.DoEvents();
                                  }

                                  Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                                       2006, 2007

                                  S Offline
                                  S Offline
                                  sujithkumarsl
                                  wrote on last edited by
                                  #17

                                  this is what i done........ anyway thanks for all

                                  My small attempt...

                                  1 Reply Last reply
                                  0
                                  • G gumi_r msn com

                                    Ok, I'm wrong, damn OS being smart and getting in the way :) My app fails to refresh if I try to move the window around. During the first 2 calls to anyMethodCall() my UI is unresponsive and my label is refreshing perfectly but then the window will start to move perfectly according to my mouse requests even if the app is still executing calls 3 and 4. And whats worse, my label stops refreshing. This is due (I guess) to Windows (Xp in this case, don't know if 2000 or previous work the same way) detecting my window as unresposive and taking action. DoEvents() will take care of this situation becuase it will inform the OS that my window is once again valid while Refresh() will not do that and the OS will keep on thinking its not responding. So yeah DoEvents() is the best solution because you can't rely on the user not trying to move the window around. My bad :) -- modified at 12:48 Thursday 3rd May, 2007

                                    D Offline
                                    D Offline
                                    Dave Kreskowiak
                                    wrote on last edited by
                                    #18

                                    gumi_r@msn.com wrote:

                                    Ok, I'm wrong, damn OS being smart and getting in the way My app fails to refresh if I try to move the window around.

                                    OK, but that wasn't the point behind using Application.DoEvents(). Try updating 6 or 7 controls during a blocking operation. Now you'e got to call Update on those controls, individually. Mking all the changes then calling Application.DoEvents() let's all those controls repaint themselves all at once, with a single call.

                                    gumi_r@msn.com wrote:

                                    So yeah DoEvents() is the best solution because you can't rely on the user not trying to move the window around. My bad

                                    No big deal! :-D Everything has a purpose, even Refresh!

                                    Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                                         2006, 2007

                                    1 Reply Last reply
                                    0
                                    Reply
                                    • Reply as topic
                                    Log in to reply
                                    • Oldest to Newest
                                    • Newest to Oldest
                                    • Most Votes


                                    • Login

                                    • Don't have an account? Register

                                    • Login or register to search.
                                    • First post
                                      Last post
                                    0
                                    • Categories
                                    • Recent
                                    • Tags
                                    • Popular
                                    • World
                                    • Users
                                    • Groups