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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. Can't set Label.Text

Can't set Label.Text

Scheduled Pinned Locked Moved C#
helpcsharpalgorithmsannouncement
6 Posts 3 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.
  • P Offline
    P Offline
    Pelgar
    wrote on last edited by
    #1

    I've been working with C# for about 6 months. This is the first time that a little searching has not resolved a problem for me. I'm trying to change the text of a label and update a progress bar in a loop that does some graphic calculations. The progress bar is working fine but the label text does not change until the loop has completed.

            int nImgCnt = m\_Parent.m\_ImageList.Count - 1;
            int nStatBarAdd = 1000 / nImgCnt;
    
            for (int nIdx = 0; nIdx <= nImgCnt; nIdx++)
            {   //FInd Min rectangle that will contain largest image
               lblProg.Text = "Calculating minimum image size. Image " + (nIdx+1).ToString() + " of " + (nImgCnt + 1).ToString();
               lblProg.Focus();        //Tried this just to see if it would work
               lblProg.Invalidate();   //Tried this just to see if it would work
               CurRC = getImageMinSize(m\_Parent.m\_ImageList\[nIdx\]); 
                if (CurRC.Width > MinRC.Width)
                {
                    MinRC.Width = CurRC.Width;
                    MinRC.X = CurRC.X;
                }
                if (CurRC.Height > MinRC.Height)
                {
                    MinRC.Height = CurRC.Height;
                    MinRC.Y = CurRC.Y;
                }
                prgBr.Value += nStatBarAdd;
    
            }
    

    If anyone can help me out with this I will really appreciate it!

    L 1 Reply Last reply
    0
    • P Pelgar

      I've been working with C# for about 6 months. This is the first time that a little searching has not resolved a problem for me. I'm trying to change the text of a label and update a progress bar in a loop that does some graphic calculations. The progress bar is working fine but the label text does not change until the loop has completed.

              int nImgCnt = m\_Parent.m\_ImageList.Count - 1;
              int nStatBarAdd = 1000 / nImgCnt;
      
              for (int nIdx = 0; nIdx <= nImgCnt; nIdx++)
              {   //FInd Min rectangle that will contain largest image
                 lblProg.Text = "Calculating minimum image size. Image " + (nIdx+1).ToString() + " of " + (nImgCnt + 1).ToString();
                 lblProg.Focus();        //Tried this just to see if it would work
                 lblProg.Invalidate();   //Tried this just to see if it would work
                 CurRC = getImageMinSize(m\_Parent.m\_ImageList\[nIdx\]); 
                  if (CurRC.Width > MinRC.Width)
                  {
                      MinRC.Width = CurRC.Width;
                      MinRC.X = CurRC.X;
                  }
                  if (CurRC.Height > MinRC.Height)
                  {
                      MinRC.Height = CurRC.Height;
                      MinRC.Y = CurRC.Y;
                  }
                  prgBr.Value += nStatBarAdd;
      
              }
      

      If anyone can help me out with this I will really appreciate it!

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      it all depends where your code is sitting. if it is in a GUI event handler (say a button click handler) then the main thread would be busy with your code, and hence the GUI would not respond to the things the user does, and a lot of things your code does. The right approach would be to use a separate thread, maybe a BackgroundWorker would be fine. if it is not in a GUI event handler, hence already being executed by some other thread, then you are performing illegal cross-thread operations to GUI components. You might want to read this article[^]. All the above applies to all kinds of Controls; from what I've seen, I can't tell why the progress bar works better for you, I don't think it should the way things are. :)

      Luc Pattyn [Forum Guidelines] [My Articles] [My CP bug tracking] Nil Volentibus Arduum

      Season's Greetings to all CPians.

      P J 2 Replies Last reply
      0
      • L Luc Pattyn

        it all depends where your code is sitting. if it is in a GUI event handler (say a button click handler) then the main thread would be busy with your code, and hence the GUI would not respond to the things the user does, and a lot of things your code does. The right approach would be to use a separate thread, maybe a BackgroundWorker would be fine. if it is not in a GUI event handler, hence already being executed by some other thread, then you are performing illegal cross-thread operations to GUI components. You might want to read this article[^]. All the above applies to all kinds of Controls; from what I've seen, I can't tell why the progress bar works better for you, I don't think it should the way things are. :)

        Luc Pattyn [Forum Guidelines] [My Articles] [My CP bug tracking] Nil Volentibus Arduum

        Season's Greetings to all CPians.

        P Offline
        P Offline
        Pelgar
        wrote on last edited by
        #3

        Thanks for the quick response Luc! Sorry, I should have pointed out this is in a button click event handler. I will trying doing the calculations in a BackgroundWorker, thanks again! Edit: I added a BackgroundWorker and life is good again. Thanks again Luc! I should have worked this one out for myself. I must be having a bad code day.

        modified on Sunday, January 2, 2011 1:47 PM

        L 1 Reply Last reply
        0
        • P Pelgar

          Thanks for the quick response Luc! Sorry, I should have pointed out this is in a button click event handler. I will trying doing the calculations in a BackgroundWorker, thanks again! Edit: I added a BackgroundWorker and life is good again. Thanks again Luc! I should have worked this one out for myself. I must be having a bad code day.

          modified on Sunday, January 2, 2011 1:47 PM

          L Offline
          L Offline
          Luc Pattyn
          wrote on last edited by
          #4

          You're welcome. :)

          Luc Pattyn [Forum Guidelines] [My Articles] [My CP bug tracking] Nil Volentibus Arduum

          Season's Greetings to all CPians.

          1 Reply Last reply
          0
          • L Luc Pattyn

            it all depends where your code is sitting. if it is in a GUI event handler (say a button click handler) then the main thread would be busy with your code, and hence the GUI would not respond to the things the user does, and a lot of things your code does. The right approach would be to use a separate thread, maybe a BackgroundWorker would be fine. if it is not in a GUI event handler, hence already being executed by some other thread, then you are performing illegal cross-thread operations to GUI components. You might want to read this article[^]. All the above applies to all kinds of Controls; from what I've seen, I can't tell why the progress bar works better for you, I don't think it should the way things are. :)

            Luc Pattyn [Forum Guidelines] [My Articles] [My CP bug tracking] Nil Volentibus Arduum

            Season's Greetings to all CPians.

            J Offline
            J Offline
            Jeff Connelly
            wrote on last edited by
            #5

            Luc Pattyn wrote:

            if it is not in a GUI event handler, hence already being executed by some other thread, then you are performing illegal cross-thread operations to GUI components. You might want to read this article[^].

            But since he's not getting an exception, that can't be it. Unless there's more to it than I've experienced.

            L 1 Reply Last reply
            0
            • J Jeff Connelly

              Luc Pattyn wrote:

              if it is not in a GUI event handler, hence already being executed by some other thread, then you are performing illegal cross-thread operations to GUI components. You might want to read this article[^].

              But since he's not getting an exception, that can't be it. Unless there's more to it than I've experienced.

              L Offline
              L Offline
              Luc Pattyn
              wrote on last edited by
              #6

              He didn't mention exceptions, so it was unclear whether any occurred or not. And a ThreadPool thread would happily swallow any exception he didn't handle explicitly: ThreadPool threads are built to survive no matter what. :)

              Luc Pattyn [Forum Guidelines] [My Articles] [My CP bug tracking] Nil Volentibus Arduum

              Season's Greetings to all CPians.

              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