Week 3: WINDOWS PROGRAMMING

74
C#4.0 Week 3: WINDOWS PROGRAMMING Chapter 15 in “Beginning Visual C# 2010” ebook Chapter 4 in “”MCTS_Self-Paced_Training_Kit” ebook

description

Week 3: WINDOWS PROGRAMMING. Chapter 15 in “Beginning Visual C# 2010” ebook Chapter 4 in “”MCTS_Self-Paced_Training_Kit” ebook. 4. GroupBoxes and Panels. Arrange components on a GUI GroupBox es can display a caption Text property determines its caption Panel s can have scrollbar - PowerPoint PPT Presentation

Transcript of Week 3: WINDOWS PROGRAMMING

C#4.0Week 3:

WINDOWS PROGRAMMING

Chapter 15 in “Beginning Visual C# 2010” ebook Chapter 4 in “”MCTS_Self-Paced_Training_Kit”

ebook

C#4.0

4. GroupBoxes and Panels Arrange components on

a GUI GroupBoxes can display

a caption Text property determines

its caption Panels can have scrollbar

View additional controls inside the Panel

Windows Programming 1 Chapter 15:Basic Windows Programming Slide 2

GroupGroup

C#4.0

GroupBoxes and Panels

GroupBox Properties

Description

Common Properties Controls The controls that the GroupBox contains. Text Text displayed on the top portion of the

GroupBox (its caption). Fig. 12.19 GroupBox properties.

Windows Programming 1 Chapter 15:Basic Windows Programming Slide 3

C#4.0

GroupBoxes and Panels

Panel Properties

Description

Common Properties

AutoScroll Whether scrollbars appear when the Panel is too small to hold its controls. Default is false.

BorderStyle Border of the Panel (default None; other options are Fixed3D and FixedSingle).

Controls The controls that the Panel contains. Fig. 12.20 Panel properties.

Windows Programming 1 Chapter 15:Basic Windows Programming Slide 4

C#4.0

GroupBoxes and Panels

Fig. 12.21 Creating a Panel with scrollbars.

Controls inside panel panel

panel scrollbars

Windows Programming 1 Chapter 15:Basic Windows Programming Slide 5

C#4.0

GroupBoxes and Panels

Chapter 15:Basic Windows Programming

C#4.0

The FlowLayoutPanel Control The FlowLayoutPanel control is a subclass of

the Panel control Unlike the Panel control, however, the

FlowLayoutPanel dynamically repositions the controls it hosts when it is resized at either design time or run time

Like the Panel control, the FlowLayoutPanel control is scrollable

Windows Programming 1 Chapter 15:Basic Windows Programming Slide 7

C#4.0

The FlowLayoutPanel Control

Windows Programming 1 Chapter 15:Basic Windows Programming Slide 8

Properties Description AutoScroll Whether scrollbars appear when the FlowLayoutPanel

is too small to hold its controls. Default is false. BorderStyle Border of the Panel (default None; other options are

Fixed3D and FixedSingle). FlowDirection Determines the direction of flow in FlowLayoutPanel.

Can be set to LeftToRight, RightToLeft, TopDown, or BottomUp.

WrapContents Determines whether controls will automatically wrap to the next column or row when the FlowLayoutPanel is resized

Controls The controls that the Panel contains.

C#4.0

The SplitContainer Control The SplitContainer control creates a

subsection of the form where Splitter divides SplitContainer into two SplitterPanel controls that function similarly to Panel controls.

The SplitContainer.Dock property is set to Fill by default.

SplitContainer exposes its two child SplitterPanel controls through its Panel1 and Panel2 properties

Windows Programming 1 Chapter 15:Basic Windows Programming Slide 9

C#4.0

The SplitContainer ControlPROPERTY DESCRIPTIONBorderStyle Represents the visual appearance of the TabPage border. It

can be set to None, which indicates no border; FixedSingle, which creates a single-line border; or Fixed3D, which creates a border with a threedimensional appearance.

