Using Classes - PowerPoint PPT Presentation

1 / 18
About This Presentation
Title:

Using Classes

Description:

An object from one class may be use in an other class. Process is called ... sender As System.Object, ByVal e As _ System.EventArgs) Handles Button1.Click ... – PowerPoint PPT presentation

Number of Views:18
Avg rating:3.0/5.0
Slides: 19
Provided by: Trin181
Category:
Tags: button1 | classes | using

less

Transcript and Presenter's Notes

Title: Using Classes


1
Using Classes
  • VB.NET is object oriented
  • Each form is designed to be a class
  • An object from one class may be use in an other
    class
  • Process is called instantiation
  • Creating an instance of an object
  • Java instantiates a class from a class
  • Creates physical space in memory for the class
  • Keyword NEW ..uses a constructor

2
Creating a Class
  • Class has a name
  • Attributes
  • Defined as variables
  • Methods
  • Defined as subroutines and functions

3
Example Entity ClassStudent
  • Public Class Student
  • Private SName as String
  • Private SGradeLevel as integer
  • Sub New()
  • SName default name
  • SGradeLevel 0
  • End Sub
  • Sub New(byval aSName as string, AGradeLevel as
    integer)
  • SName aSName
  • SGradeLevel AGradeLevel
  • End Sub
  • Methods
  • Public sub DisplayGrade ()
  • messagebox.show(SName is at grade level
    SGradeLevel)
  • End sub

4
Instantiating a Class
  • In VB.Net, a class is instantiated
  • Dim myname as New classname
  • Once a class has been instantiated, you may refer
    to its attributes (if public, friend or shared)
    or to its methods (if public, friend or shared)
  • Myname.methodfromclassname

5
Opening Forms
Private Sub Button1_Click(ByVal sender As
System.Object, ByVal e As _ System.EventArgs)
Handles Button1.Click Me.Hide() hide the
form but keep its variables /methods available
Dim myform2 As New Form2
myform2.ShowDialog() Me.Close() 'remember
the close - otherwise this form will be active
'but since you hid it in Me.Hide, you won't
be able to see it. End Sub
Private Sub Button1_Click(ByVal sender As
System.Object, ByVal e As _ System.EventArgs)
Handles Button1.Click Me.Hide() hide the
form but keep its variables /methods available
Dim myform2 As New Form2
myform2.ShowDialog() Me.show() this
redisplays form1 after form2 is closed End Sub
6
Be Careful with the Form Class
  • One form will be the initiator for calling the
    other forms
  • You must have a way to get back to this class it
    remains open until it is explicitly closed
  • Dont hide the main form unless you are sure that
    you will be returning to it
  • AND WILL BE CLOSING IT
  • You will have to reboot to close the windows
    application that is running in the background
  • You cant see it because you hid its form

7
Not Closing a Form
Private Sub Button1_Click(ByVal sender As
System.Object, ByVal e As _ System.EventArgs)
Handles Button1.Click Me.Hide() hide the
form but keep its variables /methods available
Dim myform2 As New Form2
myform2.ShowDialog() End Sub THIS WILL KEEP THE
APPLICATION RUNNING EVEN THOUGH YOU CANNOT SEE IT
This one is correct it closes the application
Private Sub Button1_Click(ByVal sender As
System.Object, ByVal e As _ System.EventArgs)
Handles Button1.Click Me.Hide() hide the
form but keep its variables /methods available
Dim myform2 As New Form2
myform2.ShowDialog() Me.Close() 'remember
the close - otherwise this form will be active
'but since you hid it in Me.Hide, you won't
be able to see it. End Sub
8
Using Built-in Classes
  • VB.NET has a collection of classes included with
    the .NET Framework
  • Examples
  • Math class
  • Random class
  • CreateGraphics class
  • We will experiment with using this class

