{"id":1330,"date":"2008-02-02T21:51:04","date_gmt":"2008-02-03T02:51:04","guid":{"rendered":"http:\/\/teamtutorials.com\/programming-tutorials\/basic-vb-code-%e2%80%93-changing-text"},"modified":"2008-02-02T22:05:02","modified_gmt":"2008-02-03T03:05:02","slug":"basic-vb-code-%e2%80%93-changing-text","status":"publish","type":"post","link":"https:\/\/teamtutorials.com\/programming-tutorials\/basic-vb-code-%e2%80%93-changing-text","title":{"rendered":"Basic VB Code \u2013 Changing Text"},"content":{"rendered":"

In this tutorial we will be looking at some of the more common, easy to use controls\/functions of VB. We are going to create a simple form and update it using other information that is provided by the user. When making forms it is a good practice to name all of the controls and labels on the form in a manner in which you can remember. This will allow for easy debugging if something were to go wrong and\/or needs to have a code change made. For example, I typically take the word that the command is like such as a button and turn it into a three letter acronym such as BTN. This way when scrolling through all of my objects I can automatically go to all of my buttons. Personally, I start all of my buttons with CMD as they generally \u201ccommand\u201d things to happen. So If I had a login form, everything would be named as shown below :<\/p>\n

Basic_VB_Programming_01<\/p>\n

As you can see my Text Boxes are name txtXXXXX, my Form is name frmMain, my Buttons are named cmdXXXXX and my Labels are named lblXXXXX. This is not 100% necessary but it will make life easier once you start getting into hundreds and then thousands of lines of code. <\/p>\n

To start, the screen shot below shows the properties windows when a button is selcted.<\/p>\n

Basic_VB_Programming_02<\/p>\n

All of the things listed in the column above can be modified during run time. By this, I mean that the configuration of the button can be changed using code after a certain event happens, for example: by clicking on a button we can change the text of a label to the contents of a text box that we have on the form. To call these features, you type the name of the control (ex. cmdExit) and then a period and type the name of the property you want to change. This brings us to our first form that we are going to create. Create the form as shown below: (for the sake of the tutorial, I am making two pictures, one that uses the \u201ctext field\u201d to show the name of all the items on the form so that you can name yours the same way so the code will work, and the second to show what that actual \u201ctext field\u201d of the item is.)<\/p>\n

Basic_VB_Programming_03<\/p>\n

One you have everything named, we need to start adding our code. To start, double-click on the Exit button. It will automatically open your code viewer\/editor and add the Sub Name and End Sub commands. Whatever you type between these fields will be run every time the exit button is clicked. The command for and Exit button is usually simple. Just type \u201cEnd\u201d. You should see this:<\/p>\n

\r\n    Private Sub cmdExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdExit.Click\r\n        End\r\n    End Sub\r\n<\/pre>\n

All this code does is kill the application if the Exit button is pressed. The end command kills the application and releases all system memory used to store the applications procedures. Next we need to add the code to the Change Text button to make the text of the label change to whatever text is in the text box provided. Double-click on the Change Text button for it to auto generate the opening code. Then type in the following code:<\/p>\n

\r\n    Private Sub cmdChange_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdChange.Click\r\n        If txtText.Text <> "" Then\r\n            lblText.Text = txtText.Text\r\n            txtText.Text = ""\r\n        Else\r\n            MsgBox("The textbox is blank. Please enter text into it. Thank-you.", MsgBoxStyle.Information, "No Text")\r\n        End If\r\n    End Sub\r\n<\/pre>\n

Let\u2019s break this code down a little bit to help understand what is going on. The first line:<\/p>\n

\r\nIf txtText.Text <> "" Then\r\n <\/pre>\n

Is setting up the \u201cIf, Then\u201d statement. It says If the text in txtText is not equal to (<> means not equal to) \u201c\u201d (blank) then do the following (anytime you are using a string (actual text that will be displayed as it is) you need to enclose it in quotes):<\/p>\n

\r\nlblText.Text = txtText.Text\r\ntxtText.Text = ""\r\n<\/pre>\n

These two lines are run on if the if statement is true. The first line is setting the label equal to whatever text is shown in the textbox. The second line is setting the textbox back to \u201c\u201d (blank). Then we have to tell it what to do incase the If statement is false. To do this we will need to use the else statement as shown below:<\/p>\n

\r\n        Else\r\n            MsgBox("The textbox is blank. Please enter text into it. Thank-you.", MsgBoxStyle.Information, "No Text")\r\n        End If\r\n <\/pre>\n

This code simple says that if the If statement is false, show a message box with the text \u201cThe textbox is blank. Please enter text into it. Thank-you.\u201d , with the formatting of a System Information box, with the title of \u201c No Text\u201d. This will alert the user that the textbox was empty and we don\u2019t want to make the label blank. It also uses \u201cEnd If\u201d to tell the app that it is the end of the If statement. Now we should be able to test the application by pressing the \u201cplay\u201d button. . Type \u201cI want to change the text of the label to this!!” into the textbox:<\/p>\n

Basic_VB_Programming_04<\/p>\n

As soon as you hit the change text button the label should change to what we typed in and the textbox should become empty again as shown:<\/p>\n

Basic_VB_Programming_05<\/p>\n

You should now be able to click on Change Text again to see our error message since the text box is empty.<\/p>\n

Basic_VB_Programming_06<\/p>\n

Now if you click exit it should kill the application. You have now completed the first tutorial in the basic VB programming series. Please check back for more. Thank-you for viewing. Please comment if you have any question or recommendation of what you would like to see.<\/p>\n","protected":false},"excerpt":{"rendered":"

This is the first of a series of tutorials to get you learning VB. These tutorials are going to teach you the very basics of VB programming and allow you to be able to learn more advanced features and coding.<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[25,26],"tags":[],"yoast_head":"\nBasic VB Code \u2013 Changing Text<\/title>\n<meta name=\"description\" content=\"This is the first of a series of tutorials to get you learning VB. These tutorials are going to teach you the very basics of VB programming and allow you to be able to learn more advanced features and coding.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/teamtutorials.com\/programming-tutorials\/basic-vb-code-\u2013-changing-text\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Mike Maguire\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"5 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebSite\",\"@id\":\"https:\/\/teamtutorials.com\/#website\",\"url\":\"https:\/\/teamtutorials.com\/\",\"name\":\"Team Tutorials\",\"description\":\"Learn something new today\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/teamtutorials.com\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"ImageObject\",\"@id\":\"https:\/\/teamtutorials.com\/programming-tutorials\/basic-vb-code-%e2%80%93-changing-text#primaryimage\",\"inLanguage\":\"en-US\",\"url\":\"http:\/\/teamtutorials.com\/wp-content\/uploads\/2008\/02\/basic_vb_programming_01.jpg\",\"contentUrl\":\"http:\/\/teamtutorials.com\/wp-content\/uploads\/2008\/02\/basic_vb_programming_01.jpg\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/teamtutorials.com\/programming-tutorials\/basic-vb-code-%e2%80%93-changing-text#webpage\",\"url\":\"https:\/\/teamtutorials.com\/programming-tutorials\/basic-vb-code-%e2%80%93-changing-text\",\"name\":\"Basic VB Code \\u2013 Changing Text\",\"isPartOf\":{\"@id\":\"https:\/\/teamtutorials.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/teamtutorials.com\/programming-tutorials\/basic-vb-code-%e2%80%93-changing-text#primaryimage\"},\"datePublished\":\"2008-02-03T02:51:04+00:00\",\"dateModified\":\"2008-02-03T03:05:02+00:00\",\"author\":{\"@id\":\"https:\/\/teamtutorials.com\/#\/schema\/person\/eb38d967529dbe49f7cbe082fd39105b\"},\"description\":\"This is the first of a series of tutorials to get you learning VB. These tutorials are going to teach you the very basics of VB programming and allow you to be able to learn more advanced features and coding.\",\"breadcrumb\":{\"@id\":\"https:\/\/teamtutorials.com\/programming-tutorials\/basic-vb-code-%e2%80%93-changing-text#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/teamtutorials.com\/programming-tutorials\/basic-vb-code-%e2%80%93-changing-text\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/teamtutorials.com\/programming-tutorials\/basic-vb-code-%e2%80%93-changing-text#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/teamtutorials.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Basic VB Code \\u2013 Changing Text\"}]},{\"@type\":\"Person\",\"@id\":\"https:\/\/teamtutorials.com\/#\/schema\/person\/eb38d967529dbe49f7cbe082fd39105b\",\"name\":\"Mike Maguire\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\/\/teamtutorials.com\/#personlogo\",\"inLanguage\":\"en-US\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/36f2aa9a11241ca79ed05e758e36f3cb?s=96&d=mm&r=r\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/36f2aa9a11241ca79ed05e758e36f3cb?s=96&d=mm&r=r\",\"caption\":\"Mike Maguire\"},\"sameAs\":[\"http:\/\/mikemaguire.me\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Basic VB Code \u2013 Changing Text","description":"This is the first of a series of tutorials to get you learning VB. These tutorials are going to teach you the very basics of VB programming and allow you to be able to learn more advanced features and coding.","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":"https:\/\/teamtutorials.com\/programming-tutorials\/basic-vb-code-\u2013-changing-text","twitter_misc":{"Written by":"Mike Maguire","Est. reading time":"5 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebSite","@id":"https:\/\/teamtutorials.com\/#website","url":"https:\/\/teamtutorials.com\/","name":"Team Tutorials","description":"Learn something new today","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/teamtutorials.com\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"ImageObject","@id":"https:\/\/teamtutorials.com\/programming-tutorials\/basic-vb-code-%e2%80%93-changing-text#primaryimage","inLanguage":"en-US","url":"http:\/\/teamtutorials.com\/wp-content\/uploads\/2008\/02\/basic_vb_programming_01.jpg","contentUrl":"http:\/\/teamtutorials.com\/wp-content\/uploads\/2008\/02\/basic_vb_programming_01.jpg"},{"@type":"WebPage","@id":"https:\/\/teamtutorials.com\/programming-tutorials\/basic-vb-code-%e2%80%93-changing-text#webpage","url":"https:\/\/teamtutorials.com\/programming-tutorials\/basic-vb-code-%e2%80%93-changing-text","name":"Basic VB Code \u2013 Changing Text","isPartOf":{"@id":"https:\/\/teamtutorials.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/teamtutorials.com\/programming-tutorials\/basic-vb-code-%e2%80%93-changing-text#primaryimage"},"datePublished":"2008-02-03T02:51:04+00:00","dateModified":"2008-02-03T03:05:02+00:00","author":{"@id":"https:\/\/teamtutorials.com\/#\/schema\/person\/eb38d967529dbe49f7cbe082fd39105b"},"description":"This is the first of a series of tutorials to get you learning VB. These tutorials are going to teach you the very basics of VB programming and allow you to be able to learn more advanced features and coding.","breadcrumb":{"@id":"https:\/\/teamtutorials.com\/programming-tutorials\/basic-vb-code-%e2%80%93-changing-text#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/teamtutorials.com\/programming-tutorials\/basic-vb-code-%e2%80%93-changing-text"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/teamtutorials.com\/programming-tutorials\/basic-vb-code-%e2%80%93-changing-text#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/teamtutorials.com\/"},{"@type":"ListItem","position":2,"name":"Basic VB Code \u2013 Changing Text"}]},{"@type":"Person","@id":"https:\/\/teamtutorials.com\/#\/schema\/person\/eb38d967529dbe49f7cbe082fd39105b","name":"Mike Maguire","image":{"@type":"ImageObject","@id":"https:\/\/teamtutorials.com\/#personlogo","inLanguage":"en-US","url":"https:\/\/secure.gravatar.com\/avatar\/36f2aa9a11241ca79ed05e758e36f3cb?s=96&d=mm&r=r","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/36f2aa9a11241ca79ed05e758e36f3cb?s=96&d=mm&r=r","caption":"Mike Maguire"},"sameAs":["http:\/\/mikemaguire.me"]}]}},"_links":{"self":[{"href":"https:\/\/teamtutorials.com\/wp-json\/wp\/v2\/posts\/1330"}],"collection":[{"href":"https:\/\/teamtutorials.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/teamtutorials.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/teamtutorials.com\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/teamtutorials.com\/wp-json\/wp\/v2\/comments?post=1330"}],"version-history":[{"count":0,"href":"https:\/\/teamtutorials.com\/wp-json\/wp\/v2\/posts\/1330\/revisions"}],"wp:attachment":[{"href":"https:\/\/teamtutorials.com\/wp-json\/wp\/v2\/media?parent=1330"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/teamtutorials.com\/wp-json\/wp\/v2\/categories?post=1330"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/teamtutorials.com\/wp-json\/wp\/v2\/tags?post=1330"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}