IsSplitterFixed Determines whether the location of Splitter is fixed and cannot be moved by the user

Orientation Determines whether Splitter is oriented horizontally or vertically in SplitContainer. It can be set to Horizontal or Vertical.

Panel1 Exposes the properties of the SplitContainer control’s Panel1.

Panel2 Exposes the properties of the SplitContainer control’s Panel2

Windows Programming 1 Chapter 15:Basic Windows Programming Slide 10

C#4.0

Dialog Boxes (dlg prefix) Predefined standard dialog boxes for:

File Open and Saving Printing and Previewing Color selection Font selection

Add the Common Dialog control to form Appears in the Component Tray, pane at bottom

of Form Designer where nondisplay controls are shown

Windows Programming 1 Chapter 15:Basic Windows Programming Slide 11

C#4.0

Drag common dialog to form

Windows Programming 1 Chapter 15:Basic Windows Programming Slide 12

C#4.0

Common Dialog Controls OpenFileDialog SaveFileDialog FontDialog ColorDialog PrintDialog PrintPreviewDialog

Windows Programming 1 Chapter 15:Basic Windows Programming Slide 13

C#4.0

FontDialog

Windows Programming 1 Chapter 15:Basic Windows Programming Slide 14

C#4.0

FontDialog

Property Font: Gets or sets the selected font.

Property ShowEffects whether the dialog box contains controls that allow the user to specify strikethrough, underline,Color.

Property Color: Gets or sets the selected font color

Method Showdialog():

Windows Programming 1 Chapter 15:Basic Windows Programming Slide 15

C#4.0

ColorDialog

Windows Programming 1 Chapter 15:Basic Windows Programming Slide 16

C#4.0

ColorDialogProperty Color : get/set color select by the userProperty FullOpen: True/false, whether the controls used to create custom colors are visible when the dialog box is openedMethod Showdialog():Displays a dialog box user interface

Windows Programming 1 Chapter 15:Basic Windows Programming Slide 17

C#4.0

ColorDialog

colorDialog1.FullOpen=true;colorDialog1.Color = Color.Red;colorDialog1.ShowDialog();lblDisplay.ForeColor = colorDialog1.Color;

Windows Programming 1 Chapter 15:Basic Windows Programming Slide 18

C#4.0

OpenFileDialog - SaveFileDialog Property FileName: Gets or sets a string containing the

file name selected in the file dialog box. Property Filter which determines the choices that

appear in the "Save as file type" or "Files of type" box in the dialog box.

Property FilterIndex: Gets or sets the index of the filter currently selected in the file dialog box.

Property InitialDirectory: Gets or sets the initial directory displayed by the file dialog box.

Property Title: Gets or sets the file dialog box title. Method Showdialog():

Windows Programming 1 Chapter 15:Basic Windows Programming Slide 19

C#4.0

The Button, CheckBox, andRadioButton Controls Button

Text Click (event)

CheckBox Checked CheckState ThreeState CheckedChanged (Event)

RadioButton Checked CheckedChanged (Event)

Windows Programming 1 Chapter 15:Basic Windows Programming Slide 20

Check boxRadio

C#4.0

The ListBox Control The ListBox control is the simplest of the list-

based controls and serves primarily to display a simple list of items in an easy-to-navigate user interface from which users can select one or more items

Windows Programming 1 Chapter 15:Basic Windows Programming Slide 21

C#4.0

The ListBox and ComboBox Control ListBox tool

lst prefix Simple List Box with/without scroll bars

ComboBox tool cbo prefix List may allow for user to add new items List may "drop down" to display items in

Windows Programming 1 Chapter 15:Basic Windows Programming Slide 22

C#4.0

The ListBox and ComboBox Control

Windows Programming 1 Chapter 15:Basic Windows Programming Slide 23

ListBoxes

DropdownCombo Box

SimpleCombo Box

