Chapter 13 graphical user interface concepts part 1
Download
1 / 28

2007 Dr. Natheer Khasawneh. Chapter 13. Graphical User ... - PowerPoint PPT Presentation


  • 299 Views
  • Uploaded on

Chapter 13. Graphical User Interface Concepts: Part 1 Simple Windows Application In Visual Studio 2005, if you choose File->New Project Windows Application The following will be generated for you Form1.cs Form1.Designer.cs Program.cs Lets look at them Program.cs using System;

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about '2007 Dr. Natheer Khasawneh. Chapter 13. Graphical User ...' - ostinmannual


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

Simple windows application l.jpg
Simple Windows Application

  • In Visual Studio 2005, if you choose

    • File->New Project

    • Windows Application

  • The following will be generated for you

    • Form1.cs

    • Form1.Designer.cs

    • Program.cs

  • Lets look at them


Program cs l.jpg
Program.cs

using System;

using System.Collections.Generic;

using System.Windows.Forms;

namespace SimpleApplication

{

static class Program

{

///<summary>

/// The main entry point for the application.

///</summary>

[STAThread]

static void Main()

{

Application.EnableVisualStyles();

Application.SetCompatibleTextRenderingDefault(false);

Application.Run(new Form1());

}

}

}

What is this?


Form1 cs l.jpg
Form1.cs

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Windows.Forms;

namespace SimpleApplication

{

public partial class Form1 : Form

{

public Form1()

{

InitializeComponent();

}

}

}


Form1 designer cs l.jpg
Form1.Designer.cs

namespace SimpleApplication

{

partial class Form1

{

///<summary>

/// Required designer variable.

///</summary>

private System.ComponentModel.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 InitializeComponent()

{

this.components = new System.ComponentModel.Container();

this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;

this.Text = "Form1";

}

#endregion

}

}


Application class l.jpg
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:

    • DoEvents()

    • Exit()

    • ExitThread()

    • Run()

  • The class also has events and properties to synchronize execution.


Application class8 l.jpg
Application Class

  • Run static method begins running a standard application message loop on the current thread.

  • Below is the overload list:


Partial class l.jpg
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


Windows forms l.jpg
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 ->

    MarshallByRefObject ->

    Component->

    Control->

    ScrollableControl->

    Form



Simpleform cs l.jpg
SimpleForm.cs

using System;

using System.Windows.Forms;

class SimpleForm

{

static void Main()

{

Form f1 = new Form();

f1.Text = "My Simple Form";

Application.Run(f1);

}

}

csc /t:winexe SimpleForm.cs


Simpleform2 cs l.jpg
SimpleForm2.cs

using System;

using System.Windows.Forms;

class SimpleForm

{

static void Main()

{

Form f1 = new Form();

f1.Text = "My Simple Form2";

f1.Show();

}

}

What is the output?


Simpleform3 cs l.jpg
SimpleForm3.cs

using System.Threading;

using System.Windows.Forms;

class SimpleForm

{

static void Main()

{

Form f1 = new Form();

f1.Text = "My Simple Form3";

f1.Show();

f1.Text = "Time to sleep";

//Let the process go to sleep for 1000 ms (pause)

Thread.Sleep(3000);

f1.Text = "Time to wake up !";

//Let the process go to sleep for 1000 ms (pause)

Thread.Sleep(1000);

}

}

What is the output?


Simpleform4 cs l.jpg
SimpleForm4.cs

using System.Threading;

using System.Windows.Forms;

class SimpleForm

{

public static void Main()

{

Form f1 = new Form();

f1.Text = "What is wrong with this?";

f1.Visible = true; Thread.Sleep(3000);

f1.Visible = false; Thread.Sleep(3000);

f1.Text = "Coming back to live";

f1.Visible = true; Thread.Sleep(3000);

Application.Run();

f1.Text = "After Application.Run is called";

}

}

What is the output?


Simpleform5 cs l.jpg
SimpleForm5.cs

using System.Threading;

using System.Windows.Forms;

class SimpleForm

{

public static void Main()

{

Form f1 = new Form();

f1.Text = "What is wrong with this?";

f1.Visible = true; Thread.Sleep(3000);

f1.Visible = false; Thread.Sleep(3000);

f1.Text = "Coming back to live";

f1.Visible = true; Thread.Sleep(3000);

Application.Run(f1);

Thread.Sleep(3000);

f1.Text = "After Application.Run is called";

}

}

What is the output?


Simpleform6 cs l.jpg
SimpleForm6.cs

using System.Threading;

using System.Windows.Forms;

class SimpleForm

{

public static void Main()

{

Form f1 = new Form();

f1.Text = "How are you all doing?";

f1.Visible = true;

Thread.Sleep(3000);

f1.Text = "And How is your Baba?";

Application.Run(f1);

Thread.Sleep(3000);

f1.Text = "After Application.Run is called";

MessageBox.Show("And your Daddy too?");

}

}

What is the output?


Conclusion l.jpg
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 MessageBox.Show method.

  • The application ends after the MessageBox is dismissed


Simpleform7 cs l.jpg
SimpleForm7.cs

