Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 9 Programming Based on Events

Similar presentations


Presentation on theme: "Chapter 9 Programming Based on Events"— Presentation transcript:

1 Chapter 9 Programming Based on Events
Austin Joseph

2 Topics in Chapter 9 Delegates Event Handling List Box Control
Combo Box Control Menu Strip Control CheckBox Control RadioButton Control We will review the bolded topics in one session

3 Delegates What is a Delegate?
Delegates are pretty much pointer functions. They are references toward methods.

4 Example: Delegate delgate string ReturnSimpleString(); { public static string Aheading(); return “Your age will be”; } public static string StringMethod(); return “in 10 Years” The string method with the delegate both return strings, both have zero parameters. The delegate just simply specifies what the signature should be

5 Making Delegate Instances
Associate the delegate with the method. A delegate instance is defined using the method name as the argument inside the parenthesis. After the instantiation, the delegate identifier saying3 references the EndStatement( ) method. Saying3( ) calls the EndStatement( ) method. ReturnSimpleString saying3 = new ReturnSimpleString(stringMethod);

6 Your age will be 28 in 10 Years
delegate string ReturnSimpleString(); class delegateExample { static void Main( int age = 18; ReturnSimpleString saying1 = new ReturnSimpleString(AHeading); ReturnSimpleString saying2 = new ReturnSimpleString((age + 10).ToString); ReturnSimpleString saying3 = new ReturnSimpleString(StringMethod); MessageBox.Show(saying1() + saying2() + saying3()); } static string AHeading() (Output of the program) return “Your age will be”; } static string stringMethod() { return “in 10 years”; Your age will be 28 in 10 Years OK

7 Event Handling You might be surprised, but you have actually dealt with Event Handling before in one of the Chapter 8 Programs. Remember Program 3? It asked to create event-handler methods that retrieve the values, perform the calculations, and display the result of the calculations on a Label object. That's part of Event Handling.

8 Event Handling Example
Event Wiring - While creating a forms design, you've probably added a button on the design. Two thing happened when you double clicked the button. The button click event was registered and an event handler method was created. Example this.button1.Click += new System.EventHandler(this.button1_Click); (You'll find this code above in the Form Designer) And the code above is generated when you double click the button in Form Designer. This is a delegate Type. Button1_click method is associated with that delegate private void button1_Click(object sender, System.EventArgs e) { }

9 So how are delegates and Events related?
Assumingly you understand what a delegate is by now. When you double click the button in the forms and it shows the event handler method (Above) but the code above the event handler is also registered in the Form Designer. The System.EventHandler is the delegate which associates the method Button One Click. This same idea will continue on with, List Box Control, Combo Box Control and Menu Strip Control. But with different naming standards

10 ListBox Control Regular List How to make a list Go to properties
1.) Make sure the List is shown 2.) Scroll down and find Items - After finding items, enter in the items which you will want

11 Code for ListBox The Code found In Form Designer
this.listBox1.SelectedIndexChanged += new System.EventHandler(this.listBox1_SelectedIndexChanged); Code Found in Forms private void listBox1_SelectedIndexChanged(object sender, EventArgs e) { } this.txtBoxResult.Text = this.listBox1.SelectedItem.ToString();

12 Making a list box, but with multiple items being selected
Go back to properties, and change Selection Mode into MultiExtended. This allow users to make selections using the Ctrl Key and clicking the mouse Once editing the properties is done, the event handler has to be edited. private void listBox1_SelectedIndexChanged(object sender, EventArgs e) { string result = ""; foreach (string activity in listBox1.SelectedItems) result += activity + " "; } this.textBox1.Text = result;

13 Adding Items to a list Add( ) method (More of these types can be used, example Sorted Property) } private void button1_Click(object sender, EventArgs e) { listBox1.Items.Add(textBox2.Text);

14 Combo Box Similar to the List Box. Only difference, Combo Box has a dropdown style Properties - Different types of Drop Downs DropDown DropDownList (Disables new text entering in ComboBox)

15 Combo Box Regular ComboBox Example
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) { this.textBox1.Text = this.comboBox1.SelectedItem.ToString(); }

16 Adding on Items to a ComboBox
Register a KeyPress Event KeyPress() Very Simple Concept (The Bottom Code will automatically register in Form Designer) this.flowerBox.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.comboBox1_KeyPress);

17 foreach (int item in listBox1.SelectedIndices)
private void comboBox1_SelectedIndexChanged(object sender, System.EventArgs e) { this.textBox1.Text = ""; foreach (int item in listBox1.SelectedIndices) this.textBox1.Text += this.listBox1.Items[item] + ""; } this.textBox1.Text = flowerBox.Text; private void comboBox1_KeyPress(object sender, KeyPressEventArgs e) private void ADDbutton_Click(object sender, EventArgs e) flowerBox.Items.Add(textBox1.Text);

18 Menu Strips Left Side of screen, can be found in ToolBox
Can be found on Page 505

19 Assignments (Refer back to Page 575 in book if needed)
Program 1 (5 Pointer) – Make a program in which the user enters its name, address, and phone number. Use a menu strip to display a show box with the three statements, a clear entire strip and an exit strip Program (10 Pointer) (Not in book) – Tax Calculator User enters salary 3.07% state tax 3.05% city tax 15 % federal income tax Find out total contribution MenuStrip - Exit Option and clear textbox


Download ppt "Chapter 9 Programming Based on Events"

Similar presentations


Ads by Google