Anant
Anant

Reputation: 352

form goes in background

I have a form, and doing showdialog on that. It is like msg box. I am doing something like

    msgBox = new MsgBox();
    if (msgBox.InvokeRequired)
    {
      msgBox.Invoke(new ShowMsg(ShowMessage));
    }
    else
    {
      ShowMessage();
    }

in show message i am doing Show Dialog. But the form goes in background of main form some times. please help on this.

Upvotes: 0

Views: 3193

Answers (5)

Jeson Martajaya
Jeson Martajaya

Reputation: 7352

Not sure how your ShowMsg and ShowMessage methods look like, but try this out:

msgBox = new MsgBox();
if (msgBox.InvokeRequired)
{
  msgBox.Invoke((MethodInvoker)delegate() { ShowMessage(); });
}
else
{
  ShowMessage();
}

Upvotes: 0

Niki
Niki

Reputation: 15867

How do you call ShowDialog? You have to pass the main window (or any of its children) to the ShowDialog method, so it has a proper parent window.

Upvotes: 0

Wowa
Wowa

Reputation: 1811

Are you using threads?

If your threads you have to invoke the parent form, not the msgbox itself.

Form frm = new Form();
if (parent.InvokeRequired)
{
    parent.Invoke(new ThreadStart(() =>
        {
            frm.ShowDialog();
        }));
}
else
{
    frm.ShowDialog();
}

If your working without threads just remove the invoke and explain a little what ShowMessage und msgbox do.

Upvotes: 2

Davide Piras
Davide Piras

Reputation: 44595

What a mess in here!!

is MsgBox your custom type derived from Form class? if so, you can simply call its ShowDialog method to have it displayed modally.

If you can't, please share the content of the ShowMessage method.

Upvotes: 0

Related Questions