0

I have a button that loads a Form, as it gets lots of data from a database and takes a few seconds, I want to advise the user to wait.

When I click the button the button text does not change.

This is the button Click code I am using:

private void btnItemConfigForm_Click(object sender, EventArgs e)
            {
                var itemConfigBtnText = btnItemConfigForm.Text;
                btnItemConfigForm.Text = "Waiting...";

                ItemConfigForm form = new ItemConfigForm();
                form.Show();

                if (form.Created)
                {
                    btnItemConfigForm.Text = itemConfigBtnText;
                }
            }

If I Comment out

if (form.Created)
{
    btnItemConfigForm.Text = itemConfigBtnText;
}

Then the button text changes to waiting after the new form window is visible.

What am I missing to get the button text to change before the form window is visible.

Peter Campbell
  • 661
  • 1
  • 7
  • 35
  • 7
    You're asking the wrong question. The proper question would be _"How can I load the database entries in the background so my UI thread won't block and messages keep being handled?"_. See [Why won't control update/refresh mid-process](http://stackoverflow.com/questions/2341731/why-wont-control-update-refresh-mid-process) for explanation. – CodeCaster Jun 01 '15 at 13:51
  • This is more or less a duplicate of http://stackoverflow.com/questions/1360944/force-gui-update-from-ui-thread. – Sploofy Jun 01 '15 at 13:56
  • @Casper no, no, no... don't link to that Visual Basic method. See also [Use of -readacted-](http://stackoverflow.com/questions/5181777/use-of-application-doevents). – CodeCaster Jun 01 '15 at 13:57
  • Cheers @CodeCaster I will avoid Application.DoEvents – Peter Campbell Jun 01 '15 at 14:28
  • UI is pretty much a datagridview, so useless without the data, so don't mind it blocking the UI – Peter Campbell Jun 01 '15 at 14:29

2 Answers2

2

the simple solution is to add this row:

btnItemConfigForm.Refresh();

after this row

btnItemConfigForm.Text = "Waiting...";

Otherwise the text of the button will be changed only when the function ends, this function will redraw the form display!

P.s. If you want the form will not be blocked - you can use in asynchronic running to the function "Show" (or New) then you will need Event to notify the first form when the form will be loaded

sorry for my English... :)

Peter Campbell
  • 661
  • 1
  • 7
  • 35
Git
  • 41
  • 6
  • 1
    Is there any difference between using control.Update() and control.Refresh()? – Peter Campbell Jun 01 '15 at 15:59
  • 1
    @PeterCampbell You're right, control.Update() it's better. I think that control.Refresh() redraw itself and all child controls, but, control.Update() redraw only what that changed – Git Jun 01 '15 at 18:43
0

Added

  btnItemConfigForm.Update();

under

var itemConfigBtnText = btnItemConfigForm.Text;
btnItemConfigForm.Text = "Waiting...";

This updates the button Control before moving on to initialising and showing the form.

Peter Campbell
  • 661
  • 1
  • 7
  • 35