Controls, Events, and More

Slides:



Advertisements
Similar presentations
© by Pearson Education, Inc. All Rights Reserved. continued …
Advertisements

Graphical User Interface (GUI) A GUI allows user to interact with a program visually. GUIs are built from GUI components. A GUI component is an object.
Microsoft Visual Basic 2005 CHAPTER 8 Using Procedures and Exception Handling.
Copyright 2007, Information Builders. Slide 1 Maintain & JavaScript: Two Great Tools that Work Great Together Mark Derwin and Mark Rawls Information Builders.
Hello World In C++ and Microsoft Visual C++. Directions to begin a project 1. Go to All Programs 2. Open Visual Studio C++ 3. Click on New Project 4.
Microsoft Visual Basic 2012 Using Procedures and Exception Handling CHAPTER SEVEN.
Web Technologies Website Development Trade & Industrial Education
Microsoft Visual Basic 2008 CHAPTER 8 Using Procedures and Exception Handling.
IE 411/511: Visual Programming for Industrial Applications
Tutorial 1: An Introduction to Visual Basic.NET1 Tutorial 1 An Introduction to Visual Basic.NET.
Chapter 1: An Introduction to Visual Basic 2005 Programming with Microsoft Visual Basic 2005, Third Edition.
Chapter 12: Using Controls. Examining the IDE’s Automatically Generated Code A new Windows Forms project has been started and given the name FormWithALabelAndAButton.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Typing Application Introducing Keyboard Events, Menus, Dialogs and the Dictionary.
Chapter 3: Using GUI Objects and the Visual Studio IDE.
Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 2 Creating a User Interface.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved. 1 3 Welcome Application Introduction to Visual Programming.
Chapter One An Introduction to Visual Basic 2010 Programming with Microsoft Visual Basic th Edition.
WaveMaker Visual AJAX Studio 4.0 Training Basics: Building Your First Application Designer Basics.
Visual C# 2012 How to Program © by Pearson Education, Inc. All Rights Reserved.
Chapter 2 – Introduction to the Visual Studio .NET IDE
1 Creating Windows GUIs with Visual Studio. 2 Creating the Project New Project Visual C++ Projects Windows Forms Application Give the Project a Name and.
Visual C# 2012 How to Program © by Pearson Education, Inc. All Rights Reserved.
Creating and Editing a Web Page
IE 411/511: Visual Programming for Industrial Applications Lecture Notes #2 Introduction to the Visual Basic Express 2010 Integrated Development Environment.
XP New Perspectives on Macromedia Dreamweaver MX 2004 Tutorial 5 1 Adding Shared Site Elements.
 2002 Prentice Hall. All rights reserved. 1 Introduction to the Visual Studio.NET IDE Outline Introduction Visual Studio.NET Integrated Development Environment.
