C# Label Text Not Updating

前端 未结 7 1801
無奈伤痛
無奈伤痛 2021-01-04 06:16

I have the following code:

private void button1_Click(object sender, EventArgs e)
{
  var answer =
    MessageBox.Show(
      \"Do you wish to submit checked         


        
相关标签:
7条回答
  • 2021-01-04 06:36

    Just to add to this answer, I ran into an issue with our splash screen form. We had code like this:

    
    SplashScreen.Initialize(this, SplashScreenImage);
    SplashScreen.Show();
    
    // Db init, log init etc.
    
    ... Further in our app ...
    
    Application.Run(new MainWindowForm());
    
    

    The in Initialize(this, SplashScreenImage); we updated some controls and then we ran refresh on those controls;

    
    public void Initialize(this, SplashScreenImage)
    {
       ...
    
       lblVersion.Text = GetVersionString();
       lblEnv.Text = GetEnvironmentString();
    
       // Refresh (does not work)
       lblVersion.Refresh()
       lblEnv.Refresh()
    }
    
    

    Unfortunately, this does not work. The problem here was that although we call control.Refresh() explicitly, form.show() was called after we called control.refresh. This does not work.

    The fix was simple:

    SplashScreen.Show(); // First call show
    SplashScreen.Initialize(this, SplashScreenImage); // Now contorl.Refresh() works
    
    0 讨论(0)
  • 2021-01-04 06:36

    I know this question is old but I had this same issue. I tried Refresh() and many other things but nothing worked. If I put the text into a Messagebox.show then it worked in the message box but not the form so I knew I had the data. As I had people waiting to use the app I was getting desperate and was just about to do away with the class temporarily to get it working when I thought of trying Invoke. So I tried

    Invoke(new Action(() =>
            {
                lbltxt.Text = text;
    
            }));
    

    For now it works but still not sure if this is a long term fix or just a plaster till I find a better solution.

    0 讨论(0)
  • 2021-01-04 06:53

    use Label.Refresh(); it saves a lot of time.This should work for u

    0 讨论(0)
  • 2021-01-04 06:53

    This operation is executed in UI thread. UI won't update until it's finished. To make it update during sending you must perform sending in separate thread and update the label from there

    0 讨论(0)
  • 2021-01-04 06:53

    This usually happens when you're doing intensive calculations/iterations in the same thread as where the user interface elements are running. To work around this you're going to need to have a separate thread do the work and from there update the label's value accordingly. I'd post a complete source sample but I'm away from my dev machine at the moment.

    0 讨论(0)
  • 2021-01-04 06:55

    You're performing a lengthy operation on the UI thread. You should move it to a background thread (via BackgroundWorker for instance) so the UI thread can do things like repaint the screen when needed. You can cheat and execute Application.DoEvents, but I'd really recommend against it.

    This question and answer are basically what you're asking:
    Form Not Responding when any other operation performed in C#

    0 讨论(0)
提交回复
热议问题