Page 1 of 1

MessageBoxes - how to use them How to handle MessageBoxes and their results

#1 b.ihde  Icon User is offline

  • D.I.C for life
  • member icon

Reputation: 44
  • View blog
  • Posts: 1,070
  • Joined: 29-September 08

Posted 15 December 2008 - 10:24 AM

Welcome the this tutorial about MessageBoxes

In this tutorial iíll show how to create a MessageBox with different buttons and icons.
At first letís see what different kind of buttons exist:
  • OK
  • OK, Cancel
  • Yes, No
  • Yes, No, Cancel
  • Retry, Cancel
  • Abort, Retry, Ignore
And now what different kind of icons exist:
Attached Image Error
Attached Image Information
Attached Image Question
Attached Image Warning

A messagebox can be shown to show the user an information, or to let the user make a decision.

Per example: An easy information message would be ďText savedĒ with an Ok button.
A messagebox with a dicision to make would be ď Do you really want to quit?Ē with an Yes and No button.

Retrun the value of a messagebox:

If the user clicks a button, the program gets a DialogResult.

Here are the different return values and the buttons for the values:
  • OK --- DialogResult.OK
  • Yes --- DialogResult.Yes
  • No --- DialogResult.No
  • Abort --- DialogResult.Abort
  • Retry --- DialogResult.Retry
  • Cancel --- DialogResult.Cancel
  • Ignore --- DialogResult.Ignore


So letís create some messageboxes:
( to show a messagebox use the Show() method )

We create a simple messagebox with a text, caption, Ok button and an information symbol:

At the beginning of every messagebox there is a event, in this case it is a button

Letís create a button which should show a welcome massage.. double click on the button to insert a code.
There are 2 ways to write the code for a MessageBox:

1.
public void button1_Click(object sender, EventArgs e)
{
	  MessageBox.Show("hello and welcome!", "welcome", MessageBoxButtons.OK, MessageBoxIcon.Information)
}


As we see the structure of this messagebox is the following:
MessageBox.Show("text", "caption", "buttons", "icon")

2.
public void button1_Click(object sender, EventArgs e)
{
string message = "hello and welcome"; // Message-Text
string caption = "welcome"; // TitleBar Text
MessageBoxButtons buttons = MessageBoxButtons.OK;
MessageBoxIcon icon = MessageBoxIcon.Information;

MessageBox.Show(message, caption, buttons, icon)
}



If we have more than one button we can set a defaultbutton.

In an example it would look like: (yes no - buttons)
public void button1_Click(object sender, EventArgs e)
{
string message = "Do you really want to quit?";
string caption = "exit";
MessageBoxButtons buttons = MessageBoxButtons.YesNo;
MessageBoxIcon icon = MessageBoxIcon.Question;
MessageBoxDefaultButton defaultbutton = MessageBoxDefaultButton.Button2;

MessageBox.Show(message, caption, buttons, icon, defaultbutton)
}



MessageBoxDefaultButton.Button1 (the first button) Yes is default
MessageBoxDefaultButton.Button2 (the second button) No is default

Now we look at how to handle a result:

So we create a messagebox with 2 buttons and the user has to decide what to do.
We create a quit-button:
public void button1_Click(object sender, EventArgs e)
   {
string message = "Do you really want to quit?";
string caption = "exit";
MessageBoxButtons buttons = MessageBoxButtons.YesNo;
MessageBoxIcon icon = MessageBoxIcon.Question;
MessageBoxDefaultButton defaultbutton = MessageBoxDefaultButton.Button2;
DialogResult result;

result = MessageBox.Show(message, caption, buttons, icon)

if (result == DialogResult.Yes)
   {
Close();
   }
}



If there is no definied event on a dialogresult, the messagebox will always close and nothing happens. So we donít need to write an event for DialogResult.No in this example.

Now we can create a MessageBox with text, caption, buttons, icon and know how to get a result and how to handle the result.

At least we talk about the different overloaded Show methods

We already used 3 overloaded Show methods in this tutorial :)


Show(String)
in example:
public void button1_Click(object sender, EventArgs e)
{
string message = "Welcome";
MessageBox.Show(message);
}


Show(String, String)in example:
public void button1_Click(object sender, EventArgs e)
{
string message = "Hello and welcome!";
string caption = "Welcome";
MessageBox.Show(message, caption);
}


Show(String, String, MessageBoxButton)
in example:
public void button1_Click(object sender, EventArgs e)
{
string message = "Hello and welcome";
string caption = "Welcome";
MessageBoxButtons buttons = MessageBoxButtons.OKCancel;
MessageBox.Show(message, caption, buttons);
}



Show(String, String, MessageBoxButton, MessageBoxIcon)
This is the first example we used.

Show(string, string, MessageBoxButton, MessageBoxIcon, MessageBoxDefaultButton)
This is the second example we used.

Show(String, String, MessageBoxButton, MessageBoxIcon, MessageBoxDefaultButton , MessageBoxResult)
This is the third example we used.

Of course you don't have to use MessageBoxIcon and MessageBoxDefaultButton to use MessageBoxResult!
You can mix it a little bit, but for MessageBoxResult, of course, you need min. 2 buttons. :)
So, you are also able to code:
Show(String, String, MessageBoxButtons, MessageBoxResult)
public void button1_Click(object sender, EventArgs e)
   {
string message = "Message - Text"; // Message
string caption = "Title - Text"; // Caption
MessageBoxButtons buttons = MessageBoxButtons.YesNo;
MessageBoxDefaultButton defaultbutton = MessageBoxDefaultButton.Button2;
DialogResult result;

result = MessageBox.Show(message, caption, buttons)

if (result == DialogResult.Yes)
   {
// Event here
   }
}




Thank you very much for reading :)

Kindly regards

Benjamin Ihde

Is This A Good Question/Topic? 3
  • +

Replies To: MessageBoxes - how to use them

#2 Zooms  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 78
  • Joined: 04-December 08

Posted 20 December 2008 - 10:31 AM

Great guide! Perfect for those who just started learning C# (me)

:P
Was This Post Helpful? 0
  • +
  • -

#3 b.ihde  Icon User is offline

  • D.I.C for life
  • member icon

Reputation: 44
  • View blog
  • Posts: 1,070
  • Joined: 29-September 08

Posted 22 December 2008 - 02:56 AM

Thank you very much!
I am happy that you like it :D
Was This Post Helpful? 0
  • +
  • -

#4 vikramjit  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 24-December 08

Posted 25 December 2008 - 11:22 PM

good notes . i will add them in my e-books
Was This Post Helpful? 0
  • +
  • -

#5 ne7ven  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 12-April 09

Posted 12 April 2009 - 08:57 PM

i use Microsoft Visual Studio 2005, want to use MessageBox class, which package i need to using ?
Was This Post Helpful? 0
  • +
  • -

#6 Core  Icon User is offline

  • using System.Linq;
  • member icon

Reputation: 774
  • View blog
  • Posts: 5,097
  • Joined: 08-December 08

Posted 12 April 2009 - 09:07 PM

Quote

i use Microsoft Visual Studio 2005, want to use MessageBox class, which package i need to using ?


The MessageBox class is ready to be used in every new Windows Forms application, since it is a member of the System.Windows.Forms namespace.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1