1 / 13

CS0004: Introduction to Programming

CS0004: Introduction to Programming. Events. 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

lael
Download Presentation

CS0004: Introduction to Programming

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. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

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: • Your program monitors the controls in the window to detect any event that a control can recognize (mouse movements, clicks, keystrokes, etc.) • When your program detects an event, it examines the code to see if you’ve written an even procedure for it. • If you have written an event procedure, the instructions in the procedure are executed and it goes back to step 1. • 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.

  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: • Double click on the control (creates the default event procedure for the control) • 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: • btnButton_Click is the name of the event procedure • 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:

  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: • Click on “Project/Solution” from the “File” menu under “Open”. • Navigate to where you saved your project. • 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) • 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: • Double Click on the folder in the same folder as your “.sln” file. • Double Click on the “bin” folder. • Double Click on the “Debug” folder. • It is the “.exe” file in the “Debug” folder. • An executable file is a file for the specific purpose of running your program.

More Related