程序代写代做代考 Application Development with .NET (31927) .NET Application Development (32998)

Application Development with .NET (31927) .NET Application Development (32998)
Lecture 8: Windows Forms in C#: Part – 2

Outline
• MessageBox in details • With buttons
• With Icons
• Dialogs:
• File Open
• Save, etc.
• Handling Multiple Forms
• ToString() method and overriding • Generics introduction

MessageBox with Buttons
• MessageBox.Show(Over20overloadedoptions)
• Will display message on the screen in a dialog box. User clicks OK to close box. Useful for
displaying debugging messages, etc.
• MessageBoxButtons is use to specify which buttons to display.
• The buttons available are show below:
Available button combination

MessageBox with Buttons
• To check the user input/selection DialogResult is used
• DialogResult is an enum which Specifies identifiers to indicate the return value of a dialog box
on the button clicked.

MessageBox with Buttons

MessageBox with Buttons

MessageBox with Icons
• MessageBoxIcon is use to specify which icon to display.
• MessageBoxIcon is an enum which Specifies the icon o display.
Available button combination

MessageBox with Icons

MessageBox with Icons

MessageBox with Icons

Common Dialogs boxes:
• Dialog boxes are type of windows, which is used to initiate communication or dialog between a computer and its user.
• A dialog box is most often used to implement a command or to respond to a question.
• Windows.Form is a base class
• Dialog boxes are of two types, which are given below.
• Modal dialog box: Temporarily halts the application and the user cannot continue until the dialog has been closed/completed
• Modeless dialog box: Used when the requested information is not essential to continue and the dialog can be left open while continuing the work.
Reference: https://www.c-sharpcorner.com/article/dialog-boxes-in-c-sharp/

Common Dialogs boxes:
• Common Dialogs boxes: The dialog boxes which are common to all windows applications.
• Performs common tasks such as open a file, save a file, selecting font etc.
• Steps to use Common Dialog boxes:
1. Create an instance of the required common dialog box, 2. Set the properties as required,
3. Call the ShowDialog() method to invoke it.
ShowDialog() returns a the enum called DialogResult. Reference: https://www.c-sharpcorner.com/article/dialog-boxes-in-c-sharp/

Common Dialogs boxes:
1. OpenFileDialog: Allows to choose a file to be opened in an application.
Reference: https://www.c-sharpcorner.com/article/dialog-boxes-in-c-sharp/

Common Dialogs boxes:
2. SaveFileDialog: Allows to choose a file to be opened in an application.
Reference: https://www.c-sharpcorner.com/article/dialog-boxes-in-c-sharp/

Common Dialogs boxes:
3. FontDialogBox: Allows the user to select font settings.

Common Dialogs boxes:
4. ColorDialog: Allows the user to select a color.

Handling Multiple Forms
• Majority of the Windows applications uses multiple forms.
• Applications usually have a main form which loads at the start up
• Other forms are accessible from the main form based on the program requirement.
• Example:
• Main Form: Login Form
• Other forms: Profile pages, data entry page etc.

Handling Multiple Forms – Example
ChildForm
MainForm
About Form
Show About form
Logout

ToString() method and overloading
• What is ToString(): Returns a String Representation of the current object
• Object.ToString is the major formatting method in the .Net Framework
• System.Object is the base class of all reference in the .Net Framework.
• Through inheritance, behaviors/methods in the System.Object class are also available to every type .Net FrameWork.
• Hence ToString() method is available to all the types in the .Net Framework.

ToString() method and overriding
Example:
Output

ToString() method and overriding
Example:
Output

Generics
• Allows to design classes and methods decoupled from data types
• Generic classes are widely used by the collection classes available in
the System.Collections.Generic namespace.
• Generics allow us to create a parameterized type
• They allow us to create code that is independent of the specific type and rely on the properties of the type.
• Particularly useful for cases where there are multiple sections of code performing the same duty but on different data types

Generics
Task: Check whether two values are equal
This solution works for integer values only!
Can it work for string or other type?
Output
Example: Version1

Generics
Task: Check whether two values of any type are equal
This solution works! And can be used with any types
Issues:
1. All type are inherited from System.Object
2. Performance degradation due to boxing and unboxing
3. Its is not strongly typed any more!
Output
Example: Version2

Generics
Task: Check whether two values of any type are equal
This solution works and solves the previous issues!
Example: Version3
Output