1 / 40

B IL528 – Bilgisayar Programlama II

B IL528 – Bilgisayar Programlama II. Objects, Collections, and Events. Contents. Objects and Classes Properties Methods Events. Objects. Objects. C# is a true object-oriented programming language. Everything you use in Visual C# is an object

Download Presentation

B IL528 – Bilgisayar Programlama II

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. BIL528 – Bilgisayar Programlama II Objects, Collections, and Events

  2. Contents • Objects and Classes • Properties • Methods • Events

  3. Objects

  4. Objects • C# is a true object-oriented programming language. • Everything you use in Visual C# is an object • Forms and the controls you put on a form are all objects • All objects have attributes (called properties), most have methods, and many have events. • Whether creating simple applications or building large-scale enterprise solutions, you must understand what an object is and how it works to be successful.

  5. Objects (continued) • An object is a programming structure that encapsulates data and functionality as a single unit and for which the only public access is through the programming structure’s interfaces (properties, methods, and events). • Whether creating simple applications or building large-scale enterprise solutions, you must understand what an object is and how it works to be successful.

  6. “Controls” • We used a PictureBox and two Button objects in the last week’s class • Both PictureBox and Button objects are Controls • They are different objects but they share some properties • The shared properties come from the class Control.

  7. Properties • The attributes of an object which specify and return the state of the object are called properties. • You used some properties last week. • What you see in Properties window are all properties of the selected object on the form • Text of a Button, Width and Height of a Form, and Image of a PictureBox are all properties

  8. Changing Properties by Code • By using the Properties window, you can change the properties of an object in design time. • At runtime, you don’t have Properties window • When referencing properties in code, you specify the name of the object first, followed by a period (.), and then the property name as in the following syntax: • {ObjectName}.{Property}

  9. Changing Properties (continued) • If you had a button object named btnClickMe, for example, you would reference button’s Text property this way: • btnClickMe.Text • This line of code would return whatever value was contained in the Text property of the button object btnClickMe. • To set a property to some value, you use an equal sign (=). To change the Button object’s Left property, for example, you’d use a line of code such as the following: • btnClickMe.Left = 90;

  10. Storing a Property in a Temporary Variable • The following line of code places the value of the Text property of the button object called btnClickMe into a temporary variable. This statement retrieves the value of the Text property because the Text property is referenced on the right side of the equal sign. • strText = btnClickMe.Text;

  11. Read-Only Properties • Just as in real life, some properties can be read but not changed. • Those properties are called read-only properties. • One example of a read-only property is the Height property of the combo box control. • Although you can view the value of the Height property in the Properties window, you can’t change the value—no matter how hard you try. • If you attempt to use Visual C# code to change the Height property, Visual C# simply changes the value back to the default.

  12. Exercise • Last week, we changed the Size of the Form in the Properties window. • Today, we are going to change the Width and Height properties of the Form with Visual C# code. • We are going to add two buttons to the Picture Viewer project. One button enlarges the form when clicked, whereas the other shrinks the form.

  13. Add the Buttons

  14. Enlarging Code • Double-click the btnEnlarge button and write the following code: • this.Width = this.Width + 20; • this.Height = this.Height + 20;

  15. Shrinking Code • Double-click the btnShrink button and write the following code: • this.Width = this.Width - 20; • this.Height = this.Height - 20;

  16. this • this refers to the object to which the code belongs (in this case, the form). • this is a reserved word; it’s a word that you can’t use to name objects or variables because Visual C# has a specific meaning for it.

  17. Intellisense • When you type the period, or dot, as it’s called, a small drop-down list containing all properties and methods that you can use appears. • Visual Studio is smart enough to realize that this represents the current form (more on this in a moment), and to aid you in writing code for the object, it gives you a drop-down list containing all the properties and methods of the form. • This feature is called IntelliSense. • This prevents you from misspelling a member name, thereby reducing compile errors.

  18. Intellisense

  19. Methods

  20. Understanding Methods • In addition to properties, most objects have methods. • Methods are actions the object can perform, in contrast to attributes that describe the object. • You can call methods like properties by using dot: • {ObjectName}.{MethodName}({Parameters});

  21. Parentheses • Method calls in Visual C# must always have parentheses. • Sometimes they’ll be empty, but at other times they’ll contain data to pass to the method. • Example: • MessageBox.Show("Hello World!");

  22. Exercise • We are going to modify our PictureViewer project to include a button that, when clicked, draws a colored border around the picture.

  23. Creating the Interface • Add a new button to the form and set its properties and location (as in pp.69). • Write the following code into Click event: GraphicsobjGraphics = this.CreateGraphics(); objGraphics.Clear(SystemColors.Control); objGraphics.DrawRectangle(Pens.Blue, picShowPicture.Left - 1, picShowPicture.Top - 1, picShowPicture.Width + 1, picShowPicture.Height + 1); objGraphics.Dispose(); • Run your program.

  24. Graphics Object • A Graphics object can be used to draw some geometrical shapes or to write some texts. • CreateGraphics() methods of a form returns an object which represents the client area of the form. • Anything drawn onto the Graphics object of the form appears on the form. • Notice that the values returned by a property don’t have to be traditional values, such as numbers or texts; they could also be objects.

  25. The Methods Used in the Code • Clear() method clears the background of the form using the color specified. • DrawRectangle() method draws a rectangle by using a Pen object on the specified coordinates. • Dispose() method cleans up the resources used by the object (like free() in C).

  26. Running the Project

  27. Collections

  28. Understanding Collections • A collection is just what its name implies: a collection of objects. • Collections make it easy to work with large numbers of similar objects by enabling you to create code that performs iterative processing on items within the collection. • Iterative processing is an operation that uses a loop to perform actions on multiple objects, rather than writing the operative code for each object. • In addition to containing an indexed set of objects, collections also have properties and might have methods.

  29. Collections have their own properties and methods • All collections have a Count property. • All collections might have an Add() and a Delete() method.

  30. Exercise • Create a small form that tells the names of all control it contains: • Add some controls to the form and name them uniquely. • Write the code on the following slide to the Click method of the button which is labeled as Show Control Names.

  31. The Necessary Code for (int i = 0; i < this.Controls.Count; i++) { MessageBox.Show("Control #" + i.ToString() + " has the name " + this.Controls[i].Name); }

  32. Another Version int i = 1; foreach (Control cnt in this.Controls) { MessageBox.Show("Control #" + i.ToString() + " has the name " + cnt.Name); i++; }

  33. Events

  34. Understanding Events • In addition to designing an interface, you have to empower your program to perform actions in response to both how a user interacts with the program and how Windows interacts with the program. • You do this by using events.

  35. Event-Driven Programming • With traditional programming languages, the first line of code in the program executes, and the code continues to execute in a completely predetermined path. • Event-driven programs have logical sections of code placed within events. There’s no predetermined order in which events occur; the user often has complete control over what code is executed in an event-driven program by interactively triggering specific events, such as by clicking a button.

  36. Event • An event is a special kind of method used by an object to signal state changes that might be useful to clients (code using the object). • Events can be called in special ways: • Users can trigger the event (e.g. clicking a button) • Objects can trigger their own event (e.g. timers) • The operating system can trigger events • You can trigger an event just by calling its method

  37. Exercise: Display Mouse Coordinates • Open the PictureViewer project • Add two labels for X and Y coordinates • Select the picture box and create the new MouseMove event using the Properties window • Enter the following code: • lblX.Text = "X: " + e.X.ToString(); • lblY.Text = "Y: " + e.Y.ToString();

  38. Running the Project • When you move the mouse out of the picture box, the labels display the latest coordinates! • Solution: Handle the MouseLeave event! • lblX.Text = ""; • lblY.Text = "";

  39. Notes • The event methods has two parameters: Sender and EventArgs. • The mouse event methods have a parameter of type MouseEventArgs instead of EventArgs. • Other event methods may have different type of parameter, but still inherited from EventArgs class. • The EventArgs object gives useful information related to the event.

More Related