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

Asked

Viewed 803 times

2

I need to make the event click a button located on a form, open another modal form. While the other form is loaded, I need to display a message to the user in a Statuslabel of a Statusstrip. (an msg of type... Wait for configuration to load...)

The behavior I noticed is that the message displayed to the user is always loaded 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 didn’t 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?

Obs: modelStatusLabel is an object of the type ToolStripStatusLabel.

  • 1

    @Caffè, modelStatusLabel is an object of the type ToolStripStatusLabel

  • @Caffé Thanks. Perfect worked. statusStrip1.Refresh();

1 answer

2


This is because your application is busy opening the form and will only process Windows messages (for example this one that has write new text in the component) when the application is available ("Idle").

You can order immediate replenishment of the component by invoking its method Refresh().

Like the ToolStripStatusLabel does not have such method, you can paste this component on a StatusStrip (if not yet so) and then invoke the method refresh of Statusstrip, since this method orders the immediate repainting not only of the component itself but also of its child components.

Your code would look 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);
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.