Morgan Peters
Morgan Peters

Reputation: 33

Show MessageBox immediately in Windows Forms?

Is there any way to have a messagebox immediately pop up when a form opens? I just want to display a short message about how to use the form when it opens. I tried

private void myForm_Load(object sender, EventArgs e)
{
    DialogResult dialogOpen = MessageBox.Show("Use the navigation menu to get started.", "Welcome!", MessageBoxButtons.OK);
}

but it doesn't work.

Upvotes: 0

Views: 6193

Answers (5)

KirilStankov
KirilStankov

Reputation: 87

Form_Load event occurs before the form is really visible. I use:

static private bool splashShown = false;
    private void Form1_Activated(object sender, System.EventArgs e)
    {
        if (!splashShown)
        {
            MessageBox.Show("message");
            splashShown = true;
        }
    }

Upvotes: 0

Christopher Bowler
Christopher Bowler

Reputation: 1

I have used this and it works fine. App start brings up messagebox first before all else.

InitializeComponent();
MessageBox.Show("put your message here");

Upvotes: -1

Zack Campbell
Zack Campbell

Reputation: 125

I don't see why it wouldn't work in Form_Load. Definitely try doing as others have pointed out by putting it beneath form initialization.

Though, given that you're just showing a message box, I don't think there is any reason to store the result, so a simple MessageBox.Show(message); Should do the trick.

As @s.m. said, from a UX point of view, having a notification thrown in your face as soon as the app starts would be very obnoxious, at least if you have it EVERY time. Personally, I would create a boolean Settings variable, set it to true the first time the message is displayed, and only display it when the setting is false, i.e. the first time the message is displayed.

private boolean splashShown = Properties.Settings.Default.splashShown;

private void Form_Load(object sender, EventArgs e)
{
    if (!splashShown)
    {
        MessageBox.Show("message");
        myForm.Properties.Settings.Default.splashShown = true;
        myForm.Properties.Settings.Default.Save();
    }
}

And set up the splashShown Setting in your form properties.

If the problem is that your Form_Load() method isn't actually attached to your Form.Load() event, you can double click the form window in the designer and it will automatically created the Form_Load() base method for you and attach it to the Form.Load() event

Upvotes: 1

DangerousDetlef
DangerousDetlef

Reputation: 381

Is there a reason to use the Load method of the form? If not you could to it in the constructor of form. If you want it to show up immediately after your form loads, you should do it in the constructor after the form is initialized. It should look something like this:

public partial class myForm : Form
{
    public myForm()
    {
        InitializeComponent();

        DialogResult dialogOpen = MessageBox.Show("Use the navigation menu to get started.", "Welcome!", MessageBoxButtons.OK);
    }
}

The constructor (public myForm()) and the InitializeComponent(); should be automatically added to the form by Visual Studio after creating it.

Upvotes: 0

Rajeev Goel
Rajeev Goel

Reputation: 1523

Showing a MessageBox during Form_Load works just fine for me. I literally copy/pasted the code from your original post, and it worked. I'm on .NET Framework 4.5 on Windows 8.1.

Are you sure your Load event handler is getting called? Perhaps the it's not hooked up to the Load event properly.

Upvotes: 4

Related Questions