Session 1. Bai 1 ve winform

26
Session 1 Trình bày : Võ Ngọc Đạt Email : [email protected] Điện thoại : 0934.969.680 Slide 1 of 38 06/18/22 Windows Forms and Basic Controls

Transcript of Session 1. Bai 1 ve winform

Page 1: Session 1. Bai 1 ve winform

Session 1

Trình bày : Võ Ngọc ĐạtEmail : [email protected]Điện thoại : 0934.969.680

Slide 1 of 38

04/12/23

Windows Forms and Basic Controls

Page 2: Session 1. Bai 1 ve winform

Slide 2 of 26

04/12/23

This module provides an overview of Windows Forms and the basic controls used to create graphical user interfaces.

Module Introduction

A Windows Form is a window used to hold various controls.

These controls are graphical objects that facilitate user interaction to accept data or to display information.

Page 3: Session 1. Bai 1 ve winform

Slide 3 of 38

04/12/23

Windows Forms Windows Forms allows the developer to create user interfaces using various built-in components.

The applications created using Windows Forms are executed on the local machine. The form can access the resources of the local machine such as memory, printer, folders, and files.

The role of Windows Forms include: + Holding controls + Handling user input + Displaying information + Connecting to database, which might exists on another machine

Page 4: Session 1. Bai 1 ve winform

Slide 4 of 38

04/12/23

Windows Forms in .NET Framework

Page 5: Session 1. Bai 1 ve winform

Slide 5 of 38

04/12/23

"Form" Class- The basic unit of an application is a form.

- A form is a container that holds and manages controls and is used to create GUI based Windows applications.

- A Windows Form application will have at least one form (window)

- Create a form in an application by creating an object of the Form class

System.Windows. Form namespace.

Page 6: Session 1. Bai 1 ve winform
Page 7: Session 1. Bai 1 ve winform

Slide 7 of 38

04/12/23Property Description

ForeColor Specifies or retrieves the foreground color.

Location Specifies or retrieves the co-ordinates of the upper left corner of the form.

Modal Specifies or retrieves a value indicating whether the form is shown modally. This means if the user tries to activate another window or form, the user cannot do so without closing the current modal form.

WindowState Specifies or retrieves a value indicating whether a form is minimized, maximized, or is in the normal state.

Method Description

Activate Activates a form and gives it focus.

Focus Puts the focus on a control.

Event Description

Activated Occurs when the form is activated in code or by the user.

Click Occurs when a control in clicked.

Deactivate Occurs when the focus on the form is taken away in code or by the user.

FormClosed Occurs after the form is closed.

GotFocus Occurs when the focus is put on a control of the form.

Resize Occurs when a control on the form is resized.

Validated Occurs when validation is completed by a control. For example, the event is raised after validating the login details in the TextBox control.

Page 8: Session 1. Bai 1 ve winform

Slide 8 of 38

04/12/23

Page 9: Session 1. Bai 1 ve winform

Slide 9 of 38

04/12/23

Lifecycle of Windows Forms

Page 10: Session 1. Bai 1 ve winform

Slide 10 of 38

04/12/23

Types of Controls Microsoft Visual Studio provides different types of controls for Windows Forms programming.

Page 11: Session 1. Bai 1 ve winform

Slide 11 of 38

04/12/23

TypesA control is associated with its respective built-in class defined in the System.Windows.Forms namespace.

For example, the Label control is associated with the Label class.

Each control class consists of various properties, methods, and events

Page 12: Session 1. Bai 1 ve winform

Slide 12 of 38

04/12/23

"Control" ClassThe Control class is the base class of all the controls

The class is defined in System.Windows.Forms namespace and defines various properties, methods, and events.

Page 13: Session 1. Bai 1 ve winform

Slide 13 of 38

04/12/23

Page 14: Session 1. Bai 1 ve winform

Slide 14 of 38

04/12/23Property Description

Controls Retneves a collection of controls contained within the control.

Name Specifies or retrieves the name of a control.

Parent Specifies or retrieves the parent container of the control.

Method Description

Focus Sets input focus to the control.

Hide Hides the control from the user.

Select Activates a control.

Show Displays the control.

Event Description

Click Occurs when a control is clicked.

ControlAdded Occurs when a new control is added to the Control .

Doubleclick Occurs when the control is double-clicked.

GotFocus Occurs when the focus is on the control.

Leave Occurs when the input focus leaves the control.

LostFocus Occurs when the control loses focus.

Move Occurs when the user moves the control.

TextChanged Occurs when the value of the Text property changes.

Page 15: Session 1. Bai 1 ve winform

Slide 15 of 38

04/12/23

"Label" Control- The Label control is a control that is used to show detailed information, which cannot be modified by the user. - The most common purpose of the Label control is to provide captions to the controls

Page 16: Session 1. Bai 1 ve winform

Slide 16 of 38

04/12/23"TextBox" Controls

- The TextBox control takes the required information from the user, which can be modified. For example, you can create a TextBox control to accept the name of the student.

Property Description

CharacterCasing Specifies whether the characters should be converted to uppercase or lowercase.

