Chapter 13 Graphical User Interface Concepts Part 1

  • Slides: 32
Download presentation
Chapter 13. Graphical User Interface Concepts: Part 1 2007 Dr. Natheer Khasawneh.

Chapter 13. Graphical User Interface Concepts: Part 1 2007 Dr. Natheer Khasawneh.

Simple Windows Application • In Visual Studio 2005, if you choose – File->New Project

Simple Windows Application • In Visual Studio 2005, if you choose – File->New Project – Windows Application • The following will be generated for you – Form 1. cs – Form 1. Designer. cs – Program. cs • Lets look at them 2007 Dr. Natheer Khasawneh.

 2007 Dr. Natheer Khasawneh.

2007 Dr. Natheer Khasawneh.

Program. cs using System; using System. Collections. Generic; using System. Windows. Forms; namespace Simple.

Program. cs using System; using System. Collections. Generic; using System. Windows. Forms; namespace Simple. Application { static class Program { /// <summary> /// The main entry point for the application. /// </summary> What is this? [STAThread] static void Main() { Application. Enable. Visual. Styles(); Application. Set. Compatible. Text. Rendering. Default(false); Application. Run(new Form 1()); } } } 2007 Dr. Natheer Khasawneh.

Form 1. cs using System; using System. Collections. Generic; using System. Component. Model; using

Form 1. cs using System; using System. Collections. Generic; using System. Component. Model; using System. Data; using System. Drawing; using System. Text; using System. Windows. Forms; namespace Simple. Application { public partial class Form 1 : Form { public Form 1() { Initialize. Component(); } } } 2007 Dr. Natheer Khasawneh.

Form 1. Designer. cs namespace Simple. Application { partial class Form 1 { ///

Form 1. Designer. cs namespace Simple. Application { partial class Form 1 { /// <summary> /// Required designer variable. /// </summary> private System. Component. Model. IContainer components = null; /// <summary> /// Clean up any resources being used. /// </summary> /// <param name="disposing">true if managed resources should be disposed; otherwise, false. </param> protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components. Dispose(); } base. Dispose(disposing); } #region Windows Form Designer generated code /// <summary> /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// </summary> private void Initialize. Component() { this. components = new System. Component. Model. Container(); this. Auto. Scale. Mode = System. Windows. Forms. Auto. Scale. Mode. Font; this. Text = "Form 1"; } #endregion } } 2007 Dr. Natheer Khasawneh.

Application Class • The System. Windows. Forms. Application class wraps the basic functionality to

Application Class • The System. Windows. Forms. Application class wraps the basic functionality to start a. NET application. • This class has methods to start and stop applications and their threads, and to process Windows Messages: – Do. Events() – Exit. Thread() – Run() • The class also has events and properties to synchronize execution. 2007 Dr. Natheer Khasawneh.

Application Class • Run static method begins running a standard application message loop on

Application Class • Run static method begins running a standard application message loop on the current thread. • Below is the overload list: 2007 Dr. Natheer Khasawneh.

Partial Class • A partial class, or partial type, is a feature of C#

Partial Class • A partial class, or partial type, is a feature of C# programming languages in which the declaration of a class or a struct or an interface may be split across multiple source-code files. • Purpose – Very large classes – Separation of concerns – Multiple developer 2007 Dr. Natheer Khasawneh.

Windows Forms • Forms are contained within a namespace called System. Windows. Forms that

Windows Forms • Forms are contained within a namespace called System. Windows. Forms that contains around 200 classes, 100 enumeration, more than 40 delegates, 7 interfaces, and 4 structures. • The inheritance lineage is Object -> Marshall. By. Ref. Object -> Component-> Control-> Scrollable. Control-> Form 2007 Dr. Natheer Khasawneh.

Windows Forms 2007 Dr. Natheer Khasawneh.

Windows Forms 2007 Dr. Natheer Khasawneh.

Form • Form class represents window, provides appropriate services – properties: Size, Location, Controls,

Form • Form class represents window, provides appropriate services – properties: Size, Location, Controls, Show. In. Taskbar – methods: Show, Close, Set. Desktop. Location – events: Load, Click, Closing • Common to derive from Form to create custom form define custom form set properties class My. Form : Form { public My. Form() { this. Show. In. Taskbar = false; this. Location = new Point(10, 10); this. Size = new Size(100, 100); } } 2007 Dr. Natheer Khasawneh.

Form border • Forms can look like windows, dialogs, or tools – controlled using

Form border • Forms can look like windows, dialogs, or tools – controlled using Form. Border. Style property – set Show. In. Taskbar property to false for tool windows – window border styles show the icon from the Icon property 2007 Dr. Natheer Khasawneh.

Form properties • Form has many properties – used to customize appearance and behavior

Form properties • Form has many properties – used to customize appearance and behavior public class Form : Container. Control { public IButton. Control Accept. Button { get; set; } public IButton. Control Cancel. Button { get; set; } public bool Help. Button { get; set; } public Icon { get; set; } public String Text { get; set; } public Size Maximum. Size { get; set; } public Size Minimum. Size { get; set; } properties public Main. Menu { get; set; } public bool Show. In. Taskbar { get; set; } } public Form. Border. Style { get; set; }. . . 2007 Dr. Natheer Khasawneh.

Simple. Form. cs using System; using System. Windows. Forms; class Simple. Form csc /t:

Simple. Form. cs using System; using System. Windows. Forms; class Simple. Form csc /t: winexe Simple. Form. cs { static void Main() { Form f 1 = new Form(); f 1. Text = "My Simple Form"; Application. Run(f 1); } } 2007 Dr. Natheer Khasawneh.

Simple. Form 2. cs using System; using System. Windows. Forms; class Simple. Form {

Simple. Form 2. cs using System; using System. Windows. Forms; class Simple. Form { static void Main() { Form f 1 = new Form(); f 1. Text = "My Simple Form 2"; f 1. Show(); } } 2007 Dr. Natheer Khasawneh. What is the output?

Simple. Form 3. cs using System. Threading; using System. Windows. Forms; class Simple. Form

Simple. Form 3. cs using System. Threading; using System. Windows. Forms; class Simple. Form { static void Main() What is the output? { Form f 1 = new Form(); f 1. Text = "My Simple Form 3"; f 1. Show(); f 1. Text = "Time to sleep"; //Let the process go to sleep for 1000 ms (pause) Thread. Sleep(3000); f 1. Text = "Time to wake up !"; //Let the process go to sleep for 1000 ms (pause) Thread. Sleep(1000); } } 2007 Dr. Natheer Khasawneh.

Simple. Form 4. cs using System. Threading; using System. Windows. Forms; class Simple. Form

Simple. Form 4. cs using System. Threading; using System. Windows. Forms; class Simple. Form { public static void Main() { What is the output? Form f 1 = new Form(); f 1. Text = "What is wrong with this? "; f 1. Visible = true; Thread. Sleep(3000); f 1. Visible = false; Thread. Sleep(3000); f 1. Text = "Coming back to live"; f 1. Visible = true; Thread. Sleep(3000); Application. Run(); f 1. Text = "After Application. Run is called"; } } 2007 Dr. Natheer Khasawneh.

Simple. Form 5. cs using System. Threading; using System. Windows. Forms; class Simple. Form

Simple. Form 5. cs using System. Threading; using System. Windows. Forms; class Simple. Form { public static void Main() { What is the output? Form f 1 = new Form(); f 1. Text = "What is wrong with this? "; f 1. Visible = true; Thread. Sleep(3000); f 1. Visible = false; Thread. Sleep(3000); f 1. Text = "Coming back to live"; f 1. Visible = true; Thread. Sleep(3000); Application. Run(f 1); Thread. Sleep(3000); f 1. Text = "After Application. Run is called"; } } 2007 Dr. Natheer Khasawneh.

Simple. Form 6. cs using System. Threading; using System. Windows. Forms; class Simple. Form

Simple. Form 6. cs using System. Threading; using System. Windows. Forms; class Simple. Form { public static void Main() { What is the output? Form f 1 = new Form(); f 1. Text = "How are you all doing? "; f 1. Visible = true; Thread. Sleep(3000); f 1. Text = "And How is your Baba? "; Application. Run(f 1); Thread. Sleep(3000); f 1. Text = "After Application. Run is called"; Message. Box. Show("And your Daddy too? "); } } 2007 Dr. Natheer Khasawneh.

Conclusion • Main creates the form and displays it. • The form is given

Conclusion • Main creates the form and displays it. • The form is given to Application. Run where it continues to exist. • The form takes control of execution until the form is dismissed. • When the form is dismissed, presumably, the Application. Run method continues (err, ends). • The Application. Run method returns control to Main, which then calls the Message. Box. Show method. • The application ends after the Message. Box is dismissed 2007 Dr. Natheer Khasawneh.

Simple. Form 7. cs using System. Threading; using System. Windows. Forms; Take Home Bring

Simple. Form 7. cs using System. Threading; using System. Windows. Forms; Take Home Bring back your description class Simple. Form Quiz { public static void Main() { Form f 0 = new Form(); f 0. Text = "How are you? "; Form f 1 = new Form(); f 1. Text = "How is C#? "; Form f 2 = new Form(); f 2. Text = "How is C++"; f 0. Show(); f 2. Show(); Application. Run(f 1); Message. Box. Show("Where are we? "); f 2. Text = "F 2, after Application. Run is called"; Thread. Sleep(3000); } } 2007 Dr. Natheer Khasawneh.

Not Very Simple Windows Application! • Drag and drop textbox and button from the

Not Very Simple Windows Application! • Drag and drop textbox and button from the toolbox into the Form • Lets see what happened to our three files. • Program. cs Form 1. Designer. cs 2007 Dr. Natheer Khasawneh.

Program. cs using System; using System. Collections. Generic; using System. Windows. Forms; NO CHANGES!

Program. cs using System; using System. Collections. Generic; using System. Windows. Forms; NO CHANGES! namespace Simple. Application { static class Program { /// <summary> /// The main entry point for the application. /// </summary> [STAThread] static void Main() { Application. Enable. Visual. Styles(); Application. Set. Compatible. Text. Rendering. Default(false); Application. Run(new Form 1()); } } } 2007 Dr. Natheer Khasawneh.

Form 1. cs using System; using System. Collections. Generic; using System. Component. Model; using

Form 1. cs using System; using System. Collections. Generic; using System. Component. Model; using System. Data; using System. Drawing; using System. Text; using System. Windows. Forms; namespace Simple. Application { public partial class Form 1 : Form { public Form 1() { Initialize. Component(); } } } 2007 Dr. Natheer Khasawneh. NO CHANGES!

namespace Simple. Application { partial class Form 1 { //SAME AS BEFORE CODE OMMITED

namespace Simple. Application { partial class Form 1 { //SAME AS BEFORE CODE OMMITED FOR SPACE private System. Windows. Forms. Text. Box text. Box 1; private System. Windows. Forms. Button button 1; private void Initialize. Component() { this. text. Box 1 = new System. Windows. Forms. Text. Box(); this. button 1 = new System. Windows. Forms. Button(); this. Suspend. Layout(); // text. Box 1 this. text. Box 1. Location = new System. Drawing. Point(72, 24); this. text. Box 1. Name = "text. Box 1"; this. text. Box 1. Size = new System. Drawing. Size(100, 20); this. text. Box 1. Tab. Index = 0; // button 1 this. button 1. Location = new System. Drawing. Point(82, 59); this. button 1. Name = "button 1"; this. button 1. Size = new System. Drawing. Size(75, 23); this. button 1. Tab. Index = 1; this. button 1. Text = "button 1"; this. button 1. Use. Visual. Style. Back. Color = true; // Form 1 this. Auto. Scale. Dimensions = new System. Drawing. Size. F(6 F, 13 F); this. Auto. Scale. Mode = System. Windows. Forms. Auto. Scale. Mode. Font; this. Client. Size = new System. Drawing. Size(246, 109); this. Controls. Add(this. button 1); this. Controls. Add(this. text. Box 1); this. Name = "Form 1"; this. Text = "Form 1"; this. Resume. Layout(false); this. Perform. Layout(); } #endregion } 2007 Dr. Natheer Khasawneh. } Form 1. Designer. cs We have two new fields The fields properties are initialized The fields are attached to the Form

Not Simple Windows Application! • Lets say I want to find the square of

Not Simple Windows Application! • Lets say I want to find the square of the number entered • Double click on the button, and suddenly Form 1. cs is opened for me and the following code is displayed for me. 2007 Dr. Natheer Khasawneh.

Form 1. cs using System; using System. Collections. Generic; using System. Component. Model; using

Form 1. cs using System; using System. Collections. Generic; using System. Component. Model; using System. Data; using System. Drawing; using System. Text; using System. Windows. Forms; namespace Simple. Application { public partial class Form 1 : Form { public Form 1() { Initialize. Component(); } private void button 1_Click(object sender, Event. Args e) { int x = int. Parse(text. Box 1. Text); x = x*x; Message. Box. Show(x. To. String()); } } } 2007 Dr. Natheer Khasawneh. This method is written for me I wrote this code

Form 1. Designer. cs namespace Simple. Application { partial class Form 1 { //SAME

Form 1. Designer. cs namespace Simple. Application { partial class Form 1 { //SAME AS BEFORE CODE OMMITED FOR SPACE //THE BUTTON 1 IS THE PART THAT CHANGES // button 1 // this. button 1. Location = new System. Drawing. Point(82, 59); this. button 1. Name = "button 1"; this. button 1. Size = new System. Drawing. Size(75, 23); this. button 1. Tab. Index = 1; this. button 1. Text = "button 1"; this. button 1. Use. Visual. Style. Back. Color = true; this. button 1. Click += new System. Event. Handler(this. button 1_Click); } } control Event handler (method name) 2007 Dr. Natheer Khasawneh.

Form as container • Form manages a list of controls – stored in Controls

Form as container • Form manages a list of controls – stored in Controls collection property all forms inherit Controls collection class My. Form : Form { Button button 1 = new Button(); Label label 1 = new Label (); add to collection } public My. Form() {. . . this. Controls. Add(button 1); this. Controls. Add(label 1 ); }. . . my. Form Controls 2007 Dr. Natheer Khasawneh. button 1 label 1

What We Just Did? • We have attached an event handler for the click

What We Just Did? • We have attached an event handler for the click event. • Button has Click event • If this event happens (some one clicks on the button) • button 1_Click method will be executed. 2007 Dr. Natheer Khasawneh.

using System; using System. Threading; using System. Windows. Forms; class Simple. Form { public

using System; using System. Threading; using System. Windows. Forms; class Simple. Form { public static void Main() { My. Form f 1 = new My. Form(); Application. Run(f 1); } } class My. Form : Form { private Text. Box text. Box 1; private Button button 1; Simple. Form 8. cs I like this code. . public My. Form() { this. text. Box 1 = new System. Windows. Forms. Text. Box(); this. button 1 = new System. Windows. Forms. Button(); this. text. Box 1. Location = new System. Drawing. Point(72, 24); this. text. Box 1. Name = "text. Box 1"; this. button 1. Location = new System. Drawing. Point(82, 59); this. button 1. Name = "button 1"; this. button 1. Text = "button 1"; this. button 1. Click += new System. Event. Handler(this. button 1_Click); this. Controls. Add(this. button 1); this. Controls. Add(this. text. Box 1); this. Name = "Form 1"; this. Text = "Form 1"; } private void button 1_Click(object sender, Event. Args e) { int x = int. Parse(text. Box 1. Text); x = x * x; Message. Box. Show(x. To. String()); } } 2007 Dr. Natheer Khasawneh.