using System.Threading;

using System.Windows.Forms;

class SimpleForm

{

public static void Main()

{

Form f0 = new Form(); f0.Text = "How are you?";

Form f1 = new Form(); f1.Text = "How is C#?";

Form f2 = new Form(); f2.Text = "How is C++";

f0.Show(); f2.Show();

Application.Run(f1);

MessageBox.Show("Where are we?");

f2.Text = "F2, after Application.Run is called";

Thread.Sleep(3000);

}

}

Take Home

Bring back your description

Quiz


Not very simple windows application l.jpg
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 Form1.cs Form1.Designer.cs


Program cs21 l.jpg
Program.cs

using System;

using System.Collections.Generic;

using System.Windows.Forms;

namespace SimpleApplication

{

static class Program

{

///<summary>

/// The main entry point for the application.

///</summary>

[STAThread]

static void Main()

{

Application.EnableVisualStyles();

Application.SetCompatibleTextRenderingDefault(false);

Application.Run(new Form1());

}

}

}

NO CHANGES!


Form1 cs22 l.jpg
Form1.cs

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Windows.Forms;

namespace SimpleApplication

{

public partial class Form1 : Form

{

public Form1()

{

InitializeComponent();

}

}

}

NO CHANGES!


Form1 designer cs23 l.jpg
Form1.Designer.cs

We have two new fields

namespace SimpleApplication

{

partial class Form1

{

//SAME AS BEFORE CODE OMMITED FOR SPACE

private System.Windows.Forms.TextBox textBox1;

private System.Windows.Forms.Button button1;

private void InitializeComponent()

{

this.textBox1 = new System.Windows.Forms.TextBox();

this.button1 = new System.Windows.Forms.Button();

this.SuspendLayout();

// textBox1

this.textBox1.Location = new System.Drawing.Point(72, 24);

this.textBox1.Name = "textBox1";

this.textBox1.Size = new System.Drawing.Size(100, 20);

this.textBox1.TabIndex = 0;

// button1

this.button1.Location = new System.Drawing.Point(82, 59);

this.button1.Name = "button1";

this.button1.Size = new System.Drawing.Size(75, 23);

this.button1.TabIndex = 1;

this.button1.Text = "button1";

this.button1.UseVisualStyleBackColor = true;

// Form1

this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);

this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;

this.ClientSize = new System.Drawing.Size(246, 109);

this.Controls.Add(this.button1);

this.Controls.Add(this.textBox1);

this.Name = "Form1";

this.Text = "Form1";

this.ResumeLayout(false);

this.PerformLayout();}

#endregion

}

}

The fields properties are initialized

The fields are attached to the Form


Not simple windows application l.jpg
Not Simple Windows Application!

  • Lets say I want to find the square of the number entered

  • Double click on the button, and suddenly Form1.cs is opened for me and the following code is displayed for me.


Form1 cs25 l.jpg
Form1.cs

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Windows.Forms;

namespace SimpleApplication

{

public partial class Form1 : Form

{

public Form1()

{

InitializeComponent();

}

private void button1_Click(object sender, EventArgs e)

{

int x = int.Parse(textBox1.Text);

x = x*x;

MessageBox.Show(x.ToString());

}

}

}

This method is written for me

I wrote this code


Form1 designer cs26 l.jpg
Form1.Designer.cs

namespace SimpleApplication

{

partial class Form1

{

//SAME AS BEFORE CODE OMMITED FOR SPACE

//THE BUTTON1 IS THE PART THAT CHANGES

// button1

//

this.button1.Location = new System.Drawing.Point(82, 59);

this.button1.Name = "button1";

this.button1.Size = new System.Drawing.Size(75, 23);

this.button1.TabIndex = 1;

this.button1.Text = "button1";

this.button1.UseVisualStyleBackColor = true;

this.button1.Click += new System.EventHandler(this.button1_Click);

}

}

control

Event

Event handler

(method name)


What we just did l.jpg
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)

  • button1_Click method will be executed.


Simpleform8 cs i like this code l.jpg
SimpleForm8.csI like this code..

using System;

using System.Threading;

using System.Windows.Forms;

class SimpleForm

{

public static void Main()

{

MyForm f1 = new MyForm();

Application.Run(f1);

}

}

class MyForm : Form

{

private TextBox textBox1;

private Button button1;

public MyForm()

{

this.textBox1 = new System.Windows.Forms.TextBox();

this.button1 = new System.Windows.Forms.Button();

this.textBox1.Location = new System.Drawing.Point(72, 24);

this.textBox1.Name = "textBox1";

this.button1.Location = new System.Drawing.Point(82, 59);

this.button1.Name = "button1";

this.button1.Text = "button1";

this.button1.Click += new System.EventHandler(this.button1_Click);

this.Controls.Add(this.button1);

this.Controls.Add(this.textBox1);

this.Name = "Form1";

this.Text = "Form1";

}

private void button1_Click(object sender, EventArgs e)

{

int x = int.Parse(textBox1.Text);

x = x * x;

MessageBox.Show(x.ToString());

}

}


ad