Lecture 7: WinForms & Controls, Part 2. 7-2 MicrosoftIntroducing CS using.NETJ# in Visual Studio.NET Objectives “Visual Studio.NET ships with a wealth.

Slides:



Advertisements
Similar presentations
Information System Design Lab 5&6. User Interface Design.
Advertisements

© by Pearson Education, Inc. All Rights Reserved.
© by Pearson Education, Inc. All Rights Reserved. continued …
 2006 Pearson Education, Inc. All rights reserved Introduction to the Visual C# 2005 Express Edition IDE.
1 Visual Basic.NET Application Overview. 2 Objectives Discuss what a typical Visual Basic.NET application looks like Configure the Visual Studio.NET Integrated.
Flowchart Start Input weight and height
1 Introduction to the Visual Studio.NET IDE Powerpoint slides modified from Deitel & Deitel.
Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter One An Introduction to Visual Basic 2010.
2. Introduction to the Visual Studio.NET IDE 2. Introduction to the Visual Studio.NET IDE Ch2 – Deitel’s Book.
Microsoft Visual Basic 2012 CHAPTER TWO Program and Graphical User Interface Design.
Microsoft Visual Basic 2005 CHAPTER 8 Using Procedures and Exception Handling.
Microsoft Visual Basic 2012 CHAPTER TWELVE (ENRICHMENT CHAPTER) Windows Store Apps.
Microsoft Visual Basic 2012 Using Procedures and Exception Handling CHAPTER SEVEN.
CIS 338: Creating ActiveX Controls Dr. Ralph D. Westfall March, 2003.
Programming with Microsoft Visual Basic 2012 Chapter 12: Web Applications.
Microsoft Visual Basic 2008 CHAPTER 8 Using Procedures and Exception Handling.
Introduction to Visual Basic. Quick Links Windows Application Programming Event-Driven Application Becoming familiar with VB Control Objects Saving and.
IE 411/511: Visual Programming for Industrial Applications
Chapter 3 Introducing Visual Basic.NET. 3.1 Visual Basic.NET Windows Programming -Used to create Windows, Web, and Console applications -Uses predefined.
Jump to first page 10/2/2015 Learning PowerPoint A Tutorial Start.
Web Programming: Client/Server Applications Server sends the web pages to the client. –built into Visual Studio for development purposes Client displays.
Chapter 2 More Controls Programming in C#. NET © 2003 by The McGraw-Hill Companies, Inc. All rights reserved.
1 Working with Menus and Dialog Boxes. 2 Objectives You will be able to Create and edit menus for Windows Forms applications. Write code to handle menu.
Visual Basic 2005 CHAPTER 2 Program and Graphical User Interface Design.
 2009 Pearson Education, Inc. All rights reserved Introduction to the Visual Basic Express 2008 IDE.
 2009 Pearson Education, Inc. All rights reserved Dive Into ® Visual C# 2008 Express.
Jozef Goetz,  2006 Pearson Education, Inc. All rights reserved.  2002 Prentice Hall. All rights reserved. expanded by J. Goetz, 2009.
XP New Perspectives on Windows XP Tutorial 1 Exploring the Basics.
Copyright © 2001 by Wiley. All rights reserved. Chapter 2: Using Visual Basic to Create a First Project Getting Started with VB Development Environment.
 Application – another name for a program.  Interface – is what appears on the screen when the application is running.  Program Code – is instructions.
Chapter One An Introduction to Visual Basic 2010 Programming with Microsoft Visual Basic th Edition.
Program Design and Coding
Microsoft Visual Basic 2012 CHAPTER THREE Program Design and Coding.
Microsoft Visual Basic 2010 CHAPTER THREE Program Design and Coding.
Graphical User Interfaces 2 Tonga Institute of Higher Education.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved. 1 3 Welcome Application Introduction to Visual Programming.
Lecture 6: Introduction to Graphical User Interfaces (GUIs)
Chapter Two Creating a First Project in Visual Basic.
Joe Hummel, PhD Dept of Mathematics and Computer Science Lake Forest College
Visual Basic.NET BASICS Lesson 1 A First Look at Microsoft Visual Basic.NET.
 2006 Pearson Education, Inc. All rights reserved Introduction to the Visual C# 2005 Express Edition IDE.