Microsoft Visual C# 2010 Fourth Edition Chapter 3 Using GUI Objects and the Visual Studio IDE.
 You won’t write a single line of program code.  Instead, you’ll use visual programming techniques.  Visual Studio processes your actions (such as mouse.
© Ms. Masihi.  A Web page contains text and images that convey specific information to viewers.  To create a new web page, open Dreamweaver and select.
Computing and Information Technology Building a Web Browser
Dive Into® Visual Basic 2010 Express
Chapter 1: An Introduction to Visual Basic .NET
Visual Basic.NET Windows Programming
Creating a Presentation
Microsoft Visual Basic 2005: Reloaded Second Edition
Chapter 2: The Visual Studio .NET Development Environment
Apply Procedures to Develop Menus, List Box and Combo Box Objects
Microsoft Access 2007 – Level 2
Chapter 1: An Introduction to Visual Basic 2015
Chapter 2 – Introduction to the Visual Studio .NET IDE
3.01 Apply Controls Associated With Visual Studio Form
Incorporating Databases with ADO.NET
1. Introduction to Visual Basic
3.01 Apply Controls Associated With Visual Studio Form
Chapter 1: An Introduction to Visual Basic 2015
Visual studio 2010 SENG 403, Tutorial 2 SENG Winter 2011.
An Introduction to Computers and Visual Basic
Using Procedures and Exception Handling
Apply Procedures to Develop Menus, List Box and Combo Box Objects
Windows Desktop Applications
Chap 7. Building Java Graphical User Interfaces
Introduction to Microsoft Windows
Incorporating Databases with ADO.NET
Chapter 1 Editing a Photo
Microsoft Office Illustrated Introductory, Windows XP Edition
Variables and Arithmetic Operations
Multi-form applications and dialogs
MODULE 7 Microsoft Access 2010
Chapter 2 – Introduction to the Visual Studio .NET IDE
Windows xp PART 1 DR.WAFAA SHRIEF.
Windows 7                      .
Visual Studio.
An Introduction to Computers and Visual Basic
Visual C# - GUI and controls - 1
Guidelines for Microsoft® Office 2013
Key Applications Module Lesson 16 — Excel Essentials
Overview of the IDE Visual Studio .NET is Microsoft’s Integrated Development Environment (IDE) for creating, running and debugging programs (also.
Chapter 8 Using Document Collaboration and Integration Tools
Microsoft Excel 2007 – Level 2
Presentation transcript:

Controls, Events, and More More Windows Material Controls, Events, and more November 7, 2018

AssemblyInfo.cs Separating version, title, product, and author information from the executable code Controls, Events, and more November 7, 2018

AssemblyInfo.cs Each C# project has a file named AssemblyInfo.cs in a project folder named Properties This file contains attributes in square brackets [ ] that give information about the project including Title Author Copyright Version And so forth Controls, Events, and more November 7, 2018

AssemblyInfo.cs Example Author Version information Controls, Events, and more November 7, 2018

The Application Class The information from AssemblyInfo.cs is available to your Windows Forms application to use during execution Primarily accessed through a .NET class named Application using static methods and properties such as CompanyName CurrentCulture ProductName ProductVersion OpenForms And so forth If the AssemblyInfo.cs file is up-to-date with version information and so forth, the application code need not be updated when versions change, and so forth Controls, Events, and more November 7, 2018

Like Java’s JOptionPane dialogs Example Like Java’s JOptionPane dialogs Controls, Events, and more November 7, 2018

Docking, anchoring, and resizing Be in control of how windows look once resized Controls, Events, and more November 7, 2018

Docking and Anchoring Controls may be placed on a form at any point the designer wishes They can be dragged/dropped from the tool box They can be moved in the design view The buttons on the Layout toolbar may be used to align, center, and resize controls Panels can be used as in Java to isolate and group some components of a window from others The alignment and centering then applies to the panel Their properties can be set to change the position either in the design view or at run time Controls, Events, and more November 7, 2018

Docking and Anchoring When controls are placed in a fixed position on a form, they stay there even when the window is resized Controls, Events, and more November 7, 2018

Anchoring The client area of the window is the part of the window that does not include the caption bar, the borders around the window, … Controls may be anchored to the top, bottom, left, or right sides of the client area of the window Controls may be anchored to more than one side of the client area By default, most controls are anchored to both the top and the left sides of the client area When the window is resized, they stay in a fixed position relative to their anchors as illustrated on the previous slide Controls, Events, and more November 7, 2018

Anchoring In the design view, one may set the Anchor property as shown in this diagram … top, … … right, … Anchored on the left … … but not the bottom Clicking on one of the 4 rectangles creates or removes the respective anchor Controls, Events, and more November 7, 2018

Effects of Anchoring If the textboxes in this window are anchored to the top and right, resizing the window keeps them in a fixed position relative to their anchors Controls, Events, and more November 7, 2018

Effects of Anchoring If a control is anchored to opposite sides of the window (top and bottom or to the left and right), resizing the window in that direction stretches/shrinks the control in that direction if the control is stretchable in that direction – a label is not stretchable, for example Controls, Events, and more November 7, 2018

Docking When appropriate, some controls may be “docked” to one of the 4 sides of the client area of a window This attaches the docked control to that side The control is sized appropriately A control may be docked to Fill the remaining space as well When the window is resized, the docked controls may be resized as appropriate for their docked position Controls, Events, and more November 7, 2018

Five panel controls with different background colors shown docked Docking Five panel controls with different background colors shown docked Controls, Events, and more November 7, 2018

Docking Docking properties may be set at design time using the properties tab Dock at top Fill remaining area Dock on right Dock at left Dock at bottom Do not dock Controls, Events, and more November 7, 2018

Prevent Resizing by User There are two ways in which a user typically resizes a window Maximize button Drag the border of the window Each of these ways may be enabled or disabled At design time At run time Controls, Events, and more November 7, 2018

Disable the Maximize Button At design time, select the form in the design view Open the properties tab Set the Maximize Box property to False May also disable the minimize button Controls, Events, and more November 7, 2018

Disable Resizing by Dragging Border In the design view, select the form Open the properties tab Set the FormBorderStyle to one of the Fixed types such as Fixed3D Choose a Fixed Style Controls, Events, and more November 7, 2018

Disable Resizing: A Caveat Often, windows can be made to look very unprofessional or even unusable if resized incorrectly by the user We can prevent the user from doing so We should be careful in deciding whether to do so There are MANY different monitors with MANY different resolutions A fixed size window may look great on the designer’s monitor but be totally unusable on another – because a button or a text box or a list control is off the visible window Be careful in choosing colors for the same reason – the designer may have a black wallpaper so a white font looks good while the user may have a white wallpaper, for example What looks good on one monitor may not be usable on another The solution: test, test, test on many different resolutions and color schemes Controls, Events, and more November 7, 2018

Controls Without Visual Representations Controls that perform a service but do not have visual representations Controls, Events, and more November 7, 2018

Non-Visual Controls Most Windows controls have a visual representation on a Windows form Label TextBox Button And so forth A few controls, however, provide a service without having a visual representation One example is the timer control Controls, Events, and more November 7, 2018

Timer Control Many applications may use a timer Network or database connection “times out” if inactive for some designated period An operating system “shares” processor time among multiple applications Educational software may give a timed test Some electronic card or board games may give each player a time limit in which to make a play Some applications may need a “clock” display The timer control may be used to facilitate some of these types of activities Controls, Events, and more November 7, 2018

Using the Timer Control Drag the Timer Control from the Toolbox onto the form and drop it anywhere The timer control “bounces off” the form and lands in the tray below the form Drag and Drop Controls, Events, and more November 7, 2018

Using the Timer Control Like other controls, the timer control has a set of properties that may be set/used Object name in program Must enable the timer to use it Interval until a “Tick” event occurs. Measured in milliseconds (5000 represents 5 seconds) Controls, Events, and more November 7, 2018

Using the Timer When the timer is enabled during program execution, the timer runs When the timer interval expires, a Tick event occurs This event may be handled or ignored like other events As long as the timer is enabled, it starts timing again immediately so that the Tick event will occur periodically – at the end of each specified interval Controls, Events, and more November 7, 2018

Tick Event Handler Double-clicking on the timer in the tray in the design view generates a skeleton Tick event handler One may add code to take appropriate action No need to reset the timer – that is automatic unless the timer is disabled or stopped by the program A digital clock application takes only a few lines of code Controls, Events, and more November 7, 2018

Digital Clock Application Form below has one Label with a Blue Background and Gold Foreground (for ETSU) The Label is docked (its Dock property is set to Fill) to Fill the client area of the Form with the Form sized appropriately for the application Fill Blue area is the Label’s background Controls, Events, and more November 7, 2018

Digital Clock Application Form_Load event initializes the clock: sets form caption with the date and sets clock to current time Update the date and time every time the timer expires (set to one second interval) Controls, Events, and more November 7, 2018

Digital Clock Application Controls, Events, and more November 7, 2018

More on events and event handlers Enter and KeyPress events; using the handler arguments; doing double duty Controls, Events, and more November 7, 2018

The Enter Event For some controls such as TextBox, the Enter event occurs when they gain the focus (i.e., when program control or the “input” focus “enters” them) The Leave event occurs when the control loses focus The handler for the Enter event may take appropriate actions such as SelectAll the text, Clear the text box, or validate its contents The Enter event is just one of many in the event list found in the Properties window for many controls Controls, Events, and more November 7, 2018

Double-click any event to generate a skeleton of its event-handler Enter Event Return to properties Double-click any event to generate a skeleton of its event-handler Event List Enter Event Controls, Events, and more November 7, 2018

Now we can use TextBox methods on tb Enter Event Handler The code below was intended for use with the TextBox named txtPoints as seen from the name of the handler However, we can make it more general by determining what control was “entered” from the sender argument As an “Object”, we have to cast the sender as a more specific type (TextBox) in this case before we can use it as such (using the unique methods of the more specific type, for example) Cast sender as TextBox Now we can use TextBox methods on tb Controls, Events, and more November 7, 2018

Kill 2 birds with one stone In the previous example, we can make the same event handler handle the same event for other TextBoxes as well We wrote the body of the handler to be general enough to handle any TextBox in this case Now, we simply need to register this handler for other TextBox Enter events Go to the .Designer.cs file for this form Locate the txtPoints Enter event handler registration in the generated code Copy to any other TextBox control and rename as needed Controls, Events, and more November 7, 2018

… 2 birds … Copy this line Paste here Now, one event handler handles the Enter event for both txtPoints and txtHours Change to txtHours Controls, Events, and more November 7, 2018

One handler worked for both TextBoxes See Results After calculating one GPA, if we want to do another … it helps to have this value “selected” so that typing replaces what is here Both TextBox contents are selected as the focus “enters” them, allowing easy editing One handler worked for both TextBoxes Controls, Events, and more November 7, 2018

KeyPress Event One way to help avoid situations in which bad input causes program crashes is to limit what the user is allowed to type into a TextBox or other control For example, if a user is to type the number of credits earned, then only digits should be typed If a user is to enter a number, then only characters such as digits, +, -, and . are allowed Should also allow a backspace in case the user types something incorrect and wants to erase and correct it The KeyPress event occurs each time the user presses a key Controls, Events, and more November 7, 2018

KeyPress Event The KeyPressEventArgs argument e to the handler for this event has a string property named KeyChar that identifies the key pressed One may interrogate this value to determine whether to process or ignore the pressed key - typically, an if statement is used To ignore a character, set the (already) Handled property of e to true The character will not even be echoed to the screen since the event has been marked as completely handled in this case Controls, Events, and more November 7, 2018

KeyPress Event Handler Example If character typed is less than ‘0’ … … or greater than ‘9’ ( then it cannot be a digit) … … and it is not a backspace … … THEN we mark it as completely handled since we are discarding the offending character Controls, Events, and more November 7, 2018

Register KeyPress Handler for Other TextBoxes Copy this line … … to here and change object name to txtHours Register this handler for any other TextBoxes to which it applies Controls, Events, and more November 7, 2018

More Complex KeyPress Event Handler Should be == instead of != Controls, Events, and more November 7, 2018

Other controls Many different controls are available … Controls, Events, and more November 7, 2018

Some other controls Datetime picker Month Calendar Menu Strip Groupbox Checkboxes Numeric up-down Combo Box Progress Bar Picture Box List Box Status Strip Controls, Events, and more November 7, 2018

For a “tabbed” interface on a form Controls Windows Forms Controls listed in the Toolbar … others can be added to the Toolbar For a “tabbed” interface on a form Controls, Events, and more November 7, 2018

MenuStrip MenuStrip One can add a new Menu item by simply typing it here An item can be added to the drop down by typing it here From the right-click context menu, one can add a separator line, a TextBox, or a ComboBox to the menu Once all items have been added, double click on a menu item to generate skeleton of an event handler for it Controls, Events, and more November 7, 2018

A ListBox is designed to display a list of strings An item may be selected by clicking on it. Depending on ListBox properties, one may select multiple items Scroll bar is only visible if there are too many items in the list to show all at one time A ListBox is designed to display a list of strings Controls, Events, and more November 7, 2018

ListBox One may manually add items to the ListBox in the code (or in the design view) This example shows adding items in code Programmatically selects the first item in the list – exception if list is empty Controls, Events, and more November 7, 2018

List Contents at Design Time ListBox Initialize items in the ListBox in the design view Collection Editor List Contents at Design Time Select Items Controls, Events, and more November 7, 2018

Set the DataSource for MyListBox to be the array named MyData ListBox – Data Binding One may bind the contents of a ListBox to a collection object such as an array or List<type> Set the DataSource for MyListBox to be the array named MyData Controls, Events, and more November 7, 2018

Changing the DataSource Controls, Events, and more November 7, 2018

Selection Changed Handling When an item in a ListBox is selected by clicking on it, a SelectedIndexChanged event occurs It may be handled to take some action Retrieve the selected item Controls, Events, and more November 7, 2018

This picture is displayed in a PictureBox control The PictureBox control is designed to make it easy to display a picture as illustrated here PictureBox control This picture is displayed in a PictureBox control Controls, Events, and more November 7, 2018

Using a Picture Box Control The image may be set at design time using a property Some PictureBox properties are shown here This picture comes from the Resources file Set the image property Controls, Events, and more November 7, 2018

Resources Can be Set from Project Properties May Add new Resources Resources tab of the Project Properties Dialog Controls, Events, and more November 7, 2018

Adding an Image to a Resource Can add existing files Can add new pictures in any of these formats Controls, Events, and more November 7, 2018

Internationalizing a Program Controls, Events, and more November 7, 2018

Internationalizing a Program Replace EVERY literal string in the code by a reference to a string resource Replace the resources but not the code to have the program run in a different language Controls, Events, and more November 7, 2018

Setting the Picture from Code One may also set the Image property of the control at runtime Controls, Events, and more November 7, 2018

Create Your Own Web Browser You may use the WebBrowser control to create your own web browser or to create web browsing capability as part of a larger desktop application Controls, Events, and more November 7, 2018

The Web Browser Application WebBrowser control – docked to fill rest of the form Panel control with a Label, TextBox, and Button on it – docked at the top of the form Controls, Events, and more November 7, 2018

Code for the WebBrowser App This is all of the code required… When user types a new URL and clicks Go, update the WebBrowser’s URL from the TextBox When user clicks a link, update the TextBox Controls, Events, and more November 7, 2018