DropdownList Box

C#4.0

The ListBox ControlPROPERTY DESCRIPTIONItems Returns the collection of items contained in this control.MultiColumn Indicates whether this item shows multiple columns of

items or only a single item.SelectedIndex Gets the index of the selected item or, if the

SelectionMode property is set to MultiSimple or MutilExtended, returns the index to any selected item.

SelectedIndices Returns a collection of all selected indexes.SelectedItem Returns the selected itemSelectedItems Returns a collection of all selected items.SelectionMode Determines how many items can be selected in a list

box

GetSelected returns true if property at given index is selecte

Windows Programming 1 Chapter 15:Basic Windows Programming Slide 24

C#4.0

Common ListBox Methods

Windows Programming 1 Chapter 15:Basic Windows Programming Slide 25

METHOD DESCRIPTIONItems.Add() Add the item into ListBox

Items.RemoveAt(int Index) Removes the Item at the specified index within the collection

Items.Remove(object value)

Removes the specified object from the collections

Items.Clear() Removes all items from the collection

ClearSelected() Clears all selections in the ListBox.

FindString() Finds the first string in the ListBox beginning with a string you specify

GetSelected() Returns a value that indicates whether an item is selected.

SetSelected() Sets or clears the selection of an item.

ToString() Returns the currently selected item.

C#4.0

The ListBox, CheckedListBox, andComboBox Controls

methoda. <MyListBox>.Items.Add(“MyListItem”)

lstMyListBox.Items.Add(“cat”);b. <MyListBox>.Items.RemoveAt(Index)

lstMyListBox.Items.RemoveAt(3);c. <MyListBox>.Items.Remove(String)

lstMyListbox.Item.Remove(“cat”);d. <MyListBox>.Items.Clear()

lstMyListBox.Clear();e. <MyListBox>.SetSelect(index, True/Fale)

lstMyListBox.SetSelect(3,True);e. <MyListBox>.ClearSelect()

Windows Programming 1 Chapter 15:Basic Windows Programming Slide 26

C#4.0

ComboBox Control The ComboBox control is similar to the

ListBox control, but, in addition to allowing the user to select items from a list, it provides a space for a user to type an entry.

Additionally, you can configure the ComboBox to either display a list of options or to provide a drop-down list of options

Windows Programming 1 Chapter 15:Basic Windows Programming Slide 27

C#4.0

ComboBox ControlPROPERTY DESCRIPTIONDropDownHeight Sets the maximum height for the drop-down box.

DropDownStyle Determines the style of the combo box. Can be set to Simple, which is similar to a ListBox; DropDown, which is the default; or DropDownList, which is similar to DropDown but does not allow the user to type a new value.

DropDownWidth Sets the width of the drop-down section of the combobox.

Text

Windows Programming 1 Chapter 15:Basic Windows Programming Slide 28

C#4.0

The CheckedListBox Controls CheckedListBox displays a list of items to

users and allows them to select multiple items by checking boxes that are displayed next to the items.

Any number of items can be checked, but only one item can be selected at a time.

You can retrieve a collection that represents the checked items by accessing the CheckedItems collection

Windows Programming 1 Chapter 15:Basic Windows Programming Slide 29

C#4.0

The CheckedListBox ControlsPROPERTYCheckedIndices Returns a collection that represents all the checked

indexesCheckedItems Returns a collection that exposes all the checked

items in the controlItems Returns the collection of items contained in this control

MultiColumn Indicates whether this control shows multiple columns of items or only a single item

Windows Programming 1 Chapter 15:Basic Windows Programming Slide 30

C#4.0

The DomainUpDown and NumericUpDown Controls DomainUpDown (dud)

Items ReadOnly SelectedIndex SelectedItem Sorted SelectedItemChanged (Event)

Windows Programming 1 Chapter 15:Basic Windows Programming Slide 31

C#4.0

