Anda di halaman 1dari 5

4/26/12

Microsoft Visual C# - Message Boxes

Message Boxes
Introduction
A message box is a special dialog box used to display a piece of information to the user. As opposed to a regular form, the user cannot type anything in the dialog box. The .NET Framework inherently supports message boxes through its own MessageBox class. Besides this, you can also use functions from either the Visual Basic or the Win32 libraries.

The Return Value of a Message Box


Besides displaying a message, a message box is may be meant to let the user make a decision by clicking a button and, depending on the button the user would have clicked, the message box would return a value. The value returned by a message box corresponds to the particular button the user would have clicked (on the message box). The return values are defined in the DialogResult enumeration. The buttons and the returned values are as follows: If the User Clicks The Method Returns DialogResult.Abort DialogResult.Cancel DialogResult.Ignore DialogResult.No DialogResult.OK DialogResult.Retry DialogResult.Yes

The Message of a Message Box


The .NET Framework provides the MessageBox class function used to easily create a message box. To display a simple message with just an OK button, you can call the Show() static method of this class. Its syntax is as follows: pbi sai DaoRsl Msaeo.hwsrn msae; ulc ttc ilgeut esgBxSo(tig esg) In this case, the message to display must be passed as a string to the Show() method. Here is an example: pbi casPorm ulc ls rga { sai itMi( ttc n an) { Msaeo.hw"ecm t teWneflWrdo Vsa C"; esgBxSo(Wloe o h odru ol f iul #) rtr 0 eun ; } } This would produce:

www.functionx.com/vcsharp/general/messagebox.htm

1/5

4/26/12

Microsoft Visual C# - Message Boxes

The message to display can be made of up to 1024 characters. To display the message on multiple lines, you can use the new line escape sequence anywhere inside the string.

The Caption of a Message Box


In reality, the MessagBox.Show() method is overloaded with various versions. Another version is: pbi sai DaoRsl So(tigtx,srn cpin; ulc ttc ilgeut hwsrn et tig ato) This version allows you to specify a custom caption for the message box. With this version, the first argument is the string that the user will see displaying on the message box. You can pass it as a string. You can also create it from other pieces of strings. The second argument, caption, will be the sentence to display in the title bar of the message box. Here is an example: pbi casPorm ulc ls rga { sai itMi( ttc n an) { Msaeo.hw"ecm t teWneflWrdo Vsa C" esgBxSo(Wloe o h odru ol f iul #, "iulC Ttras) Vsa # uoil"; rtr 0 eun ; } } This would produce:

The Buttons of a Message Box


Another version of the MessageBox.Show() method is as follows: pbi sai DaoRsl So(tigtx, ulc ttc ilgeut hwsrn et srn cpin tig ato, MsaeoBtosbtos; esgBxutn utn) This version allows you to display one or more buttons on the message box. The available buttons are defined through the MessageBoxButtons enumeration. Its members are: MessageBoxButtons OK OKCancel YesNo YesNoCancel RetryCancel AbortRetryIgnore To use any of these combinations of buttons, call the MessageBoxButtons enumeration and access the desired combination. Here is an example: pbi casPorm ulc ls rga { sai itMi( ttc n an)
www.functionx.com/vcsharp/general/messagebox.htm 2/5

Display

4/26/12

sai itMi( ttc n an) { Msaeo.hw"ecm t teWneflWrdo Vsa C" esgBxSo(Wloe o h odru ol f iul #, "iulC Ttras, Vsa # uoil" MsaeoBtosOCne) esgBxutn.Kacl; rtr 0 eun ; }

Microsoft Visual C# - Message Boxes

} This would produce:

The Icon of a Message Box


This version allows you to display an icon. The possible icons are available through the MessageBoxIcon enumeration. The members of this enumerator are: MessageBoxIcon None Asterisk Error Exclamation Hand Information Question Stop Warning Here is an example: pbi casPorm ulc ls rga { sai itMi( ttc n an) { Msaeo.hw"orodrapast b cret + esgBxSo(Yu re per o e orc" "nr yurayt poieyu cei cr ifrain" \Ae o ed o rvd or rdt ad nomto?, "utmrOdrPoesn" Csoe re rcsig, MsaeoBtosYsoacl esgBxutn.eNCne, MsaeoIo.nomto) esgBxcnIfrain; rtr 0 eun ; } } This would produce: Description

www.functionx.com/vcsharp/general/messagebox.htm

3/5

4/26/12

Microsoft Visual C# - Message Boxes

The Default Button of a Message Box


When a message box is configured to display more than one button, the operating system is set to decide which button is the default. The default button has a thick border that sets it apart from the other button(s). If the user presses Enter, the message box would behave as if the user had clicked the default button. If the message box has more than one button, you can decide what button would be the default. To specify the default button, the MessageBox.Show() method provides the following version: pbi sai DaoRsl So(tigtx, ulc ttc ilgeut hwsrn et srn cpin tig ato, MsaeoBtosbtos esgBxutn utn, MsaeoIo io, esgBxcn cn MsaeoDfututndfututn; esgBxealBto ealBto) Based on this, you can specify the default button using the last argument that provides values through the MessageBoxDefaultButton enumerator whose values are: Button1: The left button will be the default. Here is an example: pbi casPorm ulc ls rga { sai itMi( ttc n an) { Msaeo.hw"orodrapast b cret + esgBxSo(Yu re per o e orc" "nr yurayt poieyu cei cr ifrain" \Ae o ed o rvd or rdt ad nomto?, "utmrOdrPoesn" Csoe re rcsig, MsaeoBtosYsoacl esgBxutn.eNCne, MsaeoIo.nomto, esgBxcnIfrain MsaeoDfututnBto1; esgBxealBto.utn) rtr 0 eun ; } }

Button2: If the message box displays two buttons, the right button will be the default. If the message box displays three buttons, the middle button will be the default. Here is an example: pbi casPorm ulc ls rga { sai itMi( ttc n an) { Msaeo.hw"orodrapast b cret + esgBxSo(Yu re per o e orc" "nr yurayt poieyu cei cr ifrain" \Ae o ed o rvd or rdt ad nomto?, "utmrOdrPoesn" Csoe re rcsig, MsaeoBtosYsoacl esgBxutn.eNCne, MsaeoIo.nomto, esgBxcnIfrain MsaeoDfututnBto2; esgBxealBto.utn) rtr 0 eun ; } }

Button3: The right button will be the default. Here is an example:


www.functionx.com/vcsharp/general/messagebox.htm 4/5

4/26/12

Microsoft Visual C# - Message Boxes

pbi casPorm ulc ls rga { sai itMi( ttc n an) { Msaeo.hw"orodrapast b cret + esgBxSo(Yu re per o e orc" "nr yurayt poieyu cei cr ifrain" \Ae o ed o rvd or rdt ad nomto?, "utmrOdrPoesn" Csoe re rcsig, MsaeoBtosYsoacl esgBxutn.eNCne, MsaeoIo.nomto, esgBxcnIfrain MsaeoDfututnBto3; esgBxealBto.utn) rtr 0 eun ; } }

Home

Copyright 2007 FunctionX, Inc.

www.functionx.com/vcsharp/general/messagebox.htm

5/5

Anda mungkin juga menyukai