Click event. Display message in current form and open new form

2

I need to make the click event of a button on a form open another modal form. While the other form is loaded, I need to display the user a message on a StatusLabel from a StatusStrip. (a msg of type ... Wait for the configuration to be loaded ...)

The behavior I noticed is that the message displayed to the user always loads after the modal form is opened, operated by the user, and then closed. The secure click event to display the message to the user until the modal of the other form is closed.

I tried to use a BackgroundWorker inside the click event, both to update the UI with a message, and to try to open the other modal form, but it did not work.

private void btnEdit_Click(object sender, EventArgs e)
{
    modelStatusLabel.BackColor = Color.FromArgb(192, 255, 192);
    modelStatusLabel.ForeColor = Color.Green;
    modelStatusLabel.Text = "Por favor aguarde a configuração ser carregada...";

    FormAddModel frmAddModelo = new FormAddModel(this, nameConfigFile);
    frmAddModelo.ShowDialog(this);
}

How can I do this?

Note: modelStatusLabel is an object of type ToolStripStatusLabel .

    
asked by anonymous 30.10.2014 / 13:47

1 answer

2

This is because your application is busy opening the form and will only process Windows messages (for example, the one that sends new text to the component) when the application is available (idle).

You can sort the immediate repaint of the component by invoking its Refresh() method.

Since ToolStripStatusLabel does not have such a method, you can paste this component over a StatusStrip (if it is not already that way) and then invoke the refresh method of StatusStrip , since this method orders the immediate repainting not only of the component itself but also of its child components.

Your code looks like this:

private void btnEdit_Click(object sender, EventArgs e)
{
    modelStatusLabel.BackColor = Color.FromArgb(192, 255, 192);
    modelStatusLabel.ForeColor = Color.Green;
    modelStatusLabel.Text = "Por favor aguarde a configuração ser carregada...";

    // considerando que modelStatusLabel seja um componente filho de statusStrip1,
    // o código abaixo fará o novo texto ser mostrado imediatamente.
    statusStrip1.Refresh();

    FormAddModel frmAddModelo = new FormAddModel(this, nameConfigFile);
    frmAddModelo.ShowDialog(this);
}
    
30.10.2014 / 16:38