The DomainUpDown and NumericUpDown Controls NumericUpDown (nud)

Increment Maximum Minimum Value ValueChanged Event

Windows Programming 1 Chapter 15:Basic Windows Programming Slide 32

C#4.0

The MonthCalendar andDateTimePicker Controls

Windows Programming 1 Chapter 15:Basic Windows Programming Slide 33

C#4.0

MonthCalendar properties and an event

Windows Programming 1 Chapter 15:Basic Windows Programming Slide 34

C#4.0

DateTimePicker Control The DateTimePicker control enables the user

to set a date, a time, or both in an easy-tounderstand graphical interface similar to a ComboBox control

Windows Programming 1 Chapter 15:Basic Windows Programming Slide 35

C#4.0

DateTimePicker properties and an event

Windows Programming 1 Chapter 15:Basic Windows Programming Slide 36

C#4.0

The Timer, TrackBar, andProgressBar Controls The Timer _ tmr

Enabled Property Interval Property Start() Method Stop() Method Tick Event

Windows Programming 1 Chapter 15:Basic Windows Programming Slide 37

C#4.0

The TrackBar Controls The TrackBar control provides a simple

interface that enables the user to set a value from a predetermined range of values by graphically manipulating a slider with the mouse or keyboard commands.

This enables the user to rapidly set a value from a potentially very large range.

Windows Programming 1 Chapter 15:Basic Windows Programming Slide 38

C#4.0

The TrackBar Controls

Windows Programming 1 Chapter 15:Basic Windows Programming Slide 39

PROPERTY DESCRIPTIONLargeChange The number of positions the slider moves in response to

mouse clicks or the Page Up and Page Down keys.Maximum The maximum value for TrackBar.Minimum The minimum value for TrackBar.SmallChange The number of positions the slider moves in response to

arrow key keystrokes.TickFrequency The number of positions between tick marks on TrackBar.TickStyle Indicates where ticks appear on TrackBar.Value The value returned by TrackBar.

C#4.0

The Timer, TrackBar, andProgressBar Controls ProgressBar (prg)

Maximum Property (100) Minimum Property (0) Value Property (0)

Windows Programming 1 Chapter 15:Basic Windows Programming Slide 40

C#4.0

The TabControl The TabControl control enables you to group

sets of controls in tabs, rather like files in a filing cabinet or dividers in a notebook.

TabControl serves as a host for one or more TabPage controls, which themselves contain controls.

The user can switch between tab pages (and the controls contained therein) by clicking the tabs on TabControl

Windows Programming 1 Chapter 15:Basic Windows Programming Slide 41

C#4.0

The TabControl

Windows Programming 1 Chapter 15:Basic Windows Programming Slide 42

PROPERTY DESCRIPTIONAppearance Determines the visual style of TabControlAlignment Determines whether the tabs appear on the top, bottom,

left, or right of the tab controlMultiline Determines whether more than one row of tabs is

allowed on the tab controlTabPages Represents the collection of TabPage controls hosted by

TabControlAutoScroll Determines whether TabPage will display scroll bars

when controls are hosted outside the visible bounds of the panel

BorderStyle Represents the visual appearance of the TabPage border

Text Represents the text displayed on the tab in the tab control that represents this TabPage

C#4.0

ToolTips We demonstrated tool tipsthe helpful text that

appears when the mouse hovers over an item in a GUI

Windows Programming 1 Chapter 15:Basic Windows Programming Slide 43

C#4.0

Menus (mnu prefix) Menus provide groups of related commands for

Windows applications

separator bars Checked menu item

Windows Programming 1 Chapter 15:Basic Windows Programming Slide 44

C#4.0

Menu Design Standards

Windows Programming 1 Chapter 15:Basic Windows Programming Slide 45

New (Ctrl N) Open (Ctrl O) Close Save As Save (Ctrl S) Print (Ctrl P) Exit

