Presentation is loading. Please wait.

Presentation is loading. Please wait.

Carnegie Mellon University MSCF1 C#/.NET Basics 2 Some code is from “C# in a Nutshell”

Similar presentations


Presentation on theme: "Carnegie Mellon University MSCF1 C#/.NET Basics 2 Some code is from “C# in a Nutshell”"— Presentation transcript:

1 Carnegie Mellon University MSCF1 C#/.NET Basics 2 Some code is from “C# in a Nutshell”

2 Carnegie Mellon University MSCF2 Outline Graphical User Interface Basics Visual Programming Demonstration using Visual Studio.NET Event Handling and C# Delegates

3 Carnegie Mellon University MSCF3 Graphical User Interface Concepts(1) Typical GUI Components Label, TextBox, Button, Scrollbar, … Components implement IComponent A Control is a Component with a graphical part and is visible Some Components, e.g., MessageQueues and Timers have no graphical representation A Form is a container for Components

4 Carnegie Mellon University MSCF4 Graphical User Interface Concepts(2) A Component may generate Events Event Handlers process or handle those events The basic window is of class Form and is fully qualified by System.Windows.Forms.Form A Button is qualified by System.Windows.Forms.Button

5 Carnegie Mellon University MSCF5 Graphical User Interface Concepts(3) The basic design process runs like this: (1) Create a Windows Form (2) Set its properties (3) Add controls (4) Set their properties (5) Implement event handlers

6 Carnegie Mellon University MSCF6 Visual Studio.NET Getting Started Steps 1.Start/Programs/MS Visual Studio.NET 2.New Project/Visual C# Projects 3.Enter directory name and project name 4.To run the default application use “Build Solution” and “Debug Start” or just click the arrow next to debug 5. Right click the mouse to view code

7 Carnegie Mellon University MSCF7 Demonstration Visual C#.Net GUI building

8 Carnegie Mellon University MSCF8 Event Handling Model Delegates listen for the events and call registered handlers Each component has a delegate for every event it can raise We register a method with the delegate and the delegate will call the method asynchronously

9 Carnegie Mellon University MSCF9 Delegates (1) A Button, for example, needs to notify some object when it is pushed We don’t want to hardwire (in the button) which object to call A delegate is a reference type used to encapsulate a method with particular parameter types

10 Carnegie Mellon University MSCF10 Delegate (2) using System; delegate String Foo(String x); // create a delegate class class Test { public static void Main() { Foo f = new Foo(ConvertToUpperCase); // create a delegate object String answer = f("abcd"); // call the method in the // object Console.WriteLine(answer); } public static String ConvertToUpperCase(String s) { return s.ToUpper(); }

11 Carnegie Mellon University MSCF11 Delegate (3) public class Form1 : System.Windows.Forms.Form { private System.Windows.Forms.Button multiplyButton; public void foo() { this.multiplyButton = new System.Windows.Forms.Button(); this.multiplyButton.Text = "Multiply"; this.multiplyButton.Click += new System.EventHandler(this.multiplyButton_Click); } private void multiplyButton_Click(object sender, System.EventArgs e) { textBox3.Clear(); string op1Str = op1.Text; string op2Str = op2.Text; : } Delegate reference Delegate Encapsulated method

12 Carnegie Mellon University MSCF12 Multicast Delegate using System; // From C# In A Nutshell delegate void MethodInvoker(); // define delegate class class Test { static void Main() { // create a Test object // and call its constructor new Test(); }

13 Carnegie Mellon University MSCF13 Test() { MethodInvoker m = null; m += new MethodInvoker(Foo); // overloaded += m += new MethodInvoker(Goo); // delegate holds m(); // pointers to two } // methods

14 Carnegie Mellon University MSCF14 void Foo() { Console.WriteLine("Foo"); } void Goo() { Console.WriteLine("Goo"); } Output: Foo Goo


Download ppt "Carnegie Mellon University MSCF1 C#/.NET Basics 2 Some code is from “C# in a Nutshell”"

Similar presentations


Ads by Google