9
Using a Supplied Class Graphics
  • VB.NET comes with a collection of classes related
    to drawing graphics
  • The namespace for this class is
  • System.Drawing
  • Has rectangle, point, brushes, colors
  • Rectangles, points etc are classes in the name
    space
  • To use them you instantiate them
  • Dim myrectangle as new rectangle(x,y,width,height0

10
Form Drawing Basics
  • Windows form is a series of points
  • x,y coordinate system
  • 0,0 is the top left corner
  • Bottom corner is identified in the form size
    property
  • For a form of height 400 and width 500
  • Coordinates would be
  • Upper left 0,0
  • Bottom right 400,500

11
VB.NET Drawing A Line
  • To produce a graphic
  • Use CreateGraphics class
  • For example to create a diagonal line across a
    form
  • Find the bottom coordinate from the size property
    of the form
  • For example 400 wide by 500 high
  • Use the drawline method which asks for pen color,
    x1, y1, x2,y2
  • Creategraphics.drawline(pens.black, 0,0,400,500)

12
VB.NET Drawing
  • creategraphics.DrawRectangle(pens.color, x1, y1,
    x2,y2)
  • creategraphics.DrawEllipse(pens.color, x1, y1,
    x2,y2)
  • Fill an object
  • CreateGraphics.FillRectangle(Brushes.Beige,
    myrectangle)
  • Create a tool
  • Dim mybrush as new solidBrush(Color.white)
  • Dim mypen as new pen(Color.blue,2)

13
VB.NET Drawing - Polygon
  • CreateGraphics.DrawPolygon(Pens.Black,points)
  • Point is a class with x,y coordinates
  • Dim onepoint As New Point(0, 0)
  • Dim twopoint As New Point(30, 30)
  • Dim threepoint As New Point(100, 100)
  • Dim mypoints(3) As System.Drawing.Point
  • mypoints(0) onepoint
  • mypoints(1) twopoint
  • mypoints(2) threepoint
  • CreateGraphics.DrawPolygon(Pens.Black,
    mypoints)

14
Drawing an Image
  • Can Use the Picture Box
  • Or
  • creategraphics.DrawImage(image, x1,y1, x2,y2)
  • Image is a system.drawing.image
  • Copy an image into the bin folder (of the debug
    folder of the project your are working with)
  • This makes it a resource
  • Then declare it
  • Dim myimage As System.Drawing.Image
  • myimage Global.graphics1.My.Resources.Resources.
    Waterlilies

15
Animating an Image
  • Animation is about moving an image across the
    screen
  • For true animation, really need to make a slight
    modification to an image
  • Really have many images that you use
  • Picture Box Animation
  • Start with it visible
  • Make it invisible
  • Change the top (left) x,y coordinates

16
Picture Box Animation Code
  • Create the form
  • Drag a picture box onto the form
  • Set the image property
  • Set the top and left coordinates
  • Create an event (button)
  • On the button makes it move randomly
  • picturebox.visible False
  • picturebox.top int(200rnd())
  • Picturebox.left int(200rnd))
  • Picturebox.visible true

17
Animation Draw Image
  • Dim myimage As System.Drawing.Image
  • myimage Global.graphics1.My.Resources.Resources.
    Waterlilies
  • creategraphics.DrawImage(myimage, 0,0, 50,50)
  • On the button
  • Clear the form
  • Draw a filled triangle the size of the form
  • Use a background color of the form
  • DrawImage moving the coordinates

18
Move Image Code
  • Private Sub Button2_Click(ByVal sender As
    System.Object, ByVal e _ As System.EventArgs)
    Handles Button2.Click
  • Dim x1 As Integer 0
  • Dim y1 As Integer 0
  • Dim width As Integer 500
  • Dim height As Integer 500
  • CreateGraphics.DrawRectangle(Pens.Gray,
    0, 0, 500, 500)
  • CreateGraphics.FillRectangle(Brushes.Gray,
    0, 0, 500, 500)
  • x1 Int(200 Rnd())
  • y1 Int(200 Rnd())
  • width Int(200 Rnd())
  • height Int(200 Rnd())
  • CreateGraphics.DrawImage(myimage, x1, y1,
    width, height)
  • End Sub
Write a Comment
User Comments (0)
About PowerShow.com