Undo(Ctrl Z) Cut (Ctrl X) Copy(Ctrl C) Paste (Ctrl V) Find (Ctrl F) Replace (Ctrl H)

File Menu Edit Menu

C#4.0

The MenuStrip Control The MenuStrip control is essentially a ToolStrip control

optimized for the display of ToolStripMenu items. The MenuStrip control derives from ToolStrip and can

host all the tool strip items described in the previous lesson. Its primary function, however, is to host ToolStripMenu items.

ToolStripMenuItem controls are the controls that provide the visual representation for items on a menu. They can appear as text, an image, or both and can execute code found in their ToolStripMenuItem

Windows Programming 1 Chapter 15:Basic Windows Programming Slide 46

C#4.0

The MenuStrip ControlPROPERTY DESCRIPTIONAllowMerge Indicates whether this menu strip can be merged with

another tool strip.LayoutStyle Indicates how the controls on the tool strip are laid out

ShowItemToolTips Indicates whether tool tips for individual tool strip items are displayed.

TextDirection Indicates the direction of the text in controls hosted in the tool strip.

Windows Programming 1 Chapter 15:Basic Windows Programming Slide 47

C#4.0

ToolStripMenuItem properties

Windows Programming 1 Chapter 15:Basic Windows Programming Slide 48

PROPERTY DESCRIPTIONAutoSize Determines whether the menu item is automatically sized

to fit the text.Checked Determines whether the menu item appears as selected.

CheckOnClick Determines whether the menu item is automatically selected when clicked

CheckState Returns the CheckState value of the menu item. CheckState can be Checked, Unchecked, or Indeterminate.

DisplayStyle Determines how the tool strip menu item is displayed.

DoubleClickEnabled Determines whether the DoubleClick event fires.

DropDownItems Contains a collection of tool strip items (usually, but notnecessarily, tool strip menu items) that appear in thedrop-down list when this item is chosen.

C#4.0

CREATING MENUS AND MENU ITEMS To create an access shortcut (or keyboard

shortcut),

Windows Programming 1 Chapter 15:Basic Windows Programming Slide 49

C#4.0

CREATING MENUS AND MENU ITEMS To create an access shortcut (or keyboard

shortcut),

Windows Programming 1 Chapter 15:Basic Windows Programming Slide 50

C#4.0

CREATING MENUS AND MENU ITEMS

To add other shortcut keys (e.g., <Ctrl>-F9)

Windows Programming 1 Chapter 15:Basic Windows Programming Slide 51

C#4.0

Adding Separator Bars to Menus You can add a separator to any submenu at

design time by choosing Separator from the drop-down box in the menu item design interface.

Using “-”

Windows Programming 1 Chapter 15:Basic Windows Programming Slide 52

C#4.0

MenuStrip and ToolStripMenuItem an event.

Click Generated when an item is clicked or a shortcut key is used. This is the default event when the menu is double clicked in the designer

DropDownOpened Occurs when Dropdown hasoOpened

Windows Programming 1 Slide 53

C#4.0

Context Menus and the ContextMenuStrip Control All controls that can display a context menu

expose a ContextMenuStrip property that represents the context menu associated with that control

A context menu is invoked when the user right-clicks a control.

You can set this property at design time in the Properties window.

Windows Programming 1 Chapter 15:Basic Windows Programming Slide 54

C#4.0

The StatusBar Control Alignment Property AutoSize Property BorderStyle Property Icon Property Style Property ToolTipText Property

Windows Programming 1 Chapter 15:Basic Windows Programming Slide 55

C#4.0

The ToolBar Control Buttons Property ButtonClick Event ImageList Property SendToBack() Method ShowToolTips Property

Windows Programming 1 Chapter 15:Basic Windows Programming Slide 56

C#4.0

CREATING MDI APPLICATIONS Multiple Document Interface (MDI) Windows

Single Document Interface (SDI) Multiple Document Interface (MDI)

Windows Programming 1 Chapter 15:Basic Windows Programming Slide 57

