{"id":1112,"date":"2012-04-06T16:45:32","date_gmt":"2012-04-06T08:45:32","guid":{"rendered":"http:\/\/www.vbtutor.net\/index.php\/"},"modified":"2018-06-24T22:54:39","modified_gmt":"2018-06-24T14:54:39","slug":"visual-basic-2010-lesson-22","status":"publish","type":"page","link":"https:\/\/www.vbtutor.net\/index.php\/visual-basic-2010-lesson-22\/","title":{"rendered":"Visual Basic 2010 Lesson 22 &#8211; The DrawRectangle Method"},"content":{"rendered":"<p align=\"center\"><strong><a href=\"http:\/\/www.vbtutor.net\/index.php\/visual-basic-2010-lesson-21\/\">[Lesson 21]<\/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-23\/\"> [Lesson 23]<\/a><\/strong><\/p>\n<h3>22.1 Creating a Rectangle with DrawRectangle Method<\/h3>\n<p>There are two methods to draw a rectangle on the screen in VB2010:<\/p>\n<h4>Method 1<\/h4>\n<p>Use the <strong>DrawRectangle<\/strong> method by specifying its upper-left corner&#8217;s coordinate and it width and height. You also need to create a Graphics and a Pen object to handle the actual drawing. The syntax is:<\/p>\n<pre style=\"font-size: 110%; width: 80%;\">myGrapphics.DrawRectangle(myPen, X, Y, width, height)<\/pre>\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><br \/>\n*myGraphics is the variable name of the Graphics object and myPen is the variable name of the Pen object created by you. X, Y is the coordinate of the upper left corner of the rectangle.<\/p>\n<h4>The code<\/h4>\n<pre style=\"font-size: 110%;\">Dim myPen As Pen\r\n myPen = New Pen(Drawing.Color.Blue, 5)\r\nDim myGraphics As Graphics = Me.CreateGraphics\r\n myGraphics.DrawRectangle(myPen, 0, 0, 100, 50)\r\n<\/pre>\n<h4>Method 2<\/h4>\n<p>Create a rectangle object first and then draw this rectangle using the DrawRectangle method. The syntax is as shown below:<\/p>\n<pre style=\"font-size: 110%; width: 80%;\">myGraphics.DrawRectangle(myPen,myRectangle)\r\n<\/pre>\n<p>where myRectangle is the rectangle object created by you, the user.<\/p>\n<p>The code is:<\/p>\n<pre style=\"font-size: 110%; width: 80%;\">Dim myRectangle As New Rectangle\r\n myRect.X = 10\r\n myRect.Y = 10\r\n myRect.Width = 100\r\n myRect.Height = 50\r\n<\/pre>\n<p>You can also create a rectangle object using a one-line code as follows:<\/p>\n<pre style=\"font-size: 110%;\">Dim myRectangle As New Rectangle(X,Y,width, height)\r\n<\/pre>\n<p>and the code to draw the above rectangle is<\/p>\n<pre style=\"font-size: 110%; width: 80%;\">myGraphics.DrawRectangle(myPen, myRectangle)\r\n<\/pre>\n<h3>22.2 Customizing Line Style of the Pen Object<\/h3>\n<p>The shape we draw so far are drawn with a solid line, we can actually customize the line style of the Pen object so that we have dotted line, a line consisting of dashes and more. For example, the syntax to draw a dotted line is shown below:<\/p>\n<pre style=\"font-size: 110%; width: 80%;\">myPen.DashStyle=Drawing.Drawing2D.DashStyle.Dot\r\n<\/pre>\n<p>Where the last argument Dot specifies a particular line DashStyle value, a line that makes up of dots here. The following code draws a rectangle with the red dotted line.<\/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 myPen As Pen\r\n myPen = New Pen(Drawing.Color.Red, 5)\r\nDim myGraphics As Graphics = Me.CreateGraphics\r\n myPen.DashStyle = Drawing.Drawing2D.DashStyle.Dot\r\n myGraphics.DrawRectangle(myPen, 10, 10, 100, 50)\r\nEnd Sub\r\n<\/pre>\n<p>The output image is shown below:<br \/>\n<img loading=\"lazy\" decoding=\"async\" class=\"aligncenter\" src=\"https:\/\/www.vbtutor.net\/vb2010\/vb2010_Image\/lesson22_1.jpg\" alt=\"Visual Basic 2010\" width=\"300\" height=\"300\" \/><\/p>\n<h6 align=\"center\">Figure 22.1<\/h6>\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<p>If you change the DashStyle value to DashDotDot, you can draw rectangles with different border, as shown in Figure 22.2.<\/p>\n<p>The possible values of the line DashStyle of the Pen are listed in the table below:<\/p>\n<div style=\"overflow-x:auto;\">\n<table align=\"center\">\n<tbody>\n<tr>\n<th>DashStyle<br \/>\n                  Value<\/th>\n<th>Line Style<\/th>\n<\/tr>\n<tr>\n<td>Dot<\/td>\n<td>Line consists of dots<\/td>\n<\/tr>\n<tr>\n<td>Dash<\/td>\n<td>Line consists of dashes<\/td>\n<\/tr>\n<tr>\n<td>DashDot<\/td>\n<td>Line consists of alternating dashes and<br \/>\n                  dots<\/td>\n<\/tr>\n<tr>\n<td>DashDotDot<\/td>\n<td>Line consists of alternating dashes and<br \/>\n                  double dots<\/td>\n<\/tr>\n<tr>\n<td>Solid<\/td>\n<td>Solid line<\/td>\n<\/tr>\n<tr>\n<td>Custom<\/td>\n<td>Custom line style<\/td>\n<\/tr>\n<\/tbody>\n<\/table><\/div>\n<p style=\"text-align: center;\"><img decoding=\"async\" src=\"https:\/\/www.vbtutor.net\/wordpress\/wp-content\/uploads\/2014\/01\/vb2013_figure26.2.jpg\" alt=\"vb2010\"style=\"width: auto ; max-width: 100%;height: auto\" \/><\/a><\/p>\n<h6 align=\"center\">Figure 22.2<\/h6>\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<p align=\"center\"><strong><strong><a href=\"http:\/\/www.vbtutor.net\/index.php\/visual-basic-2010-lesson-21\/\">[Lesson 21]<\/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-23\/\"> [Lesson 23]<\/a><\/strong><\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>[Lesson 21] &lt;&lt;\u00a0[CONTENTS] &gt;&gt; [Lesson 23] 22.1 Creating a Rectangle with DrawRectangle Method There are two methods to draw a rectangle on the screen in VB2010: Method 1 Use the DrawRectangle method by specifying its upper-left corner&#8217;s coordinate and it width and height. You also need to create a Graphics and a Pen object to &hellip; <a href=\"https:\/\/www.vbtutor.net\/index.php\/visual-basic-2010-lesson-22\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Visual Basic 2010 Lesson 22 &#8211; The DrawRectangle Method<\/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-1112","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 22 - The DrawRectangle Method - Learn Visual Basic Programming \u2013 VB.NET, VBA &amp; Classic VB<\/title>\n<meta name=\"description\" content=\"This Visual Basic 2010 shows how to draw rectangle 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_lesson22.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 22 - The DrawRectangle Method - Learn Visual Basic Programming \u2013 VB.NET, VBA &amp; Classic VB\" \/>\n<meta property=\"og:description\" content=\"This Visual Basic 2010 shows how to draw rectangle in Visual Basic 2010\" \/>\n<meta property=\"og:url\" content=\"http:\/\/www.vbtutor.net\/vb2010\/vb2010_lesson22.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:54:39+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.vbtutor.net\/vb2010\/vb2010_Image\/lesson22_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=\"2 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-22\/\",\"url\":\"http:\/\/www.vbtutor.net\/vb2010\/vb2010_lesson22.html\",\"name\":\"Visual Basic 2010 Lesson 22 - The DrawRectangle Method - 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_lesson22.html#primaryimage\"},\"image\":{\"@id\":\"http:\/\/www.vbtutor.net\/vb2010\/vb2010_lesson22.html#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.vbtutor.net\/vb2010\/vb2010_Image\/lesson22_1.jpg\",\"datePublished\":\"2012-04-06T08:45:32+00:00\",\"dateModified\":\"2018-06-24T14:54:39+00:00\",\"description\":\"This Visual Basic 2010 shows how to draw rectangle in Visual Basic 2010\",\"breadcrumb\":{\"@id\":\"http:\/\/www.vbtutor.net\/vb2010\/vb2010_lesson22.html#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"http:\/\/www.vbtutor.net\/vb2010\/vb2010_lesson22.html\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"http:\/\/www.vbtutor.net\/vb2010\/vb2010_lesson22.html#primaryimage\",\"url\":\"https:\/\/www.vbtutor.net\/vb2010\/vb2010_Image\/lesson22_1.jpg\",\"contentUrl\":\"https:\/\/www.vbtutor.net\/vb2010\/vb2010_Image\/lesson22_1.jpg\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"http:\/\/www.vbtutor.net\/vb2010\/vb2010_lesson22.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.vbtutor.net\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Visual Basic 2010 Lesson 22 &#8211; The DrawRectangle Method\"}]},{\"@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 22 - The DrawRectangle Method - Learn Visual Basic Programming \u2013 VB.NET, VBA &amp; Classic VB","description":"This Visual Basic 2010 shows how to draw rectangle 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_lesson22.html","og_locale":"en_US","og_type":"article","og_title":"Visual Basic 2010 Lesson 22 - The DrawRectangle Method - Learn Visual Basic Programming \u2013 VB.NET, VBA &amp; Classic VB","og_description":"This Visual Basic 2010 shows how to draw rectangle in Visual Basic 2010","og_url":"http:\/\/www.vbtutor.net\/vb2010\/vb2010_lesson22.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:54:39+00:00","og_image":[{"url":"https:\/\/www.vbtutor.net\/vb2010\/vb2010_Image\/lesson22_1.jpg","type":"","width":"","height":""}],"twitter_card":"summary_large_image","twitter_site":"@liewvk","twitter_misc":{"Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.vbtutor.net\/index.php\/visual-basic-2010-lesson-22\/","url":"http:\/\/www.vbtutor.net\/vb2010\/vb2010_lesson22.html","name":"Visual Basic 2010 Lesson 22 - The DrawRectangle Method - 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_lesson22.html#primaryimage"},"image":{"@id":"http:\/\/www.vbtutor.net\/vb2010\/vb2010_lesson22.html#primaryimage"},"thumbnailUrl":"https:\/\/www.vbtutor.net\/vb2010\/vb2010_Image\/lesson22_1.jpg","datePublished":"2012-04-06T08:45:32+00:00","dateModified":"2018-06-24T14:54:39+00:00","description":"This Visual Basic 2010 shows how to draw rectangle in Visual Basic 2010","breadcrumb":{"@id":"http:\/\/www.vbtutor.net\/vb2010\/vb2010_lesson22.html#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["http:\/\/www.vbtutor.net\/vb2010\/vb2010_lesson22.html"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"http:\/\/www.vbtutor.net\/vb2010\/vb2010_lesson22.html#primaryimage","url":"https:\/\/www.vbtutor.net\/vb2010\/vb2010_Image\/lesson22_1.jpg","contentUrl":"https:\/\/www.vbtutor.net\/vb2010\/vb2010_Image\/lesson22_1.jpg"},{"@type":"BreadcrumbList","@id":"http:\/\/www.vbtutor.net\/vb2010\/vb2010_lesson22.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.vbtutor.net\/"},{"@type":"ListItem","position":2,"name":"Visual Basic 2010 Lesson 22 &#8211; The DrawRectangle Method"}]},{"@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\/1112","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=1112"}],"version-history":[{"count":54,"href":"https:\/\/www.vbtutor.net\/index.php\/wp-json\/wp\/v2\/pages\/1112\/revisions"}],"predecessor-version":[{"id":13143,"href":"https:\/\/www.vbtutor.net\/index.php\/wp-json\/wp\/v2\/pages\/1112\/revisions\/13143"}],"wp:attachment":[{"href":"https:\/\/www.vbtutor.net\/index.php\/wp-json\/wp\/v2\/media?parent=1112"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.vbtutor.net\/index.php\/wp-json\/wp\/v2\/categories?post=1112"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.vbtutor.net\/index.php\/wp-json\/wp\/v2\/tags?post=1112"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}