MaxLength Specifies or retrieves the maximum number of characters that the user can enter in the control.

MultiLine Specifies or retrieves a value indicating whether the user can enter multi-line text.

Name Specifies or retrieves the name of the control.

PasswordChar Specifies or retrieves the special character used to mask characters of a password.

Readonly Specifies or retrieves a value whether the text in the control is editable.

Text Specifies or retrieves the text on the control.

Page 17: Session 1. Bai 1 ve winform

Slide 17 of 38

04/12/23Method Description

AppendText Adds text to the existing text of the control.

Clear Removes text from the control.

Copy Copies the selected text in the control to the Clipboard.

Focus Sets the input focus on the control to enter text.

Paste Replaces the selected text in the control with the text copied to the Clipboard.

Event Description

KeyPress Occurs when the user has finished pressing a key.

Leave Occurs when the control is no longer the active control of the form.

TextChanged Occurs when the value of the Text property changes.

Page 18: Session 1. Bai 1 ve winform

Slide 18 of 38

04/12/23

Property Description

Mask Specifies or retrieves the characters to be used as a mask.

MaskCompleted Retrieves a value indicating whether the user has entered all the required characters into the input mask.

Name Specifies or retrieves the name of the control.

PromptChar Specifies or retrieves the character to be used for prompting the user if the user has missed some input.

Text Specifies or retrieves the text on the control.

"MaskedTextBox" Controls

- The MaskedTextBox control is a new control used to validate user input.

Page 19: Session 1. Bai 1 ve winform

Slide 19 of 38

04/12/23

"Button" Control The Button control provides the easiest way to allow the user to interact with an application. The user can click the button to perform the required action.

Page 20: Session 1. Bai 1 ve winform

Slide 20 of 38

04/12/23

Property Description

DialogResult Specifies or retrieves a value which is sent to the parent form when a button is clicked.

Enabled Specifies or retrieves a value indicating whether the control can be accessed by the user.

FlatStyle Specifies or retrieves the flat style of the control, which can be Flat, standard, Popup, or System.

Image Specifies or retrieves the image on the control.

Name Specifies or retrieves the name of the control.

Text Specifies or retrieves the text on the control.

Method Description

Focus Sets the focus on the control.

PerformClick Triggers a Click event for a button.

Event Description

Click Occurs when the control is clicked.

Doubleclick Occurs when the control is double-clicked.

MouseDoubleClick Occurs when the control is double-clicked using the mouse.

Page 21: Session 1. Bai 1 ve winform

Slide 21 of 38

04/12/23

"ListBox" and "ComboBox" Controls- The ListBox control is a control used to select a single value or multiple values from the given list of values.

- The ComboBox control is similar to the ListBox control, but it allows you to select only one value at a time.

Page 22: Session 1. Bai 1 ve winform

Slide 22 of 38

04/12/23

Property Description

Items Retrieves the items of the control.

SelectionMode Specifies or retrieves the way in which items are selected in the control, which can be:+ None: For not selecting any item+ One: For single selection+ MultiSimple: For multiple selections+ MultiExtended: For multiple selections using the Shift + Mouse click, Shift+ Arrow keys, or Ctrl+ Mouse click.

Selectedlndex Specifies or retrieves the zero-based index number of the selected item in the control.

Text Specifies or retrieves the text of the selected item in the control.

Method Description

ClearSelected Deselects all the selected items in the control.

GetltemText Retrieves the text of the given item.

GetSelected Returns true if the specified item is selected and returns false if the specified item is not selected.

SetSelected Selects or removes the selection for a specified item.

Event Description

SelectedlndexChanged Occurs when the value of the Selectedlndex property is modified.

SelectedValueChanged Occurs when the value of the SelectedValue property is modified.

"ListBox" Controls

Page 23: Session 1. Bai 1 ve winform

Slide 23 of 38

04/12/23

Page 24: Session 1. Bai 1 ve winform

Slide 24 of 38

04/12/23

"ComboBox" ControlsProperty Description

DropDownStyle Specifies or retrieves a value indicating the style of the control. The appearance of the control can be changed to Simple, DropDown, and DropDownList styles.

Items Retrieves the object containing the items of the control.

MaxDropDownItems Specifies or retrieves the maximum number of items to be displayed in the control.

Selectedltem Specifies or retrieves the selected item in the control.

Selectedlndex Specifies or retrieves the zero-based index number of the selected item in the control.

Text Specifies or retrieves the text of the control.

Method Description

GetltemText Gets the text of the given item.

SelectAll Selects the text appearing in the control.

Select Activates a control and is inherited from the Control class.

Event Description

DropDown Occurs when the drop-down portion of the control is displayed.

SelectedlndexChanged Occurs when the value of the selectedlndex property is modified.

SelectedValueChanged Occurs when the value of the SelectedValue property is modified.

Page 25: Session 1. Bai 1 ve winform

Slide 25 of 38

04/12/23

Page 26: Session 1. Bai 1 ve winform

Slide 26 of 38

04/12/23

"LinkLabel" Control- The LinkLabel control is similar to the Label control, but allows you to display its caption as a hyperlink.