Click here to Skip to main content
15,884,744 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
C#
In my application when the user clicks on a label to open a form the form opens in the background.I mean it does not come to the front but is rather placed on taskbar. once the user cancels that instance of form and again clicks on the label,the form opens in foreground.Following portion of code does the specific work.


C#
if (DataFormDlg.Instance.InvokeRequired)
       {
           DataFormDlg.Instance.BeginInvoke(
               new ShowDataFormDelegate(ShowDataForm), pageId, timeout);
           return;
       }

       DataFormDlg.Instance.CurrentPageId = pageId;
       DataFormDlg.Instance.Timeout = timeout;

       if (!DataFormDlg.Instance.Visible)
           DataFormDlg.Instance.ShowDialog();
       else
           DataFormDlg.Instance.Focus();


C#
Here the DataFormDlg is derived from windows form.


What I have tried:

I have heard that BringToFront() and Activate() might help but not sure on how to use them.
Posted
Updated 23-Jul-16 6:45am
Comments
Ravi Bhavnani 23-Jul-16 12:02pm    
Try DataFormDlg.Instance.TopMost = true;

/ravi
Philippe Mori 23-Jul-16 12:32pm    
You almost never need that... If it does not work, then something wrong was done like not setting the owner.
Philippe Mori 23-Jul-16 12:31pm    
If you correctly specify the appropriate owner and don't mess with forms' enabling, then you should never have such problem!

If a form is in the Task bar then you have probably don't set the owner nor adjust form properties.

In fact, it is probably harder to make it not work properly as normally it just works as expected.
BillWoodruff 23-Jul-16 17:33pm    
Please add a specific description of how you are using threading in your code. What is the Main Form, and how does it launch other threads ?

The only time I've seen this happen is when you're creating and showing a form (with .Show instead of .ShowDialog) from a Dialog form. The dialog will always be on top until it's dismissed. Then your newly created form will have the focus and be on top.
 
Share this answer
 
Comments
Philippe Mori 23-Jul-16 12:47pm    
... and probably without the owner being properly set to an active and enabled form.
You code look suspicious...

Why would you create a form from an arbitrary thread?

Why a model form would be already visible?

What is Instance property (or what is the type of DataFormDlg?).

Where is the code that call DataFormDlg constructor?

In most application, you should never need to use BringToFront(), Activate(), Focus() or TopMost. Obviously, you make simple things very complex.

Usually, you simply do something like this and it works:
C#
void ParentForm_ShowDialogClicked(object sender, EventArgs args)
{
    using (var form = new DataFormDlg())
    {
        form.ShowDialog(this);    // or ParentForm for owner if inside a UserControl.
    }
}
 
Share this answer
 
v2
Comments
Ravi Bhavnani 23-Jul-16 14:54pm    
The OP's code seems to imply there's a singleton dialog that is shown when an event occurs in another thread, hence the check for InvokeRequired. The dialog seems to auto-dismiss after a timeout. For this reason, I suggested making the form TopMost (after making it visible) when it needs to be shown.

The TopMost property is the easiest way to ensure an app is "always" visible on the desktop.

/ravi
Philippe Mori 23-Jul-16 15:46pm    
TopMost should almost never be used as it cause a form to appear above other application forms even if the other application is activated.

This could be used by program like the Task Manager so that it can be above other application and even then, it should be optional.

In any case, it does not make sense to have a singleton to a modal dialog. A modal dialog should only exist while it is displayed.
Ravi Bhavnani 23-Jul-16 16:12pm    
Of course it's not common, but some apps (like screenshot takers and Task Manager) have a valid reason to always be on top. Note, if 2 apps both have TopMost set, the one that is most recently clicked will be on top of the other one. Of course, a dev could abuse this feature by setting TopMost in a timer event which (unless they have a very good reason) isn't recommended.

> it does not make sense to have a singleton to a modal dialog.
I agree.

/ravi

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900