0% found this document useful (0 votes)
2 views

IT22-OOP1-Module-2

This document is a module on Object Oriented Programming in C# focusing on the use of MessageBox and textboxes in Windows Forms applications. It covers the creation and manipulation of buttons, setting properties, and various configurations for MessageBox including buttons, icons, and help options. Additionally, it includes practice exercises, resources, and references for further learning.

Uploaded by

Joshua Riana
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

IT22-OOP1-Module-2

This document is a module on Object Oriented Programming in C# focusing on the use of MessageBox and textboxes in Windows Forms applications. It covers the creation and manipulation of buttons, setting properties, and various configurations for MessageBox including buttons, icons, and help options. Additionally, it includes practice exercises, resources, and references for further learning.

Uploaded by

Joshua Riana
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

IT22-OOP1 (Object Oriented Programming 1)

PAMBAYANG DALUBHASAAN NG MARILAO


College of Computer Studies

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.

Object(Buttons and II. OBJECTIVES


1. Create and manipulate MessageBox, Labels
Textboxes) and Buttons
2. Distinguish the different use of Buttons
3. Design and create a c# application that
uses Buttons and messagebox

IV. LESSON PROPER


C# Buttons and MessageBox
Creating a C# Button

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

Setting Button Properties

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

Background and Foreground Color of a Button Control

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.

1. // Set background and foreground


2. dynamicButton.BackColor = Color.Red;
3. dynamicButton.ForeColor = Color.Blue;

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.

The following code snippet creates a simple Message Box.

1. string message = "Simple MessageBox";


2. MessageBox.Show(message);

MessageBox with Title

The following code snippet creates a simple MessageBox with a title.

1. string message = "Simple MessageBox";


2. string title = "Title";
3. MessageBox.Show(message, title);

MessageBox with Buttons

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.

1. string message = "Do you want to close this window?";


2. string title = "Close Window";
3. MessageBoxButtons button = MessageBoxButtons.YesNo;
4. DialogResult result = MessageBox.Show(message, title, buttons);
5. if (result == DialogResult.Yes) {
6. this.Close();
7. } else {
8. // Do something
9. }

MessageBox with Icon

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

MessageBox with Default Button

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.

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, MessageBoxDefaultButton.Button2);
5. if (result == DialogResult.Abort) {
6. this.Close();
7. }
8. elseif(result == DialogResult.Retry) {

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

MessageBox with Message Options

MessageBoxOptions enumeration represents various options and has the following values.

• ServiceNotification
• DefaultDesktopOnly
• RightAlign
• RtlReading

The following code snippet creates a MessageBox with various options.

1. DialogResult result = MessageBox.Show(message, title, buttons,


2. MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2,
3. MessageBoxOptions.RightAlign|MessageBoxOptions.RtlReading);

MessageBox with Help Button

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.

1. DialogResult result = MessageBox.Show(message, title,


2. buttons, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1, 0, "
helpfile.chm");

V. PRACTICE EXERCISES/ACTIVITIES
VI. ADDITIONAL RESOURCES
Microsoft Visual C# Step by Step, Ninth Edition by John sharp

Learn C3 in One Day and Learn it Well by Jamie Chan

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

You might also like