You are on page 1of 26

9/12/2013

Slide 1 of 38

Session 1
Windows Forms and Basic Controls
Trnh by : V Ngc t Email : vongocdatit@gmail.com in thoi : 0934.969.680

9/12/2013

Slide 2 of 26

Module Introduction
This module provides an overview of Windows Forms and the basic controls used to create graphical user interfaces. 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.

9/12/2013

Slide 3 of 38

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

9/12/2013

Slide 4 of 38

Windows Forms in .NET Framework

9/12/2013

Slide 5 of 38

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

9/12/2013
Property ForeColor Location Modal WindowState Method Activate Focus Event Activated Click Deactivate FormClosed GotFocus Resize Validated Description Specifies or retrieves the foreground color. Specifies or retrieves the co-ordinates of the upper left corner of the form.

Slide 7 of 38

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. Specifies or retrieves a value indicating whether a form is minimized, maximized, or is in the normal state. Description Activates a form and gives it focus. Puts the focus on a control. Description Occurs when the form is activated in code or by the user. Occurs when a control in clicked. Occurs when the focus on the form is taken away in code or by the user. Occurs after the form is closed. Occurs when the focus is put on a control of the form. Occurs when a control on the form is resized. Occurs when validation is completed by a control. For example, the event is raised after validating the login details in the TextBox control.

9/12/2013

Slide 8 of 38

9/12/2013

Slide 9 of 38

Lifecycle of Windows Forms

9/12/2013

Slide 10 of 38

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

9/12/2013

Slide 11 of 38

Types
A 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

9/12/2013

Slide 12 of 38

"Control" Class
The 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.

9/12/2013

Slide 13 of 38

9/12/2013
Property Controls Name Parent Method Focus Hide Select Show Event Click ControlAdded Doubleclick GotFocus Leave LostFocus Move Description Retneves a collection of controls contained within the control. Specifies or retrieves the name of a control. Specifies or retrieves the parent container of the control. Description Sets input focus to the control. Hides the control from the user. Activates a control. Displays the control. Description Occurs when a control is clicked. Occurs when a new control is added to the Control . Occurs when the control is double-clicked. Occurs when the focus is on the control. Occurs when the input focus leaves the control. Occurs when the control loses focus. Occurs when the user moves the control.

Slide 14 of 38

TextChanged

Occurs when the value of the Text property changes.

9/12/2013

Slide 15 of 38

"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

9/12/2013 "TextBox" Controls

Slide 16 of 38

- 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 MaxLength MultiLine Name PasswordChar Readonly Text

Description Specifies or retrieves the maximum number of characters that the user can enter in the control. Specifies or retrieves a value indicating whether the user can enter multi-line text. Specifies or retrieves the name of the control. Specifies or retrieves the special character used to mask characters of a password. Specifies or retrieves a value whether the text in the control is editable. Specifies or retrieves the text on the control.

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

9/12/2013
Method AppendText Clear Copy Focus Paste Event KeyPress Leave TextChanged Description Adds text to the existing text of the control. Removes text from the control. Copies the selected text in the control to the Clipboard. Sets the input focus on the control to enter text.

Slide 17 of 38

Replaces the selected text in the control with the text copied to the Clipboard. Description Occurs when the user has finished pressing a key. Occurs when the control is no longer the active control of the form. Occurs when the value of the Text property changes.

9/12/2013 "MaskedTextBox" Controls - The MaskedTextBox control is a new control used to validate user input.
Property Mask MaskCompleted Name PromptChar Text Description Specifies or retrieves the characters to be used as a mask.

Slide 18 of 38

Retrieves a value indicating whether the user has entered all the required characters into the input mask. Specifies or retrieves the name of the control. Specifies or retrieves the character to be used for prompting the user if the user has missed some input. Specifies or retrieves the text on the control.

9/12/2013

Slide 19 of 38

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

9/12/2013
Property DialogResult Enabled FlatStyle Image Name Description

Slide 20 of 38

Specifies or retrieves a value which is sent to the parent form when a button is clicked. Specifies or retrieves a value indicating whether the control can be accessed by the user. Specifies or retrieves the flat style of the control, which can be Flat, standard, Popup, or System. Specifies or retrieves the image on the control. Specifies or retrieves the name of the control.

Text
Method Focus PerformClick Event Click Doubleclick MouseDoubleClick

Specifies or retrieves the text on the control.


Description Sets the focus on the control. Triggers a Click event for a button. Description Occurs when the control is clicked. Occurs when the control is double-clicked. Occurs when the control is double-clicked using the mouse.

9/12/2013

Slide 21 of 38

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

"ListBox" Controls
Property Items SelectionMode Description Retrieves the items of the control.

9/12/2013

Slide 22 of 38

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. Specifies or retrieves the zero-based index number of the selected item in the control. Specifies or retrieves the text of the selected item in the control. Description Deselects all the selected items in the control. Retrieves the text of the given item. Returns true if the specified item is selected and returns false if the specified item is not selected. Selects or removes the selection for a specified item. Description Occurs when the value of the Selectedlndex property is modified. Occurs when the value of the SelectedValue property is modified.

Selectedlndex Text Method ClearSelected GetltemText GetSelected SetSelected Event SelectedlndexChanged SelectedValueChanged

9/12/2013

Slide 23 of 38

9/12/2013

Slide 24 of 38
Description 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. Retrieves the object containing the items of the control. Specifies or retrieves the maximum number of items to be displayed in the control. Specifies or retrieves the selected item in the control. Specifies or retrieves the zero-based index number of the selected item in the control. Specifies or retrieves the text of the control. Description Gets the text of the given item. Selects the text appearing in the control. Activates a control and is inherited from the Control class. Description Occurs when the drop-down portion of the control is displayed. Occurs when the value of the selectedlndex property is modified. Occurs when the value of the SelectedValue property is modified.

"ComboBox" Controls
Property DropDownStyle Items MaxDropDownItems Selectedltem Selectedlndex Text Method GetltemText SelectAll Select Event DropDown SelectedlndexChanged SelectedValueChanged

9/12/2013

Slide 25 of 38

9/12/2013

Slide 26 of 38

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