Frank
Frank

Reputation: 87

How to set a custom position for a MessageBox

I want to display a MessageBox at a given position. I don't want the MessageBox to overlay and hide a section of the parent form. Is there a way using the default MessageBox object to specify a custom location?

I'm using C# in VS 2008 with .Net Framework 3.5

Upvotes: 1

Views: 17101

Answers (2)

clamchoda
clamchoda

Reputation: 4941

You can not change the attributes of the message box. You will have to create your own form() and use it as a message box. This way you can define everything about it (position, size, etc)

Here's one that I've done.

public partial class NotifyForm : Form
    {
        public NotifyForm(string message, string title)
        {
            InitializeComponent();
            this.StartPosition = FormStartPosition.CenterScreen;// Or wherever 

            lblMessage.Text = "\r\n" + message;
            this.Text =  title;
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }

And I Initialize it like this:

NotifyForm frm = new NotifyForm("Added item: " + txtAddAcc.Text + " to Accessibility Categories list.", "Add Item!");
frm.Show();

Upvotes: 7

Sergey K
Sergey K

Reputation: 4114

You can create own custom MessageBox from From and show it as DialogBox in this way you can set the startup location. For example:

   var form = new Form
                       {
                           StartPosition = FormStartPosition.Manual, 
                           ShowInTaskbar = false,
                           Location = new Point(100,100)
                       };
        form.ShowDialog();

Upvotes: 3

Related Questions