C# GUI - Basics. Objectives.NET supports two types: WinForms, traditional, desktop GUI apps. WebForms – newer, for Web apps. Visual Studio.NET supports.
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.
Microsoft Visual Basic 2005 BASICS Lesson 1 A First Look at Microsoft Visual Basic.
Visual C# 2012 How to Program © by Pearson Education, Inc. All Rights Reserved.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 2.1 Test-Driving the Welcome Application 2.2.
More User Interface Controls User interface controls are located in the toolbox. Click the +/- signs to open/ close the different tabs of the tollbox.
Microsoft Visual Basic 2010 CHAPTER TWO Program and Graphical User Interface Design.
Lesson 1 - Understanding the Word Window and Creating a New Document
 2002 Prentice Hall. All rights reserved. 1 Chapter 2 – Introduction to the Visual Studio.NET IDE Outline 2.1Introduction 2.2Visual Studio.NET Integrated.
Windows 95/NT/XP. What is Windows 95/NT/XP n A program that sets up an environment for you to work in on your computer, based on colorful pictures (icons)
Chapter 2 More Controls Programming in C#. NET Objectives Use text boxes, group boxes, check boxes, radio buttons, and picture boxes effectively.
Visual Basic.NET Comprehensive Concepts and Techniques Chapter 3 Building an Application in the Visual Basic.NET Environment.
IE 411/511: Visual Programming for Industrial Applications Lecture Notes #2 Introduction to the Visual Basic Express 2010 Integrated Development Environment.
 2002 Prentice Hall. All rights reserved. 1 Introduction to the Visual Studio.NET IDE Outline Introduction Visual Studio.NET Integrated Development Environment.
 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.
2-1 Chapter 2 Using VB.NET to Create a First Solution.
Chapter 1: An Introduction to Visual Basic .NET
Chapter 1: An Introduction to Visual Basic 2015
Chapter 2 – Introduction to the Visual Studio .NET IDE
Program and Graphical User Interface Design
Visual studio 2010 SENG 403, Tutorial 2 SENG Winter 2011.
Introduction to the Visual C# 2005 Express Edition IDE
Using Procedures and Exception Handling
Program and Graphical User Interface Design
Chapter 2 – Introduction to the Visual Studio .NET IDE
Visual Studio.
6. WinForms 2003 C# GUI - Basics.
Presentation transcript:

Lecture 7: WinForms & Controls, Part 2

7-2 MicrosoftIntroducing CS using.NETJ# in Visual Studio.NET Objectives “Visual Studio.NET ships with a wealth of controls for building Graphical User Interfaces…” More WinForms & Controls Demos of: –Picture boxes –List boxes –Menus –Web browsers –and more!

7-3 MicrosoftIntroducing CS using.NETJ# in Visual Studio.NET Part 1 Picture Boxes and the SlideShow App…

7-4 MicrosoftIntroducing CS using.NETJ# in Visual Studio.NET SlideShow Application The SlideShow App reads & displays images from a folder: Images SlideShow.exe Images.txt

7-5 MicrosoftIntroducing CS using.NETJ# in Visual Studio.NET (1) Layout the GUI Layout the following controls on the form: MainMenu Label PictureBox TextBox Button

7-6 MicrosoftIntroducing CS using.NETJ# in Visual Studio.NET (2) Configure the controls Label: –Name (lblTitle), Font (Bold Italic), TextAlignment (MiddleCenter) PictureBox: –Name (picImageBox), BorderStyle (FixedSingle), SizeMode (CenterImage) TextBox: –Name (txtFileName), TabStop (False), TextAlign (Center) Buttons: –Name (cmdNext, cmdPrev) –Image ( C:\Program Files\MS VS.NET 2003\Common7\Graphics\icons\arrows ) MainMenu: –&File with E&xit, &Help with &About… Form: –Text, AcceptButton (cmdNext), View menu Tab Order (cmdNext 0, …)

7-7 MicrosoftIntroducing CS using.NETJ# in Visual Studio.NET (3) Run & test GUI Run & test controls… –check tab order –confirm that user can't type into text box –notice that resizing is a problem

7-8 MicrosoftIntroducing CS using.NETJ# in Visual Studio.NET (4) Resizing support Resizing support based on notion of Anchoring –controls anchor themselves to 1 or more corners of form –as form resizes, controls resize (or not) Example: –picture box should grow & shrink with form –set picture box's Anchor property to Top, Bottom, Left, Right so that it follows all 4 corners of form

7-9 MicrosoftIntroducing CS using.NETJ# in Visual Studio.NET (5) Maximize app on startup You can set form's WindowState property to Maximized if you want it to take up whole screen when run. Run & test, controls should properly resize…

7-10 MicrosoftIntroducing CS using.NETJ# in Visual Studio.NET (6) Programming… The application has two main components: –data structure for keeping track of the images –GUI form for displaying current image & interacting with user Let's build the data structure component first…

