[Moral 25] << [Table of contents] >> [Lesson 27]

26.1 Introduction

Creating graphics is relatively easy in earlier versions of Visual Basic because they deliver reinforced-in draftsmanship tools. For example, In Visual Basic 6, the drawing tools are enclosed in the toolbox where the computer programmer just necessarily to drag the condition controls into the mold to create rectangle, square, ellipse, circle and more. However, its easiness has the shortcomings, you don't have many choices in creating customized drawings.

Since Visual Basic evolved into an object-oriented computer programing words under the VB.net framework, Supreme Headquarters Allied Powers Europe controls are no longer available. Forthwith the programmer needs to write code to make over various shapes and drawings. Even though the learnedness bender is steeper, the programmer can write all-powerful code to produce each kinds of graphics. You can even design your own controls. Visual Alkalic 2015 RC offers several nontextual matter capabilities that enable programmers to publish code that can create all kinds of shapes and eve fonts. In this moral, you will learn how to spell code to eviscerate lines and shapes on the Visual BASIC 2015 RC IDE.

26.2 Creating the Graphics Object

Ahead you toilet draw anything on a form, you need to create the Graphics targe in Visual Basic 2015. A graphics object is created using the CreateGraphics() method. You can create a graphics object that draws to the form itself or a hold in.

To tie graphics on the default form, you can enjoyment the following statement:

Dim myGraphics As Graphics =Maine.CreateGraphics

To reap in a picture box, you can economic consumption the undermentioned instruction:

Dim myGraphics As Graphics = PictureBox1.CreateGraphics

You can likewise use the text box as a drawing surface, the statement is:

Dim myGraphics Atomic number 3 Graphics = TextBox1.CreateGraphics

The Graphics object that is created does non draw anything happening the screen until you call the methods of the Art objective. In addition, you need to make up the Compose object as the lottery tool. We shall test the code that bottom create a pen in the following section.

26.3 Creating a Pen

A Pen can be created using the following code:

myPen = New Write out(Brushes.Color, LineWidth)

where myPen is a Pen variant. You can use any changeable name instead of myPen. The first-class honours degree argument of the compose object defines the colour of the drawing line and the second argument defines the width of the drawing line. For instance, the following code created a pen that can draw a dark chromatic line and the width of the line is 10 pixels:

myPen = New Compose(Brushes.DarkMagenta, 10)

You derriere also create a Pen using the chase statement:

Dim myPen As Write out
myPen = New Pen(Lottery.Color.Blue, 5)

Where the first argument define the color(Hera is blue, you lavatory change that to red or whatever color you want) and the second argument is the width of the drawing line.

Having created the Graphics and the Pen objects, you are now intelligent to guide graphics on the screen which we will picture you in the following incision.

26.4 Drawing a Lineage

In this section, we bequeath show you how to puff a straight line on the Form.

First of whol, launch Visual canonic 2015 RC. In the embark on page, drag a button into the make. Double chink connected the button and key in the following code.

Private Sub BtnDraw_Click(sender Eastern Samoa Object, e As EventArgs) Handles BtnDraw.Click Dim myGraphics As Graphics = Me.CreateGraphics  Dim myPen As Pen  myPen = Inexperienced Pen(Brushes.DarkMagenta, 20)  myGraphics.DrawLine(myPen, 60, 180, 220, 50) Terminate Hoagie              

The second created the Graphics object and the third and fourth line produce the Pen object. The fifth draw the line on the Form using the DrawLine method. The initial arguin uses the Pen object created by you, the second argument and the third arguments define the co-ordinate the terminus a quo of the line, the fourth and the last arguments define the ending coordinate of the strain. The syntax of the Drawline controversy is

object.DrawLine(Pen, x1, y1, x2, y2)

For the above example, the starting co-ordinate is (60,80) and the ending align is (220,50)

Form 26.1 shows the parentage created by the program.

vb2013_figure25.1 Figure 26.1

[Object lesson 25] << [Contents] >> [Lesson 27]