Programming. C# Programming: From Problem Analysis to Program Design 2nd Edition. David McDonald, Ph.D. Director of Emerging Technologies

Similar documents
Chapter 12: Using Controls

Chapter 12: Using Controls

EEE-425 Programming Languages (2013) 1

EEE-425 Programming Languages (2013) 1

Philadelphia University Faculty of Information Technology. Visual Programming

Keeping Track, Menus. CSC 330 Object-Oriented Programming 1

CST242 Windows Forms with C# Page 1

CIS 3260 Intro to Programming with C#

Interface Design in C#

DateTimePicker Control

Click File on the menu bar to view the individual menu items and their associated icons on the File menu.

Overview Describe the structure of a Windows Forms application Introduce deployment over networks

Dive Into Visual C# 2010 Express

You will have mastered the material in this chapter when you can:

Controls. By the end of this chapter, student will be able to:

ListBox. Class ListBoxTest. Allows users to add and remove items from ListBox Uses event handlers to add to, remove from, and clear list

Microsoft Visual Basic 2005: Reloaded

WinForms Applications

Philadelphia University Faculty of Information Technology. Visual Programming. Using C# -Work Sheets-

Using Visual Basic Studio 2008

C# Programming: From Problem Analysis to Program Design. Fourth Edition

SMART Recorder. Record. Pause. Stop

Full file at Programming in Visual Basic 2010

Unit 3 Additional controls and Menus of Windows

B.V Patel Institute of BMC & IT, UTU

Microsoft Visual Basic 2005 CHAPTER 6. Loop Structures

Introductionto the Visual Basic Express 2008 IDE

Menus, Common Dialog Controls, Context Menus, Sub Procedures, and Functions

Text box. Command button. 1. Click the tool for the control you choose to draw in this case, the text box.

IT3101 -Rapid Application Development Second Year- First Semester. Practical 01. Visual Basic.NET Environment.

Changing Button Images in Microsoft Office

Forms/Distribution Acrobat X Professional. Using the Forms Wizard

Web Accessibility Change Your Mouse Pointer Visibility Handbook

Location of menu elements

Creating Web Applications Using ASP.NET 2.0

List and Value Controls

ComponentOne. TouchToolkit for WinForms

COPYRIGHTED MATERIAL. Visual Basic: The Language. Part 1

Working with PDF s. To open a recent file on the Start screen, double click on the file name.

EPSON RC+ 7.0 Option. GUI Builder 7.0 EM145A2719F. Rev. 2

Windows 7 Control Pack for WinForms

Getting started 7. Setting properties 23

Creating Your Own Documents in RealtiWeb

Word Tips & Tricks. Status Bar. Add item to Status Bar To add an itme to the status bar, click on the item and a checkmark will display.

Note that each button has a label, specified by the Text property of the button. The Text property of the group box is also visible as its title.

Your First Windows Form

Introduction to using Visual Studio 2010 to build data-aware applications

Routing a BennyBuy Access Form Using DocuSign

Pelnor Help Add-in.

NiceForm User Guide. English Edition. Rev Euro Plus d.o.o. & Niceware International LLC All rights reserved.

Unit 3. Lesson Designing User Interface-2. TreeView Control. TreeView Contol

GUI Design and Event- Driven Programming

Chapter 13: Handling Events

The first program we write will display a picture on a Windows screen, with buttons to make the picture appear and disappear.

Using the Customize Dialog Box

1. What is the definition of a problem? 2. How to solve problems? 3. What is meant by Algorithm? 4. What is a Program testing? 5. What is Flowchart?

Chapter 3. Fundamentals of Programming in Visual Basic

Introduction to Programming. Writing Programs Syntax, Logic and Run-time Errors

Learn the three palettes. Learn how data is passed in LabVIEW. Create a subvi using two different methods.

WORD XP/2002 USER GUIDE. Task- Formatting a Document in Word 2002

Menus. You ll find MenuStrip listed in the Toolbox. Drag one to your form. Where it says Type Here, type Weather. Then you ll see this:

Step 1: Start a GUI Project. Start->New Project->Visual C# ->Windows Forms Application. Name: Wack-A-Gopher. Step 2: Add Content

Menus and Printing. Menus. A focal point of most Windows applications

CHAPTER 3. Writing Windows C# Programs. Objects in C#

UNIT 3 ADDITIONAL CONTROLS AND MENUS OF WINDOWS

Understanding Events in C#

Document Editor Features Available in HTML5. it just makes sense

CIS 3260 Intro. to Programming with C#

Dive Into Visual C# 2008 Express

SIMPLE TEXT LAYOUT FOR COREL DRAW. When you start Corel Draw, you will see the following welcome screen.