7-11 MicrosoftIntroducing CS using.NETJ# in Visual Studio.NET Creating the data structure App class defines main to initialize data structure & show form –data structure is an array of filenames, one per image… public class App { private static String HomeDir; // directory where.EXE is private static String[] Images; // array of image filenames private static int IndexOfCurrentImage; // index of image being displayed public static void main(String[] args) { HomeDir = System.AppDomain.get_CurrentDomain().get_BaseDirectory(); Images = System.IO.Directory.GetFiles(HomeDir + "Images\\"); IndexOfCurrentImage = 0; // first filename in array // run app by creating a form and asking.NET to "run" it… System.Windows.Forms.Application.Run( new Form1() ); } //main // > public class App { private static String HomeDir; // directory where.EXE is private static String[] Images; // array of image filenames private static int IndexOfCurrentImage; // index of image being displayed public static void main(String[] args) { HomeDir = System.AppDomain.get_CurrentDomain().get_BaseDirectory(); Images = System.IO.Directory.GetFiles(HomeDir + "Images\\"); IndexOfCurrentImage = 0; // first filename in array // run app by creating a form and asking.NET to "run" it… System.Windows.Forms.Application.Run( new Form1() ); } //main // >

7-12 MicrosoftIntroducing CS using.NETJ# in Visual Studio.NET Creating the data structure (cont..) Since we are defining main(), we must comment out the Visual Studio supplied version of main() inside Form1.jsl /** * The main entry point for the application. */ System.STAThread() */ /* * Comment out this version of main, since we * supplied our own. * * public static void main(String[] args) { Application.Run(new Form1()); } */ /** * The main entry point for the application. */ System.STAThread() */ /* * Comment out this version of main, since we * supplied our own. * * public static void main(String[] args) { Application.Run(new Form1()); } */ Alternatively our code could be placed inside Form1.jsl, but logically it should be separate.

7-13 MicrosoftIntroducing CS using.NETJ# in Visual Studio.NET Accessing the data structure App class provides methods for accessing data structure…. public static String GetCurrentImageFileName() { return Images[IndexOfCurrentImage]; } public static void NextImage() { IndexOfCurrentImage += 1; if (IndexOfCurrentImage >= Images.length) IndexOfCurrentImage = 0; } public static void PrevImage() { IndexOfCurrentImage -= 1; if (IndexOfCurrentImage < 0) IndexOfCurrentImage = Images.length - 1; } // >. public static String GetCurrentImageFileName() { return Images[IndexOfCurrentImage]; } public static void NextImage() { IndexOfCurrentImage += 1; if (IndexOfCurrentImage >= Images.length) IndexOfCurrentImage = 0; } public static void PrevImage() { IndexOfCurrentImage -= 1; if (IndexOfCurrentImage < 0) IndexOfCurrentImage = Images.length - 1; } // >

7-14 MicrosoftIntroducing CS using.NETJ# in Visual Studio.NET Accessing the Title file Finally, App class opens & returns contents of "Images.txt" file –which contains the images' title (e.g. “Family Pictures"). public static String GetImagesText() { String s, filename; filename = App.HomeDir + "Images.txt"; System.IO.StreamReader reader; reader = new System.IO.StreamReader(filename); s = reader.ReadToEnd(); reader.Close(); return s; } } //class. public static String GetImagesText() { String s, filename; filename = App.HomeDir + "Images.txt"; System.IO.StreamReader reader; reader = new System.IO.StreamReader(filename); s = reader.ReadToEnd(); reader.Close(); return s; } } //class

7-15 MicrosoftIntroducing CS using.NETJ# in Visual Studio.NET Programming the GUI Five events to program: –File >> Exit –Help >> About –Form Load –cmdNext Click –cmdPrev Click this.Close(); MessageBox.Show("SlideShow App written by..."); this.lblTitle.set_Text( App.GetImagesText() ); DisplayCurrentImage(); App.NextImage(); DisplayCurrentImage(); App.PrevImage(); DisplayCurrentImage(); this.Close(); MessageBox.Show("SlideShow App written by..."); this.lblTitle.set_Text( App.GetImagesText() ); DisplayCurrentImage(); App.NextImage(); DisplayCurrentImage(); App.PrevImage(); DisplayCurrentImage(); private static void DisplayCurrentImage() { String filename = App.GetCurrentImageFileName(); this.txtFileName.set_Text( System.IO.Path.GetFileName(filename) ); this.picImageBox.set_Image( System.Drawing.Image.FromFile(filename) ); } private static void DisplayCurrentImage() { String filename = App.GetCurrentImageFileName(); this.txtFileName.set_Text( System.IO.Path.GetFileName(filename) ); this.picImageBox.set_Image( System.Drawing.Image.FromFile(filename) ); }