C#4.0

CREATING MDI APPLICATIONS ActiveMdiChild Property IsMdiContainer Property MdiChildActivate Event MdiChildren Property MdiParent Property LayoutMdi() Method

Windows Programming 1 Chapter 15:Basic Windows Programming Slide 58

C#4.0

Multiple Document Interface (MDI) Windows

ArrangeIcons Cascade

LayoutMdi( MdiLayout.ArrangeIcons); LayoutMdi( MdiLayout.Cascade);

Windows Programming 1 Chapter 15:Basic Windows Programming Slide 59

C#4.0

Multiple Document Interface (MDI) Windows

TileHorizontal TileVertical

LayoutMdi(MdiLayout.TileHorizontal); LayoutMdi(MdiLayout.TileVertical);

Windows Programming 1 Chapter 15:Basic Windows Programming Slide 60

C#4.0

Form Splash

Windows Programming 1 Chapter 15:Basic Windows Programming Slide 61

C#4.0

Form Splash FormBorderStyle = None; StartPosition = CenterToScreen; TopMost = true;

Windows Programming 1 Chapter 15:Basic Windows Programming Slide 62

C#4.0

Form Splashstatic void Main() { Application.EnableVisualStyles();

Application.SetCompatibleTextRenderingDefault(false); frmFlashForm f = new frmFlashForm(); f.ShowDialog(); if (f.DialogResult == DialogResult.OK) { Application.Run(new frmFormMain()); }}

Windows Programming 1 Chapter 15:Basic Windows Programming Slide 63

C#4.0

Form Splash private void timer1_Tick(object sender,

EventArgs e) { this.DialogResult = DialogResult.OK; timer1.Enabled = false; }

Windows Programming 1 Chapter 15:Basic Windows Programming Slide 64

C#4.0

Form About

Windows Programming 1 Chapter 15:Basic Windows Programming Slide 65

C#4.0

Mouse-Event Handling Mouse events can be handled for any control

that derives from class System.Windows.Forms.Control MouseEventArgs (object as arguments ) MouseEventHandler (requires an object )

Class MouseEventArgs contains information related to the mouse event, such as the mouse pointer's x- and y-coordinates, the mouse button pressed (Right, Left or Middle) and the number of times the mouse was clicked

Windows Programming 1 Chapter 15:Basic Windows Programming Slide 66

C#4.0

Mouse events and event arguments

Windows Programming 1 Chapter 15:Basic Windows Programming Slide 67

C#4.0

EX: Using the mouse to draw on a Form.

Windows Programming 1 Chapter 15:Basic Windows Programming Slide 68

C#4.0

EX: Using the mouse to draw on a Form.

Windows Programming 1 Chapter 15:Basic Windows Programming Slide 69

C#4.0

Keyboard - Event Handling Key events occur when keyboard keys are

pressed and released. Such events can be handled for any control

that inherits from System.Windows.Forms.Control

There are three key events KeyPress, KeyUp and KeyDown

Windows Programming 1 Chapter 15:Basic Windows Programming Slide 70

C#4.0

Keyboard - Event Handling The KeyPress event occurs when the user presses a key

that represents an ASCII character The specific key can be determined with property

KeyChar of the event handler's KeyPressEventArgs argument

The KeyPress event does not indicate whether modifier keys (e.g., Shift, Alt and Ctrl) were pressed when a key event occurred

If this information is important, the KeyUp or KeyDown events can be used

The KeyEventArgs argument for each of these events contains information about modifier keys

Windows Programming 1 Chapter 15:Basic Windows Programming Slide 71

C#4.0

Keyboard events and event arguments

Windows Programming 1 Chapter 15:Basic Windows Programming Slide 72

C#4.0 Windows Programming 1 Chapter 15:Basic Windows Programming Slide 73

C#4.0 Windows Programming 1 Chapter 15:Basic Windows Programming Slide 74