C# School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become a C# Expert!

Join 300,458 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 1,632 people online right now. Registration is fast and FREE... Join Now!




MessageBoxes - how to use them

 
Reply to this topicStart new topic

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

b.ihde
Group Icon



post 15 Dec, 2008 - 09:24 AM
Post #1


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:
  1. OK
  2. OK, Cancel
  3. Yes, No
  4. Yes, No, Cancel
  5. Retry, Cancel
  6. 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.
CODE

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.
CODE

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)
CODE

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:
CODE

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 smile.gif


Show(String)
in example:
CODE

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

Show(String, String)in example:
CODE

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:
CODE

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. smile.gif
So, you are also able to code:
Show(String, String, MessageBoxButtons, MessageBoxResult)
CODE

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 smile.gif

Kindly regards

Benjamin Ihde
Go to the top of the page
+Quote Post


Register to Make This Ad Go Away!

Zooms
**



post 20 Dec, 2008 - 09:31 AM
Post #2
Great guide! Perfect for those who just started learning C# (me)

tongue.gif
Go to the top of the page
+Quote Post

b.ihde
Group Icon



post 22 Dec, 2008 - 01:56 AM
Post #3
Thank you very much!
I am happy that you like it biggrin.gif

Go to the top of the page
+Quote Post

vikramjit
*



post 25 Dec, 2008 - 10:22 PM
Post #4
good notes . i will add them in my e-books
Go to the top of the page
+Quote Post

ne7ven
*



post 12 Apr, 2009 - 07:57 PM
Post #5
i use Microsoft Visual Studio 2005, want to use MessageBox class, which package i need to using ?
Go to the top of the page
+Quote Post

Core
Group Icon



post 12 Apr, 2009 - 08:07 PM
Post #6
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.
Go to the top of the page
+Quote Post


Fast ReplyReply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 


Lo-Fi Version Time is now: 11/8/09 02:09AM

Live C# Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

C# Tutorials

Reference Sheets

C# Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month