Getting started 7. Setting properties 23

Computer Science 110. NOTES: module 8

Switches between worksheet and menu / Ribbon. Calculates all worksheets in all open workbooks. Highlights shortcut keys of Menu and Ribbon items.

Introduction to Word 2010

Chapter 2 Visual Basic Interface

Windows Programming Using C#

Excel Tables & PivotTables

Creating Accessible Documents in Adobe Acrobat Pro 9

INTRODUCTION TO VISUAL BASIC 2010

Client Editor Features

Exercises for Delphi Advanced Programming Technology (English Version)

Mailing Labels from REARS

Microsoft PowerPoint 2007 Beginning

CUSTOMISE FIELDS AND LAYOUTS 2010

Microsoft Office Excel 2003

OpenStax-CNX module: m Thermometer VI * National Instruments

Getting Started The Application Window Office Office 2003 Application Window cont d

SPARK. User Manual Ver ITLAQ Technologies

WinForms Charts How to Determine the Chart Element underneath. the Mouse Cursor and Display Series Point s Data in a Tooltip

WEEK NO. 12 MICROSOFT EXCEL 2007

Introduction to PowerPoint 2007

1 Dept: CE.NET Programming ( ) Prof. Akash N. Siddhpura. Working with Form: properties, methods and events

SharePoint SITE OWNER TRAINING

DATABASE VIEWER PLUS FOR IPAD: USER GUIDE PRODUCT VERSION: 4.1

INFRAGISTICS WPF 15.2 Service Release Notes September 2016

A Second Visual BASIC Application : Greetings

C# Windows Forms Applicaton Tutorial with Example

Full file at Chapter 2: Creating a User Interface

MAIL MERGE USING MS WORD 97

How To Capture Screen Shots

Transcription:

9 Programming Based on Events C# Programming: From Problem Analysis to Program Design 2nd Edition David McDonald, Ph.D. Director of Emerging Technologies Chapter Objectives Create applications that use the ListBox control object to enable multiple selections from a single control Contrast ComboBox to ListBox objects by adding both types of controls to an application Add Menu and TabControl control options to Window forms and program their event-handler methods Wire multiple RadioButton and CheckBox object events to a single event-handler method 1

ListBox Control Objects Displays list of items for single or multiple selections Scroll bar is automatically added when total number of items exceeds the number that can be displayed Can add or remove items at design time or dynamically at run time Includes number of properties and events The Items property used to set initial values Click on (Collections) to add items Adding a ListBox Control Object Add ListBox control, then click on Items property (Collection) to type entries Figure 9-2 String Collection Editor 2

ListBox Control Objects (continued) Name property Useful to set for program statements Sorted property Set to true to avoid having to type values in sorted order Register an event for the ListBox Might want to know when the item selection changes Double-clicking on any control registers its default event for the control SelectedIndexChanged: default event for ListBox ListBox Control Objects (continued) Register its event with the System.EventHandler delegate this.lstboxevents.selectedindexchanged += new System.EventHandler (this.listbox1_selectedindexchanged); Visual Studio adds event-handler method private void listbox1_selectedindexchanged { } (object sender, System.EventArgs e) 3

ListBox Control Objects (continued) To retrieve string data from ListBox use Text property this.txtboxresult.text = this.lstboxevents.text; Place in method body When event fires, selection retrieved and stored in TextBox object ListBox Control Objects (continued) Figure 9-3 SelectedIndexChanged event fired 4

Multiple Selections with a ListBox SelectionMode Property has values of MultiSimple, MultiExtended, None, and One MultiSimple: use the spacebar and click the mouse MultiExtended can also use Ctrl key, Shift key, and arrow keys foreach(string activity in lstboxevents.selecteditems) { result += activity + " "; } this.txtboxresult.text = result; ListBox Control Objects (continued) Figure 9-4 Multiple selections within a ListBox object 5

ListBox Control Objects (continued) SelectedItem and SelectedItems return objects Store numbers in the ListBox, once retrieved as objects, cast the object into an int or double for processing Adding items to a ListBox at run time by using Add( ) method with the Items property lstboxevents.items.add("string value to add"); private void btnnew_click(object sender, System.EventArgs e) { } lstboxevents.items.add(txtboxnewact.text); ListBoxExample Figure 9-5 Add( ) method executed inside the buttonclick event 6

ListBox Control Properties 7

ListBox Control Methods ListBox Control Methods (continued) Note that ListBox control inherits members from Control class 8

ComboBox Controls Extra TextBox object with ComboBox User selects from list or types new value Figure 9-6 ComboBox and ListBox objects ComboBox Controls (continued) Top line left blank in ComboBox when DropDownStyle property is set to DropDown (default setting) Figure 9-7 ComboBox list of choices 9

