web analytics

How to Use MessageBox in Windows Forms Applications?

Options

codeling 1595 - 6639
@2016-01-18 19:42:42

Setting the Default Button In a MessageBox Dialog Window

You want to show a dialog box to the user in your Windows Forms program and then get the result of the user's response to this dialog and act on it. The MessageBox class in Windows Forms is ideal for this purpose, and can solve the problem very quickly.

To display a message box, call the static method MessageBox.Show. The title, message, buttons, and icons displayed in the message box are determined by parameters that you pass to this method.

When you use the following code to show a MessageBox dialog window  in a Windows Forms application, the default button will be the Yes Button:

DialogResult dr = MessageBox.Show(string.Format("Are you sure you want to delete the data source (HP) ?", "Information", MessageBoxButtons.YesNo, MessageBoxIcon.Information);

 

If you want to set the No button as the default button, you have to call the MessageBox.Show() method using the following format. The last parameter MessageBoxDefaultButton is used to specify the default button for the message box

DialogResult dr = MessageBox.Show(string.Format("Are you sure you want to delete the data source (HP) ?", "Information", MessageBoxButtons.YesNo, MessageBoxIcon.Information, MessageBoxDefaultButton.Button2);

@2016-01-24 10:08:36

How to display message text in multiple lines?

You can  insert a new line your message tex to display it in multiple lines:

MessageBox.Show("Hello" + Environment.NewLine + "World");
@2016-01-25 09:39:55

How to call MessageBox.Show() from a separate thread?

I am writing a multi-thread program in C# .NET in VS2008 and try to call MessageBox.Show method to display a message box in the background thread, see the following code:

//  start the updater on a separate thread so that our UI remains responsive
_autoUpdaterThread = new Thread(new ThreadStart(CheckLatestVersion));
_autoUpdaterThread.Start();

.....

private void CheckLatestVersion()
{
            try
            {
                .....

                if (string.Compare(latestVersion, curVersion) > 0)
                {
                    DialogResult result = DialogResult.None;

                    result = MessageBox.Show(this, message, title, MessageBoxButtons.YesNo, MessageBoxIcon.Information);

                }
            }
            catch (Exception ex)
            {
            }    
}

When I run my application, a System.InvalidOperationException is thrown with the following information:

Cross-thread operation not valid: Control 'MainForm' accessed from a thread other than the thread it was created on.

Solution

The "this" on the following line in your code is a reference to MainForm. That is why I am getting a cross thread exception.

MessageBox.Show(this, ....

To fix this problem, just use Control.Invoke() to make it modal to the main thread.

//  start the updater on a separate thread so that our UI remains responsive
_autoUpdaterThread = new Thread(new ThreadStart(CheckLatestVersion));
_autoUpdaterThread.Start();

.....

private void CheckLatestVersion()
{
    try
    {
        .....

        if (string.Compare(latestVersion, curVersion) > 0)
        {
            DialogResult result = DialogResult.None;

            result = ShowMessage(message, title, MessageBoxButtons.YesNo, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);

        }
    }
    catch (Exception ex)
    {
    }   
}

protected delegate DialogResult ShowMessageDelegate(string message, string title, MessageBoxButtons buttons, MessageBoxIcon icon, MessageBoxDefaultButton defaultButton);

protected DialogResult ShowMessage(string message, string title, MessageBoxButtons buttons, MessageBoxIcon icon, MessageBoxDefaultButton defaultButton)
{
    DialogResult rtn = DialogResult.None;

    if (this.InvokeRequired)
    {
        ShowMessageDelegate d = new ShowMessageDelegate(ShowMessage);
        rtn = (DialogResult)this.Invoke(d, new object[] { message, title, buttons, icon, defaultButton });
    }
    else
    {
        rtn = MessageBox.Show(this, message, title, buttons, icon, defaultButton);
    }

    return rtn;
}

@2016-02-13 21:44:06

I encountered the same issue when I tried to show a message in a thread. The following exception was thrown:

An unhandled exception of type 'System.InvalidOperationException' occurred in System.Windows.Forms.dll

Additional information: Cross-thread operation not valid: Control 'PageForm' accessed from a thread other than the thread it was created on.

The above solution helps me solve the issue.

Comments

You must Sign In to comment on this topic.


© 2024 Digcode.com