I have a class library that holds a "MessageBox" equivalent with a few more bells and whistles.
If I call the ShowDialog(IWin32Owner)
method, this works, and the form will display in the centre of my parent form.
Sometimes, however, this form is invoked from a class in my project, and so I don't have access to the form owner. In this situation, I can pass null
to the ShowDialog()
method, however it appears this doesn't recognize the "Currently Active Window" and display it in the centre. I am assuming because it is in another class library.
Is there any way then to get the currently active form (or at least the screen) the user is working on?
EDIT
Ok this is more to do with the FormStartPosition Enumeration.
If I use CentreScreen
this should default to the currently active monitor as per MSDN. However this seems to default to the default monitor if the form is in a class library.
Ok:
This is the code in question: It Fails to set the form to centre screen:
public static DialogResult ShowYesNoCancel(string message)
{
using (frmMessage form = new frmMessage())
{
form.Text = @"Input Required";
form.lblMessage.Text = message;
form.btnNo.Visible = true;
form.btnOK.Text = @"Yes";
form.btnOK.DialogResult = DialogResult.Yes;
form.StartPosition = FormStartPosition.CenterScreen;
return form.ShowDialog();
}
}
A solution:
/// <summary>
/// Overridden to ensure its in the centre of the current screen
/// </summary>
/// <returns></returns>
public new DialogResult ShowDialog()
{
Screen current = Screen.FromPoint(MousePosition);
Rectangle s = current.WorkingArea;
StartPosition = FormStartPosition.Manual;
Location = new Point(s.Left + s.Width / 2 - Width / 2, s.Top + s.Height / 2 - Height / 2);
return base.ShowDialog();
}