Presentation is loading. Please wait.

Presentation is loading. Please wait.

Joe Hummel, PhD Dept of Mathematics and Computer Science Lake Forest College Lecture 4: Intro to GUIs and the.

Similar presentations


Presentation on theme: "Joe Hummel, PhD Dept of Mathematics and Computer Science Lake Forest College Lecture 4: Intro to GUIs and the."— Presentation transcript:

1 Joe Hummel, PhD Dept of Mathematics and Computer Science Lake Forest College hummel@lakeforest.edu hummel@lakeforest.edu Lecture 4: Intro to GUIs and the other.NET Languages

2 4-2 4. GUIs and Languages 4.1 Intro to GUIs Graphical User Interfaces Event-driven programming WinForms

3 4-3 4. GUIs and Languages A Simple GUI Application Consider a Calculator app for performing addition:

4 4-4 4. GUIs and Languages GUIs are Event-driven Idea is very simple: –individual user actions are translated into “events” by OS — mouse click, mouse move, keypress, window activation, etc. –events are passed, 1 by 1, to application for processing –this is how most GUIs are programmed (Java, X,.NET, etc.)… GUI App

5 4-5 4. GUIs and Languages Code-behind Events are handled by methods that live behind visual interface –known as "code-behind" –our job is to handle the events we care about…

6 4-6 4. GUIs and Languages GUIs in.NET In.NET you can build two types of GUI applications –WinForms: this is a traditional desktop Windows GUI application –WebForms: this is a web-based GUI application It’s incredibly easy and intuitive –approach made famous by Visual Basic in the 1990's…

7 4-7 4. GUIs and Languages Example — Let's Create Calculator App Step-by-step…

8 4-8 4. GUIs and Languages (1) Create Project Create a new project of type “Windows Application” –a blank form (window) will be created for you automatically –inherits default behavior from System.Windows.Forms.Form

9 4-9 4. GUIs and Languages (2) Configure Form The Properties window is your friend Form properties to consider: –Text, StartPosition, Font, FormBorderStyle, MaximizeBox, …

10 4-10 4. GUIs and Languages (3) Design UI Select desired controls from Toolbox… –Hover mouse over Toolbox to reveal –Drag-and-drop onto form –Position, resize, set properties

11 4-11 4. GUIs and Languages (4) Naming Conventions Control’s programmatic name is set via (Name) property Naming convention is a 3-letter prefix –lbl, txt, cmd, etc.

12 4-12 4. GUIs and Languages public partial class Form1 : Form { private void cmdAdd_Click(object sender, EventArgs e) { int i, j, k; i = System.Convert.ToInt32(this.txtNumber1.Text); j = System.Convert.ToInt32(this.txtNumber2.Text); k = i + j; string msg; msg = "Sum = " + k; System.Windows.Forms.MessageBox.Show(msg); } public partial class Form1 : Form { private void cmdAdd_Click(object sender, EventArgs e) { int i, j, k; i = System.Convert.ToInt32(this.txtNumber1.Text); j = System.Convert.ToInt32(this.txtNumber2.Text); k = i + j; string msg; msg = "Sum = " + k; System.Windows.Forms.MessageBox.Show(msg); } (5) Code Events of Interest Double-click on object to reveal default event handler Example: –Double-click on button to reveal Click event…

13 4-13 4. GUIs and Languages (6) Run! Press F5 at any time to run what you have… static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run( new Form1() ); } static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run( new Form1() ); }

14 4-14 4. GUIs and Languages 4.2 The Other.NET Languages VB C++ J#

15 4-15 4. GUIs and Languages The.NET Languages Microsoft provides four languages for.NET: –C# –VB.NET friendlier syntax, case-insensitive, equivalent in power to C# –J#: Java 1.4 syntax, Java 1.2 class library +.NET class library –C++: 99% ANSI C++ generates managed (.NET) code or unmanaged (native x86) code It's about the concepts, not the language! –in.NET, the language really doesn't matter… –the standard languages have essentially the same.NET power –really just a matter of preference…

