IT22-OOP1-Module-2
IT22-OOP1-Module-2
I. INTRODUCTION
C# MessageBox in Windows Forms displays a
message with the given text and action buttons.
You can also use MessageBox control to add
additional options such as a caption, an icon, or
help buttons. On the other hand textboxes accepts
Module 2 input from the user in text form. In this Module,
you'll learn how to display and use a MessageBox
Basic Control or and textbox in C# WinForms app.
To create a Button control, you simply drag and drop a Button control from Toolbox to Form in Visual
Studio. After you drag and drop a Button to a Form, the Button looks like Figure 1. Once a Button is on
the Form, you can move it around and resize it.
Figure 1
Page 1 of 7
IT22-OOP1 (Object Oriented Programming 1)
PAMBAYANG DALUBHASAAN NG MARILAO
College of Computer Studies
After you place a Button control on a Form, the next step is to set button properties.
The easiest way to set a Button control properties is by using the Properties Window. You can open
Properties window by pressing F4 or right click on a control and select Properties menu item. The
Properties window looks like Figure 2.
Figure 2
BackColor and ForeColor properties are used to set background and foreground color of a Button
respectively. If you click on these properties in Properties window, the Color Dialog pops up.
Page 2 of 7
IT22-OOP1 (Object Oriented Programming 1)
PAMBAYANG DALUBHASAAN NG MARILAO
College of Computer Studies
Alternatively, you can set background and foreground colors at run-time. The following code snippet
sets BackColor and ForeColor properties.
C# Message Box
MessageBox class has an overloaded static Show method that displays a message box with a message
and action buttons. The action buttons can be OK and Cancel, Yes and No etc. Here are some of the
options that can be used in C# message box.
Simple MessageBox
The simplest form of a MessageBox is a dialog with a text and OK button. When you click OK button, the
box disappears.
A MessageBox can have different button combinations such as YesNo and OKCancel. The
MessageBoxButtons enumeration represents the buttons to be displayed on a MessageBox and has
following values.
Page 3 of 7
IT22-OOP1 (Object Oriented Programming 1)
PAMBAYANG DALUBHASAAN NG MARILAO
College of Computer Studies
• OK
• OKCancel
• AbortRetryIgnore
• YesNoCancel
• YesNo
• RetryCancel
The following code snippet creates a MessageBox with a title and Yes and No buttons. This is a typical
MessageBox you may call when you want to close an application. If the Yes button is clicked, the
application will be closed. The Show method returns a DialogResult enumeration.
A MessageBox can display an icon on the dialog. A MessageBoxIcons enumeration represents an icon to
be displayed on a MessageBox and has the following values.
• None
• Hand
• Question
• Exclamation
• Asterisk
• Stop
• Error
• Warning
• Information
The following code snippet creates a MessageBox with a title, buttons, and an icon.
Page 4 of 7
IT22-OOP1 (Object Oriented Programming 1)
PAMBAYANG DALUBHASAAN NG MARILAO
College of Computer Studies
1. string message = "Do you want to abort this operation?";
2. string title = "Close Window";
3. MessageBoxButtons buttons = MessageBoxButtons.AbortRetryIgnore;
4. DialogResult result = MessageBox.Show(message, title, buttons, MessageB
oxIcon.Warning);
5. if (result == DialogResult.Abort) {
6. this.Close();
7. }
8. elseif(result == DialogResult.Retry) {
9. // Do nothing
10. }
11. else {
12. // Do something
13. }
We can also set the default button on a MessageBox. By default, the first button is the default button.
The MessageBoxDefaultButton enumeration is used for this purpose and it has the following three
values.
• Button1
• Button2
• Button3
The following code snippet creates a MessageBox with a title, buttons, and an icon and sets the second
button as a default button.
Page 5 of 7
IT22-OOP1 (Object Oriented Programming 1)
PAMBAYANG DALUBHASAAN NG MARILAO
College of Computer Studies
9. // Do nothing
10. }
11. else {
12. // Do something
13. }
MessageBoxOptions enumeration represents various options and has the following values.
• ServiceNotification
• DefaultDesktopOnly
• RightAlign
• RtlReading
A MessageBox can have an extra button called Help button. This is useful when we need to display a
help file. The following code snippet creates a MessageBox with a Help button.
Page 6 of 7
IT22-OOP1 (Object Oriented Programming 1)
PAMBAYANG DALUBHASAAN NG MARILAO
College of Computer Studies
1. DialogResult result = MessageBox.Show(message, title, buttons,
2. MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2,
3. MessageBoxOptions.RightAlign, true );
We can also specify a help file when the Help button is clicked. The following code snippet references a
help file.
V. PRACTICE EXERCISES/ACTIVITIES
VI. ADDITIONAL RESOURCES
Microsoft Visual C# Step by Step, Ninth Edition by John sharp
VII. ASSESSMENT
To be given in separate worksheet.
VIII. REFERENCES
https://www.c-sharpcorner.com/
https://www.w3schools.com/cs/
https://www.tutorialspoint.com/csharp/
Page 7 of 7