{"id":1110,"date":"2012-04-06T16:45:15","date_gmt":"2012-04-06T08:45:15","guid":{"rendered":"http:\/\/www.vbtutor.net\/index.php\/"},"modified":"2018-06-24T22:49:27","modified_gmt":"2018-06-24T14:49:27","slug":"visual-basic-2010-lesson-21","status":"publish","type":"page","link":"https:\/\/www.vbtutor.net\/index.php\/visual-basic-2010-lesson-21\/","title":{"rendered":"Visual Basic 2010 Lesson 21- Managing Graphics"},"content":{"rendered":"<h4 align=\"center\"><strong><a href=\"http:\/\/www.vbtutor.net\/index.php\/visual-basic-2010-lesson-20\/\">[Lesson 20]<\/a> <\/strong>&lt;&lt;\u00a0<strong><a href=\"http:\/\/www.vbtutor.net\/index.php\/visual-basic-2010-tutorial\/\">[CONTENTS]<\/a> &gt;&gt;<a href=\"http:\/\/www.vbtutor.net\/index.php\/visual-basic-2010-lesson-22\/\"> [Lesson 22]<\/a><\/strong><\/h4>\n<h3>21.1 Introduction to Graphics<\/h3>\n<p>Though Managing graphics in earlier versions of Visual Basic seem easier as they have built-in drawing tools, Visual Basic 2010 is much more versatile in handling graphics. For example, In Visual Basic 6, the drawing tools are included in the toolbox where the programmer just need to drag the shape controls into the form to create rectangle, square, ellipse, circle and more. However, its simplicity has the shortcomings, you don&#8217;t have many choices in creating customized drawings.<\/p>\n<p>Since Visual Basic evolved into a fully OOP language under the VB.net framework, shape controls are no longer available. Now the programmer needs to write code to create various shapes and drawings. Even though the learning curve is steeper, the programmer can write powerful code to create all kinds of graphics. You can even design your own controlsVisual Basic 2010 offers various graphics capabilities that enable programmers to write code that can draw all kinds of shapes and even fonts. In this lesson, you will learn how to write code to draw lines and shapes on the VB interface.<\/p>\n<p><script async src=\"\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js\"><\/script><br \/>\n<ins class=\"adsbygoogle\"\n     style=\"display:block; text-align:center;\"\n     data-ad-layout=\"in-article\"\n     data-ad-format=\"fluid\"\n     data-ad-client=\"ca-pub-3033628290023372\"\n     data-ad-slot=\"1723562988\"><\/ins><br \/>\n<script>\n     (adsbygoogle = window.adsbygoogle || []).push({});\n<\/script><\/p>\n<h3>21.1 Creating the Graphics Object<\/h3>\n<p>Before you can draw any graphic on a form, you need to create the Graphics object in vb2010. A graphics object is created using a CreateGraphics() method. You can create a graphics object that draws to the form itself or a control. For example, if you wish to draw to the form, you can use the following statement:<\/p>\n<pre style=\"font-size: 110%; width: 80%;\">Dim myGraphics As Graphics =me.CreateGraphics\r\n<\/pre>\n<p>*Always use Dim to define the object. Using me instead of Form1 because it is not allowed in Visual Basic 2010.<\/p>\n<p>Or if you want the graphics object to draw to a picturebox, you can write the following statement:<\/p>\n<pre style=\"font-size: 110%; width: 80%;\">Dim myGraphics As Graphics = PictureBox1.CreateGraphics\r\n<\/pre>\n<p>You can also use the text box as a drawing surface, the statement is:<\/p>\n<pre style=\"font-size: 110%; width: 80%;\">Dim myGraphics As Graphics = TextBox1.CreateGraphics\r\n<\/pre>\n<p>The Graphics object that is created does not draw anything on the screen until you call the methods of the Graphics object. In addition, you need to create the Pen object as the drawing tool. We will examine the code that can create a pen in the following section.<\/p>\n<h3>21.2 Creating a Pen<\/h3>\n<p>A Pen can be created using the following code:<\/p>\n<pre style=\"font-size: 110%; width: 80%;\">myPen = New Pen(Brushes.DarkMagenta, 10)\r\n<\/pre>\n<p>where myPen is a Pen variable. You can use any variable name instead of myPen. The first argument of the pen object define the color of the drawing line and the second argument defines the width of the drawing line.<\/p>\n<p>You can also create a Pen using the following statement:<\/p>\n<pre style=\"font-size: 110%; width: 80%;\">Dim myPen As Pen\r\nmyPen = New Pen(Drawing.Color.Blue, 5)\r\n<\/pre>\n<p>Where the first argument define the color(here is blue, you can change that to red or whatever color you want) and the second argument is the width of the drawing line.<\/p>\n<p>Having created the Graphics and the Pen objects, you are now ready to draw graphics on the screen which we will show you in the following section.s<br \/>\n<script async src=\"\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js\"><\/script><br \/>\n<ins class=\"adsbygoogle\"\n     style=\"display:block; text-align:center;\"\n     data-ad-layout=\"in-article\"\n     data-ad-format=\"fluid\"\n     data-ad-client=\"ca-pub-3033628290023372\"\n     data-ad-slot=\"1723562988\"><\/ins><br \/>\n<script>\n     (adsbygoogle = window.adsbygoogle || []).push({});\n<\/script><br \/>\nIn this section, we will show you how to draw a straight line on the Form.<\/p>\n<p>First of all, launch Visual basic 2010 Express. In the startup page, drag a button into the form. Double click on the button and key in the following code.<\/p>\n<pre style=\"font-size: 110%;\">Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click\r\nDim myGraphics As Graphics = me.CreateGraphics\r\nDim myPen As Pen\r\n myPen = New Pen(Brushes.DarkMagenta, 10)\r\n myGraphics.DrawLine(myPen, 10, 10, 100, 10)\r\nEnd Sub\r\n<\/pre>\n<p>The second created the Graphics object and the third and fourth line create the Pen object. The fifth draw a line on the Form using the DrawLine method. The first argument use the Pen object created by you, the second argument and the third arguments define the coordinate the starting point of the line, the fourth and the last arguments define the ending coordinate of the line. The general syntax of the Drawline argument is<\/p>\n<pre style=\"font-size: 110%; width: 80%;\">object.DrawLine(Pen, x1, y1, x2, y2)\r\n<\/pre>\n<p>The output of the program is shown below:<br \/>\n<img loading=\"lazy\" decoding=\"async\" class=\"aligncenter\" src=\"https:\/\/www.vbtutor.net\/vb2010\/vb2010_Image\/lesson21_1.jpg\" alt=\"Visual Basic 2010\" width=\"300\" height=\"300\" \/><\/p>\n<p><script async src=\"\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js\"><\/script><br \/>\n<ins class=\"adsbygoogle\" style=\"display: block;\" data-ad-format=\"autorelaxed\" data-ad-client=\"ca-pub-3033628290023372\" data-ad-slot=\"8075128701\"><\/ins><br \/>\n<script>\n     (adsbygoogle = window.adsbygoogle || []).push({});\n<\/script><\/p>\n<h4 align=\"center\"><strong><strong><a href=\"http:\/\/www.vbtutor.net\/index.php\/visual-basic-2010-lesson-20\/\">[Lesson 20]<\/a> <\/strong>&lt;&lt;\u00a0<strong><a href=\"http:\/\/www.vbtutor.net\/index.php\/visual-basic-2010-tutorial\/\">[CONTENTS]<\/a> &gt;&gt;<a href=\"http:\/\/www.vbtutor.net\/index.php\/visual-basic-2010-lesson-22\/\"> [Lesson 22]<\/a><\/strong><\/strong><\/h4>\n","protected":false},"excerpt":{"rendered":"<p>[Lesson 20] &lt;&lt;\u00a0[CONTENTS] &gt;&gt; [Lesson 22] 21.1 Introduction to Graphics Though Managing graphics in earlier versions of Visual Basic seem easier as they have built-in drawing tools, Visual Basic 2010 is much more versatile in handling graphics. For example, In Visual Basic 6, the drawing tools are included in the toolbox where the programmer just &hellip; <a href=\"https:\/\/www.vbtutor.net\/index.php\/visual-basic-2010-lesson-21\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Visual Basic 2010 Lesson 21- Managing Graphics<\/span><\/a><\/p>\n","protected":false},"author":23013,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-1110","page","type-page","status-publish","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.0 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Visual Basic 2010 Lesson 21- Managing Graphics - Learn Visual Basic Programming \u2013 VB.NET, VBA &amp; Classic VB<\/title>\n<meta name=\"description\" content=\"This Visual Basic 2010 lesson shows you how to manage graphics in Visual Basic 2010\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"http:\/\/www.vbtutor.net\/vb2010\/vb2010_lesson21.html\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Visual Basic 2010 Lesson 21- Managing Graphics - Learn Visual Basic Programming \u2013 VB.NET, VBA &amp; Classic VB\" \/>\n<meta property=\"og:description\" content=\"This Visual Basic 2010 lesson shows you how to manage graphics in Visual Basic 2010\" \/>\n<meta property=\"og:url\" content=\"http:\/\/www.vbtutor.net\/vb2010\/vb2010_lesson21.html\" \/>\n<meta property=\"og:site_name\" content=\"Learn Visual Basic Programming \u2013 VB.NET, VBA &amp; Classic VB\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/Vbtutor\" \/>\n<meta property=\"article:modified_time\" content=\"2018-06-24T14:49:27+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.vbtutor.net\/vb2010\/vb2010_Image\/lesson21_1.jpg\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:site\" content=\"@liewvk\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.vbtutor.net\/index.php\/visual-basic-2010-lesson-21\/\",\"url\":\"http:\/\/www.vbtutor.net\/vb2010\/vb2010_lesson21.html\",\"name\":\"Visual Basic 2010 Lesson 21- Managing Graphics - Learn Visual Basic Programming \u2013 VB.NET, VBA &amp; Classic VB\",\"isPartOf\":{\"@id\":\"https:\/\/www.vbtutor.net\/#website\"},\"primaryImageOfPage\":{\"@id\":\"http:\/\/www.vbtutor.net\/vb2010\/vb2010_lesson21.html#primaryimage\"},\"image\":{\"@id\":\"http:\/\/www.vbtutor.net\/vb2010\/vb2010_lesson21.html#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.vbtutor.net\/vb2010\/vb2010_Image\/lesson21_1.jpg\",\"datePublished\":\"2012-04-06T08:45:15+00:00\",\"dateModified\":\"2018-06-24T14:49:27+00:00\",\"description\":\"This Visual Basic 2010 lesson shows you how to manage graphics in Visual Basic 2010\",\"breadcrumb\":{\"@id\":\"http:\/\/www.vbtutor.net\/vb2010\/vb2010_lesson21.html#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"http:\/\/www.vbtutor.net\/vb2010\/vb2010_lesson21.html\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"http:\/\/www.vbtutor.net\/vb2010\/vb2010_lesson21.html#primaryimage\",\"url\":\"https:\/\/www.vbtutor.net\/vb2010\/vb2010_Image\/lesson21_1.jpg\",\"contentUrl\":\"https:\/\/www.vbtutor.net\/vb2010\/vb2010_Image\/lesson21_1.jpg\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"http:\/\/www.vbtutor.net\/vb2010\/vb2010_lesson21.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.vbtutor.net\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Visual Basic 2010 Lesson 21- Managing Graphics\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.vbtutor.net\/#website\",\"url\":\"https:\/\/www.vbtutor.net\/\",\"name\":\"Learn Visual Basic Programming \u2013 VB.NET, VBA &amp; Classic VB\",\"description\":\"Start learning Visual Basic from beginner to advanced. Includes VB.NET, VBA, and classic VB tutorials for students and professionals.\",\"inLanguage\":\"en-US\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Visual Basic 2010 Lesson 21- Managing Graphics - Learn Visual Basic Programming \u2013 VB.NET, VBA &amp; Classic VB","description":"This Visual Basic 2010 lesson shows you how to manage graphics in Visual Basic 2010","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"http:\/\/www.vbtutor.net\/vb2010\/vb2010_lesson21.html","og_locale":"en_US","og_type":"article","og_title":"Visual Basic 2010 Lesson 21- Managing Graphics - Learn Visual Basic Programming \u2013 VB.NET, VBA &amp; Classic VB","og_description":"This Visual Basic 2010 lesson shows you how to manage graphics in Visual Basic 2010","og_url":"http:\/\/www.vbtutor.net\/vb2010\/vb2010_lesson21.html","og_site_name":"Learn Visual Basic Programming \u2013 VB.NET, VBA &amp; Classic VB","article_publisher":"https:\/\/www.facebook.com\/Vbtutor","article_modified_time":"2018-06-24T14:49:27+00:00","og_image":[{"url":"https:\/\/www.vbtutor.net\/vb2010\/vb2010_Image\/lesson21_1.jpg","type":"","width":"","height":""}],"twitter_card":"summary_large_image","twitter_site":"@liewvk","twitter_misc":{"Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.vbtutor.net\/index.php\/visual-basic-2010-lesson-21\/","url":"http:\/\/www.vbtutor.net\/vb2010\/vb2010_lesson21.html","name":"Visual Basic 2010 Lesson 21- Managing Graphics - Learn Visual Basic Programming \u2013 VB.NET, VBA &amp; Classic VB","isPartOf":{"@id":"https:\/\/www.vbtutor.net\/#website"},"primaryImageOfPage":{"@id":"http:\/\/www.vbtutor.net\/vb2010\/vb2010_lesson21.html#primaryimage"},"image":{"@id":"http:\/\/www.vbtutor.net\/vb2010\/vb2010_lesson21.html#primaryimage"},"thumbnailUrl":"https:\/\/www.vbtutor.net\/vb2010\/vb2010_Image\/lesson21_1.jpg","datePublished":"2012-04-06T08:45:15+00:00","dateModified":"2018-06-24T14:49:27+00:00","description":"This Visual Basic 2010 lesson shows you how to manage graphics in Visual Basic 2010","breadcrumb":{"@id":"http:\/\/www.vbtutor.net\/vb2010\/vb2010_lesson21.html#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["http:\/\/www.vbtutor.net\/vb2010\/vb2010_lesson21.html"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"http:\/\/www.vbtutor.net\/vb2010\/vb2010_lesson21.html#primaryimage","url":"https:\/\/www.vbtutor.net\/vb2010\/vb2010_Image\/lesson21_1.jpg","contentUrl":"https:\/\/www.vbtutor.net\/vb2010\/vb2010_Image\/lesson21_1.jpg"},{"@type":"BreadcrumbList","@id":"http:\/\/www.vbtutor.net\/vb2010\/vb2010_lesson21.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.vbtutor.net\/"},{"@type":"ListItem","position":2,"name":"Visual Basic 2010 Lesson 21- Managing Graphics"}]},{"@type":"WebSite","@id":"https:\/\/www.vbtutor.net\/#website","url":"https:\/\/www.vbtutor.net\/","name":"Learn Visual Basic Programming \u2013 VB.NET, VBA &amp; Classic VB","description":"Start learning Visual Basic from beginner to advanced. Includes VB.NET, VBA, and classic VB tutorials for students and professionals.","inLanguage":"en-US"}]}},"_links":{"self":[{"href":"https:\/\/www.vbtutor.net\/index.php\/wp-json\/wp\/v2\/pages\/1110","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.vbtutor.net\/index.php\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.vbtutor.net\/index.php\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.vbtutor.net\/index.php\/wp-json\/wp\/v2\/users\/23013"}],"replies":[{"embeddable":true,"href":"https:\/\/www.vbtutor.net\/index.php\/wp-json\/wp\/v2\/comments?post=1110"}],"version-history":[{"count":42,"href":"https:\/\/www.vbtutor.net\/index.php\/wp-json\/wp\/v2\/pages\/1110\/revisions"}],"predecessor-version":[{"id":13141,"href":"https:\/\/www.vbtutor.net\/index.php\/wp-json\/wp\/v2\/pages\/1110\/revisions\/13141"}],"wp:attachment":[{"href":"https:\/\/www.vbtutor.net\/index.php\/wp-json\/wp\/v2\/media?parent=1110"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.vbtutor.net\/index.php\/wp-json\/wp\/v2\/categories?post=1110"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.vbtutor.net\/index.php\/wp-json\/wp\/v2\/tags?post=1110"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}