Presentation is loading. Please wait.

Presentation is loading. Please wait.

CIS 338: Events Dr. Ralph D. Westfall April, 2011.

Similar presentations


Presentation on theme: "CIS 338: Events Dr. Ralph D. Westfall April, 2011."— Presentation transcript:

1 CIS 338: Events Dr. Ralph D. Westfall April, 2011

2 What Is an Event? an event is when something happens common user events start the program press a key click the mouse move the mouse close/end the program

3 Other Events events don't have to be from local user time (update virus checker every Wednesday at 10 AM) e-mail message arrives E:\ drive not ready  event happens when program tries to read or write the missing CD

4 Event Driven Programming interactive programs respond to events generated by users, by the operating system, possibly by sensors, or by the application itself object oriented languages are well suited for writing interactive programs event "listeners" can be added to Java objects, are built into many VB.NET ones

5 Common Controls Events Click – with left mouse button MouseDown – click with either button MouseMove – cursor moved over object Enter – object becomes "active" user tabs to it, or clicks on it (GotFocus*) Leave – loses active status (LostFocus*) KeyDown – key pressed when object has focus (instruction: "press any key") * VB6 (still works, but don't use!)don't use!

6 Form Events Resize – first thing that happens when a form is loaded also happens when user or code changes its size Move – next thing that happens also happens when user drags the form Load – goes into memory not visible yet (run MsgBox out of Load) Activated – when form is shown or gets focus if already visible useful when have multiple forms

7 Form Events - 2 Deactivate – when hidden or loses focus Closing – when being removed from memory Closed – when destroyed completely by code, or when a Visual Basic.NET application ends

8 VB.NET Event Procedures Windows (operating system) detects an event and sends a message to VB.NET VB.NET looks for code to handle it Private Sub btnExit_Click(etc.) Handles btnExit.Click 'code runs on a click event End End Sub btnExit = current button name.Click = Event

9 Handling Multiple Controls a procedure can handle multiple events Private Sub ProcessEnter(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtName.Enter, txtAddress.Enter [can test Name of control or Tag property defined by programmer to handle some aspects differently] End Sub 'more informationmore information

10 Creating an Event Procedure start VB and draw a control(s) on form open Code window View>Code, click Solution Explorer's View Code Icon, or [right click form]>View Code select control in left ComboBox select event in right ComboBox write in code to handle this event in the Sub that VB.NET creates for you

11 Event Procedure Example start a new Windows Application in code window, select (frmName Events) and MouseMove event in top ComboBoxes add following code to the Sub, and then move mouse over Form when running Me.Text = "X=" & e.X & " Y=" & e.Y 'Me means this formNotes

12 Code to Cause Event to Happen create an event procedure (previous slides) code a subroutine, or use an existing one put in a line of code to generate the event Private Sub RespondSomeEvent( [etc. ]) Call Button1_Click(sender, e) 'args required End Sub 'Call is optional useful when something needs to happen just after something else happens in above code, program clicks a button when some event specified by user happens 'previous page notes

13 Exercise add a ListBox to a form add some Items in the Properties double click inside the ListBox to create a Sub to handle the event when a different item is selected add a MsgBox to the Sub to show the new item selection (see code below if can't find necessary code through Intellisense)


Download ppt "CIS 338: Events Dr. Ralph D. Westfall April, 2011."

Similar presentations


Ads by Google