Custom Search

Friday, June 27, 2008

MessageBox Tuning in C#

MessageBox class is used when you want to show some kind of information or when the application needs user intervention.
This class contains static methods that help to interactuate with users. All you have to do is set the correct parameters of the show method. Some are: title, a proper message, icons, botons, quantities and more.

The show method returns a DialogResult type value that will need to be evaluated in order to make future actions, for example, open another dialog window.

Let’s look at some examples now:

I´ll start by creating a Windows From. Next, I´ll add 5 Buttons controls to the form and create a click event for all of them. On the click event method I created different messageBoxs for each one. Let’s analize them:

-For the button0, the code line is:

MessageBox.Show(this, "Hello", "Title Button0");

Nothing really complicated here. This creates a simple dialog with the text Hello, owner this and title Title Button0.

-For the button1, the code line is:

MessageBox.Show("Abort, Retry, Ignore example", "Title 1", MessageBoxButtons.AbortRetryIgnore);

This is a little more interesting. We indicated here that the title would be Title 1, the message Abort, Retry, Ignore example and the Button for this specific dialog is the AbortRetryIgnore Button, the same one you see in most windows applications.

-For the button2, the code line is:

MessageBox.Show(this, "Error on button2, Continue?" , "Title2", MessageBoxButtons.YesNo , MessageBoxIcon.Error);

This is very similar to the previus one: Owner this, title Title2, message Error on button2, Continue? , Button YesNo and Icon Error. This will create a dialog that contains both options Yes and No, and also an Error Icon located left of the message will appear.

-For the button3, the code line is:

MessageBox.Show("Warning", "Button3 Title", MessageBoxButtons.OK, MessageBoxIcon.Warning);

Similar to the others but now the Button is the Ok button and the Icon Warning will apear. So the purpose of this is to indicate an error, warning or a message that the user must see and give the only option of clicking the OK button.

-For the button4, the code line is:

MessageBox.Show(this, "Warning", "Button4 title", MessageBoxButtons.RetryCancel, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button3);

Here the Button is the usual RetryCancel, the Icon Asterisk and the DefautlButton indicated to Button3. This defines the constat button.

No comments: