Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
480
UltraMessageBox location
posted

Hi,

  Is it possible to set the location of the UltraMessageBox on the screen? The Windows version of messageBox always appears in the center of the screen. is it possible to set the location to be the center of the parent window?

Thanks

Parents
  • 6158
    Verified Answer
    Offline posted

    Hello,

    Unfortunately as the UltraMessageBox is not an exposed dialog, there is no simple way to center the dialog to the invoking form. At this time, the UltraMessageBox is manually centered to the screen. If you would like to see the ability to specify the start location of the UltraMessageBox, I would suggest entering a feature request to possibly have this functionality added in a future release.

    It is possible to implement this functionality on your own. Due to the protection level of the UltraMessageBox ("MessageBoxDialog") and its modal state, you will have to handle the Deactivate event on the invoking form and accessing the Form.ActiveForm to get a reference to the dialog. However, you will have to delay accessing of the Form.ActiveForm property until after the Deactivate event completes using BeginInvoke().

    Try the following code:

            private void Form1_Deactivate(object sender, EventArgs e)
            {
                this.BeginInvoke(new MethodInvoker(MoveMessageBox));
            }
    
            private void MoveMessageBox()
            {
                Form f = Form.ActiveForm;
                if (f != null &&
                    f.GetType().Name == "MessageBoxDialog" &&
                    f.Owner != null)
                {
                    Rectangle ownerBounds = f.Owner.Bounds;
                    Size messageBoxSize = f.Size;
                    Point centerOfOwner = new Point(ownerBounds.Left + ownerBounds.Width / 2, ownerBounds.Top + ownerBounds.Height / 2);
                    Point offsetLocation = new Point(centerOfOwner.X - messageBoxSize.Width / 2, centerOfOwner.Y - messageBoxSize.Height / 2);
    
                    f.Location = offsetLocation;
                }
            }
    

     

    Let me know if you need any further assistance.

    Chris

     

Reply Children