16 4-16 4. GUIs and Languages VB Let's rewrite the calculator in VB… Option Strict On Private Sub cmdAdd_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles cmdAdd.Click Dim i, j, k As Integer i = System.Convert.ToInt32(Me.txtNumber1.Text) j = System.Convert.ToInt32(Me.txtNumber2.Text) k = i + j Dim msg As String msg = "Sum = " & k System.Windows.Forms.MessageBox.Show(msg) 'A comment in VB End Sub Option Strict On Private Sub cmdAdd_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles cmdAdd.Click Dim i, j, k As Integer i = System.Convert.ToInt32(Me.txtNumber1.Text) j = System.Convert.ToInt32(Me.txtNumber2.Text) k = i + j Dim msg As String msg = "Sum = " & k System.Windows.Forms.MessageBox.Show(msg) 'A comment in VB End Sub

17 4-17 4. GUIs and Languages C++ And now let's rewrite in C++… –With C++, you have 3 type systems to worry about — C, C++, and.NET private: System::Void cmdAdd_Click(System::Object^ sender, System::EventArgs^ e) { int i, j, k; i = System::Convert::ToInt32( this->txtNumber1->Text ); j = System::Convert::ToInt32( this->txtNumber2->Text ); k = i + j; System::String^ msg; msg = "Sum = " + k; System::Windows::Forms::MessageBox::Show(msg); } private: System::Void cmdAdd_Click(System::Object^ sender, System::EventArgs^ e) { int i, j, k; i = System::Convert::ToInt32( this->txtNumber1->Text ); j = System::Convert::ToInt32( this->txtNumber2->Text ); k = i + j; System::String^ msg; msg = "Sum = " + k; System::Windows::Forms::MessageBox::Show(msg); }

18 4-18 4. GUIs and Languages J# And finally, in J#… –In J#, you can use Java's class library and.NET's class library private void cmdAdd_Click(Object sender, System.EventArgs e) { int i, j, k; i = System.Convert.ToInt32( this.txtNumber1.get_Text() ); j = System.Convert.ToInt32( this.txtNumber2.get_Text() ); k = i + j; String msg; msg = "Sum = " + k; System.Windows.Forms.MessageBox.Show(msg); } private void cmdAdd_Click(Object sender, System.EventArgs e) { int i, j, k; i = System.Convert.ToInt32( this.txtNumber1.get_Text() ); j = System.Convert.ToInt32( this.txtNumber2.get_Text() ); k = i + j; String msg; msg = "Sum = " + k; System.Windows.Forms.MessageBox.Show(msg); }

19 4-19 4. GUIs and Languages 4.3 The CTS The Common Type System

20 4-20 4. GUIs and Languages The Underlying.NET Type System CTS = Common Type System –there is ONE set of types shared by.NET languages –each type is represented by a.NET class –this set of classes underlie *all* the languages –language keywords map to these underlying classes… Dim s As String ' VB String s; // J# string s; // C# System::String s; // C++ Dim i As Integer ' VB int i; // J#,C#,C++ Dim s As String ' VB String s; // J# string s; // C# System::String s; // C++ Dim i As Integer ' VB int i; // J#,C#,C++ System.String System.Int32

21 4-21 4. GUIs and Languages CTS design CTS is based on a hierarchy of classes defined in FxCL –all types inherit from Object (all except interface types)

22 4-22 4. GUIs and Languages Value vs. Reference Types.NET (like Java) separates data types into two categories Value types: –variable represents a value ("bits") Reference types: –variable represents a reference to a heap-based object –actual data resides in the object int i; i = 10; int i; i = 10; 10 string s; s = "calico"; string s; s = "calico"; "calico"

23 4-23 4. GUIs and Languages Objects vs. Object References It's critical to understand the difference between objects, and references to objects — how many objects below? refs? Student s1; Student s2; Student s3; s1 = new Student("Jim", "Bag", …); s2 = new Student("Kim", "Lee", …); s3 = s2; registrar.CheckGradRequirements(s3); Student s1; Student s2; Student s3; s1 = new Student("Jim", "Bag", …); s2 = new Student("Kim", "Lee", …); s3 = s2; registrar.CheckGradRequirements(s3); public void CheckGradRequirements(Student s) {. s.CanGraduate = true; // all checks passed! } public void CheckGradRequirements(Student s) {. s.CanGraduate = true; // all checks passed! } how many objects? object references? refs to where?

24 4-24 4. GUIs and Languages 4.4 What's Next? Lab exercise #4…


Download ppt "Joe Hummel, PhD Dept of Mathematics and Computer Science Lake Forest College Lecture 4: Intro to GUIs and the."

Similar presentations


Ads by Google