Handling ComboBox Events ComboBox only allows a single selection to be made Default event-handler method: SelectedIndexChanged( ) Same as ListBox control object Could register KeyPress( ) event-handler method BUT, event is fired with each and EVERY keystroke Programming Event Handlers Since ListBox object allows multiple selections, Text property cannot be used Text ONLY gets the first one selected Use the SelectedItems, SelectedIndices, or Items to retrieve a collection of items selected Zero-based structures Access them as you would access an element from an array SelectedIndices is a collection of indexes 10

Programming Event Handlers KeyPress( ) eventhandler method fired with each keystroke Figure 9-8 KeyPress and SelectedIndexChanged events fired MenuStrip Controls Offers advantage of taking up minimal space Drag and drop MenuStrip object from toolbox to your form Icon representing MenuStrip placed in Component Tray Select MenuStrip object to set its properties To add the text for a menu option, select the MenuStrip icon and then click in the upper-left corner of the form 11

MenuStrip Controls (continued) Drag MenuStrip control to form, then click here to display Menu structure Figure 9-9 First step to creating a menu MenuStrip Control Objects Ampersand (&) is typed between the F and o for the Format option to make Alt+o shortcut for Format Figure 9-10 Creating a shortcut for a menu item 12

MenuStrip Control Objects (continued) To create separators, right-click on the text label (below the needed separator) Select Insert Separator Figure 9-11 Adding a separator MenuStrip Control Objects Set the text to be displayed when the cursor is rested on top of the control Figure 9-12 Setting the Property for the ToolTip control 13

Wire Methods to Menu Option Event Set the Name property for each menu option Do this first, then wire the event Click events are registered by double-clicking on the Menu option When the menu option is clicked, the event triggers, happens, or is fired Adding Predefined Standard Windows Dialog Boxes Included as part of.net Dialog boxes that look like standard Windows dialog boxes File Open, File Save, File Print, and File Print Preview Format Font Format Color dialogs 14

Adding Predefined Standard Windows Dialog Boxes Color private void menucolor_click(object sender, Retrieves the System.EventArgs e) { property setting for the Label colordialog1.color = lbloutput.forecolor; object if (colordialog1.showdialog( )!= DialogResult.Cancel ) { C lb lbloutput.forecolor = colordialog1.color; clicked } } Set to selection made current ForeColor Checks to see if Cancel button CheckBox Objects Appear as small boxes Allow users to make a yes/no or true/false selection Checked property set to either true or false depending on whether a check mark appears or not Default false value CheckChanged( ) default event-handler method Fired when CheckBox object states change Can wire one event handler to multiple objects 15

Wiring One Event Handler to Multiple Objects Using Properties window, click on the Events Icon Click the down arrow associated with that event Select method to handle the event Follow the same steps for other objects CheckBox Object Figure 9-17 ComputeCost_CheckedChanged( ) method raised 16

GroupBox Objects CheckBox objects may be grouped together for visual appearance Can move or set properties that impact the entire group A GroupBox control should be placed on the form before you add objects GroupBox control adds functionality to RadioButton objects Allow only one selection RadioButton Objects Appear as small circles Give users a choice between two or more options Not appropriate to select more than one CheckBox object with RadioButton objects Group RadioButton objects by placing them on a Panel or GroupBox control Setting the Text property for the GroupBox adds a labeled heading over the group 17

RadioButton Objects (example) Figure 9-18 GroupBox and RadioButton objects added RadioButton Objects (continued) Turn selection on this.radinterm.checked = true; Raise a number of events, including Click( ) and CheckedChanged( ) events Wire the event-handler methods for RadioButton objects, just like CheckBox 18

RadioButton Objects (continued) Register ComputeCost_CheckedChanged () method Figure 9-19 Wired Click event RadioButton Objects (continued) ComputeCost_CheckedChanged( ) method if (this.radbeginner.checked) { cost +=10; this.lblmsg.text = "Beginner + -- Extra $10 charge"; } else // more statements 19

ComputeCost_CheckChanged( ) and Click( ) Events Raised Figure 9-20 ComputeCost_CheckedChanged( ) and Click( ) events raised TabControl Controls Sometime an application requires too many controls for a single screen TabControl object displays multiple tabs, like dividers in a notebook Each separate tab can be clicked to display other options Add a TabControl object to the page by dragging the control from the Container section of the Toolbox 20

TabControl Controls (continued) Figure 9-21 Tabbed controlled application TabControl Controls (continued) Figure 9-22 TabControl object stretched to fill form 21

TabControl Controls (continued) TabPage property enables you to format individual tabs Clicking the ellipsis beside the Collection value displays the TabPage Collection Editor 22