7-16 MicrosoftIntroducing CS using.NETJ# in Visual Studio.NET Programming the GUI Before running it, be sure to have supplied: –the Images directory with image files only –The descriptive Images.txt file

7-17 MicrosoftIntroducing CS using.NETJ# in Visual Studio.NET (7) Run! App should cycle through the images, support resizing, etc.

7-18 MicrosoftIntroducing CS using.NETJ# in Visual Studio.NET Part 2 List Boxes and the Student Info App…

7-19 MicrosoftIntroducing CS using.NETJ# in Visual Studio.NET Student Info Application The Student Info App reads & displays student information: StudentInfo.exe students.txt

7-20 MicrosoftIntroducing CS using.NETJ# in Visual Studio.NET List boxes List boxes are essentially a visual data structure –display items are kept in an unbounded data structure –changes to data structure immediately reflected in list box You have a choice as to what you store in list box: –you can store strings, what are displayed as is –you can store objects, in which case obj.toString() is displayed –storing objects allows easy access to info later on…

7-21 MicrosoftIntroducing CS using.NETJ# in Visual Studio.NET Adding to a list box Student Info app adds Student objects during Form Load: –notice the entire student object is added to a list box item public class Form1 extends System.Windows.Forms.Form {. private java.util.ArrayList students; // data structure of Student objects... private void Form_Load(...) { this.students = StudentsIO.read("students.txt"); for (int i = 0; i < this.students.size(); i++) { Student s = (Student) this.students.get(i); this.lstStudents.get_Items().Add(s); } } //Load public class Form1 extends System.Windows.Forms.Form {. private java.util.ArrayList students; // data structure of Student objects... private void Form_Load(...) { this.students = StudentsIO.read("students.txt"); for (int i = 0; i < this.students.size(); i++) { Student s = (Student) this.students.get(i); this.lstStudents.get_Items().Add(s); } } //Load

7-22 MicrosoftIntroducing CS using.NETJ# in Visual Studio.NET Retrieving from a list box User selection triggers list box's SelectedIndexChanged event: –reference to selected item stored in SelectedItem property. private void lstStudents_SelectedIndexChanged(...) { Student s; if (this.lstStudents.get_SelectedItem() == null) // nothing selected return; // otherwise get selected student & display their info... s = (Student) this.lstStudents.get_SelectedItem(); this.txtID.set_Text( String.valueOf(s.getID()) ); this.txtGPA.set_Text( s.getFormattedGPA() ); } //SelectedIndexChanged. private void lstStudents_SelectedIndexChanged(...) { Student s; if (this.lstStudents.get_SelectedItem() == null) // nothing selected return; // otherwise get selected student & display their info... s = (Student) this.lstStudents.get_SelectedItem(); this.txtID.set_Text( String.valueOf(s.getID()) ); this.txtGPA.set_Text( s.getFormattedGPA() ); } //SelectedIndexChanged

7-23 MicrosoftIntroducing CS using.NETJ# in Visual Studio.NET Part 3 Additional controls…

7-24 MicrosoftIntroducing CS using.NETJ# in Visual Studio.NET Just the tip of the iceberg… Dialogs, toolbars, etc. Thousands of additional controls –.NET and ActiveX –right-click on Toolbox –Then "Add/Remove Items…" –Example! Select the COM tab add the Microsoft Web Browser control Use arrow keys to scroll through Toolbox controls see next page for usage…

7-25 MicrosoftIntroducing CS using.NETJ# in Visual Studio.NET Baby Browser Application Baby Browser App easily built from web browser control: public class Form1 extends... {. private void cmdGo_Click(...) { Object junk = null; // surf to URL entered by user... this.axWebBrowser1.Navigate(this.txtURL.get_Text(), junk, junk, junk, junk); } public class Form1 extends... {. private void cmdGo_Click(...) { Object junk = null; // surf to URL entered by user... this.axWebBrowser1.Navigate(this.txtURL.get_Text(), junk, junk, junk, junk); }

7-26 MicrosoftIntroducing CS using.NETJ# in Visual Studio.NET Summary.NET ships with a large assortment of GUI controls –even more are available from 3 rd parties, etc. –this was just a quick overview of what's possible –the sky's the limit!