C# Label Text Not Updating

前端 未结 7 1829
無奈伤痛
無奈伤痛 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
    

提交回复
热议问题