Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS0004: Introduction to Programming Events. Review  Event Procedure  A set of instructions to be executed when a certain event happens.  Many event-driven.

Similar presentations


Presentation on theme: "CS0004: Introduction to Programming Events. Review  Event Procedure  A set of instructions to be executed when a certain event happens.  Many event-driven."— Presentation transcript:

1 CS0004: Introduction to Programming Events

2 Review  Event Procedure  A set of instructions to be executed when a certain event happens.  Many event-driven programs can be divided into three important sections or layers:  Presentation Layer  Anything the user sees and how the user interacts with a program.  Logic Layer  How the program acts when it receives events.  Data Access Layer  Allows simple access to persistent storage (like a database). We probably won’t be doing this in this course.

3 Review  When you run your Visual Basic program the following things happen: 1. Your program monitors the controls in the window to detect any event that a control can recognize (mouse movements, clicks, keystrokes, etc.) 2. When your program detects an event, it examines the code to see if you’ve written an even procedure for it. 3. If you have written an event procedure, the instructions in the procedure are executed and it goes back to step 1. 4. If you have not written an event procedure, it ignores the event and goes back to step 1.

4 Review  The naming (value of the name property) of controls should indicate what it is and what it does. ObjectPrefixExample FormfrmfrmPayroll ButtonbtnbtnComputeTotal LabellbllblAddress Text BoxtxttxtCity List BoxlstlstOutput

5 Events  Now that we have an interface, we can have our controls act on events.  For now, we will just change the properties of controls with our events  The basic form of setting a property of a control: controlName.property = setting  controlName – the name of the control whose property you want to change (ex. txtName)  property – the property you want to change (ex. ForeColor)  setting – value for which you want to set the property

6 Events  Examples: txtBox.Text = “Hello”  Displays “Hello” in the text box btnButton.Visible = True  Makes the button visible txtBox.ForeColor = Color.Red  Makes the text color in the text box red frmMain.Text = “My Form”  Will NOT work in the code for frmMain. You must use the word Me (because the form is referring to itself) Me.Text = “My Form”  These lines of code (statements) are called Assignment Statements.

7 Event Procedures  To give the controls function when events occur, we need to write our code in event procedures.  Note: I will often call event procedures, “event handlers”.  To view the code right click on the form and select “View Code”  To make an event procedure you can: 1. Double click on the control (creates the default event procedure for the control) 2. Click on the control then click the “Events” button at the top of the Properties Window  This will create the code for the event procedure.

8 Event Procedures  An event procedure looks something like this: Private Sub btnButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnButton.Click ‘Code goes here End Sub  Don’t worry about all the details of this code!  There’s only two parts you need to worry about: 1. btnButton_Click is the name of the event procedure 2. btnButton.Click tells the procedure to listen for the click event on the button btnButton.  The most of the rest are keywords used to describe the procedure  Keywords are words that have a particular meaning in VB  These are reserved, meaning they can’t be used for other purposes (this will be clearer when we talk about variables)

9 Event Procedures  A single event handler can listen for multiple different events. Private Sub btnButton_Click(…) Handles btnButton1.Click, btnButton2.Click ‘Code goes here End Sub  So the code inside of the procedure will be executed if either btnButton1 or btnButton2 is clicked.

10 IntelliSense  When you start typing code in Visual Studio, a system called IntelliSense will attempt to assist you by giving you options to complete your code.  This limits the amount of code you have to memorize.  When an item is highlighted in the drop-down supplied by IntelliSense, a tool tip is supplied next to it.  You can select an item in the menu by clicking on it, pushing enter, or tab.  If the IntelliSense drop-down gets in the way, you can hold down the control key to make it transparent.

11 Default Events  When double clicking on a control, you will get the event procedure for the control’s default event.  The some default events are as follows: ControlDefault Event formLoad buttonClick labelClick list boxSelectedIndexChanged text boxTextChanged

12 Some Events  Click – Useful for buttons.  DoubleClick – Not available for buttons, but can be used for labels, text boxes, etc.  Focus  Enter – Event raised when control first receives focus  Leave – Event raised when control first loses focus  Keys – Useful for text boxes.  KeyDown – Event raised when a key is first pressed  KeyUp – Event raised when a key is first released  Key Pressed – Event raised when the control has focus and a key is pressed and released.  Mouse  MouseDown – Event raised when the a mouse button is first pressed while over a control  MouseUp - Event raised when the a mouse button is first released while over a control  MouseEnter – Event raised when the mouse enters a visible part of the control  MouseLeave – Event raised when the mouse leaves a visible part of the control  MouseHover – Event raised when a mouse remains stationary over a control  Property Changed – When the programmer or the user changes a property of a control an event is raised  TextChanged – Event raised when the value of the “Text” property is changed

13 Opening a Project  To open a saved VB project: 1. Click on “Project/Solution” from the “File” menu under “Open”. 2. Navigate to where you saved your project. 3. Double Click on the folder that was created by Visual Studio when you saved (it should have the same name that you named you project) 4. Double click on the “.sln” file (it should have the same name that you named you project).  Also, if you successfully debug you program, it will create an executable file inside of your Debug directory. To get to it: 1. Double Click on the folder in the same folder as your “.sln” file. 2. Double Click on the “bin” folder. 3. Double Click on the “Debug” folder. 4. It is the “.exe” file in the “Debug” folder.  An executable file is a file for the specific purpose of running your program.


Download ppt "CS0004: Introduction to Programming Events. Review  Event Procedure  A set of instructions to be executed when a certain event happens.  Many event-driven."

Similar presentations


Ads by Google