Check out our newest creation Tutorial Grad. The automated tutorial directory.

Subscribe to our rss feed

pc scan tool

Simple VB Code with Command Buttons

Posted in Programming Tutorials, Visual Basic 2005/2008 by Mike Maguire on the February 6th, 2008

In this tutorial we will look into changing other controls in the command of varying buttons. I am trying to keep these VB tutorials quick and simple so that they are easy to understand and you are able to do this stuff enough to be able to remember it. First we need to create the form. I will post two pictures, one of the form showing what I called all the controls on the form, and one that shows the text that I applied to each one. In this tutorial I will be using panels to show the changes made. Since panels contain no text I can’t put their names on them. I simply named them pnlXXXX (where XXXX is the same as the button below it Ex. The one on the left is pnlSize).

Basic_VB_Programming_2_01

Note: Make sure you make all 4 of the panels have a border style of FixedSingle under the properties menu. Now we can start adding the code to make these buttons do the changes we want to make. First lets program the exit button as it is the most simple to do. Remember, to start programming for a button, all we need to do is double-click on it in the GUI section. So, let’s double click on our Exit button and add the word “End” to it as below:

    Private Sub cmdExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdExit.Click
        End
    End Sub

Next we need to program the first panel to first change to a smaller size when we first click the button and then change back to normal when we click it again. To differentiate between which mode it is in we will use and if statement to check its current size. Double click on the Change Size button to start adding the code. All of my panels are a size of : “66, 32” – Shown as Width, Height. Add the code as shown below:

    Private Sub cmdSize_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSize.Click
        If pnlSize.Width = 66 Then
            pnlSize.Width = 33
        Else
            pnlSize.Width = 66
        End If
    End Sub

The code simply checks to see if the panel is at its current width of 66 pixels and if it is, it changes it to 33 pixels. If it’s not at 66 it must be 33 pixels and sets it back to 66. Next we need to code our color changing button. Note: Please set this panel’s backcolor property to Red (once you clikc backcolor an option menu will come up, select the web optioni and go to red)before putting in the code. We are going to make this a little trickier by throwing in and ElseIf statement. I will explain after I show you the code:

    Private Sub cmdColor_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdColor.Click
        If pnlColor.BackColor = Color.Red Then
            pnlColor.BackColor = Color.Blue
        ElseIf pnlColor.BackColor = Color.Blue Then
            pnlColor.BackColor = Color.Orange
        Else
            pnlColor.BackColor = Color.Red
        End If
    End Sub

This code is the same as the If statement that we had before in our previous tutorial but we added one more condition. This time the application checks to see if the box has the backcolor of red(If) and if it does it changes it to blue. If it is not Red then it checks to see if it is blue (Else If) and if it is it changes it to orange. If it is not red or blue it changes it to red (Else). This way it will make a continuous loop from Red to Blue to Orange. Our next button is going to change the border type of our panel. This is the code that should be place within the sub after double-clicking on the button:

    Private Sub cmdBorder_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdBorder.Click
        If pnlBorder.BorderStyle = BorderStyle.FixedSingle Then
            pnlBorder.BorderStyle = BorderStyle.Fixed3D
        ElseIf pnlBorder.BorderStyle = BorderStyle.Fixed3D Then
            pnlBorder.BorderStyle = BorderStyle.None
        Else
            pnlBorder.BorderStyle = BorderStyle.FixedSingle
        End If
    End Sub

This code is similar to our previous button except we are doing it with borders. There are three types of border (well, two really and then no border). This section of code is checking to see if our panel has a borderstyle of FixedSingle (which is how it is starting) and if it is, sets it to have a style of Fixed3D (If). If it is not Fixedsingle it checks to see if it is Fixed3D, and changes it to none if it is (Else IF). If it is not FixedSingle or Fixed3D it changes it to FixedSingle (Else) Our next and last button is going to change the visibility of the form (Make is visible or not). Here is the code:

    Private Sub cmdVisible_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdVisible.Click
        If pnlVisible.Visible = True Then
            pnlVisible.Visible = False
        Else
            pnlVisible.Visible = True
        End If
    End Sub

This code is not different from our others really. Visibility is known as a Boolean Type. It is either true or false. It is checking to see if the panel’s visibility is currently set to true and if it is it sets it to false, otherwise it sets it to true. This should conclude the code of the tutorial. You should no be able to run the application and the button will make the panels do their respective tasks. If you have any questions please comment. Thanks for viewing.

Popularity: 16% [?]

Share and Enjoy:
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • Reddit

Other Related Tutorials

Tutorial Grad - Recent Tutorials

30 Responses to 'Simple VB Code with Command Buttons'

Subscribe to comments with RSS or TrackBack to 'Simple VB Code with Command Buttons'.

  1. Monish Shah said,

    on March 7th, 2008 at 12:58 am

    Please tell me solution of the following

    1) – When i create a command button in vb 2005 form to open another it is been working properly but when i create a back button command in another form to show the previous form it does not work.

    2) – How to move from one form to another by closing first form. This is in vb 2005

  2. Mike Maguire said,

    on March 7th, 2008 at 3:23 pm

    Are you hiding the original form or leaving it up behind the form you are opening? If you are leaving it up you should be able to close the new form and your original form should appear. If you are closing the form, which command are you using to close it?


  3. on March 10th, 2008 at 12:26 am

    baoninam!!!vuvu…aliwa ya ai code u..DOGYUT

  4. Monish said,

    on March 30th, 2008 at 1:45 pm

    guys i wanted the code for search button
    here the button searches the text in textbox

    and display the result in the relative text box

  5. mell said,

    on April 20th, 2008 at 11:37 pm

    hye…my name is mellissa…all i want to know now…could u please give me a very good book about vb…basic one…but very up to date…including pictures in case if i didn’t get the coding…like how to make a password n stuff like that…please do recommend a very good book please…thank u

  6. Sam said,

    on April 22nd, 2008 at 3:28 am

    Hey Am Sam I want to code a search button to be such that,I can sear ch using Both names”Text ” and Numbers.The part of searching using numbers am ok with it but for text…Its really giving me head ache…
    I will appreciate.

  7. Sam said,

    on April 25th, 2008 at 5:25 am

    I just finished my project in visual Basics

  8. Sam said,

    on April 25th, 2008 at 5:31 am

    Ave just finished my vb project and compiled it to an exefile.Ave also packaged it…but when i try to installe it …I get an error mesage “msado…file is missing.with option of whether to ignore.abort or retry…I choose Ignore after retrying for several times …The problem is after installing ..It can run…It displaying a dialog box indicating that its suching for a certain file what might be the problem?

  9. amb said,

    on May 5th, 2008 at 4:18 am

    hi helpme code for the delete ,search commands and connect the project to database in access
    your website is good

  10. sam said,

    on May 5th, 2008 at 12:10 pm

    Hi there,

    Thanks for your your tutorial. It’s just the sort of thing I’ve been looking for.

    I am a beginner and I understand everything here. It works perfectly!

    Excellent site – I’ll be back!

  11. hate said,

    on May 28th, 2008 at 8:03 am

    Hi there,
    I just want to ask what is the code I need if I want to send output signal to the parallel port? Im going to use this for controlling relays. Thanks in advance

  12. accounting chick said,

    on August 11th, 2008 at 9:28 pm

    Hi,

    I am creating a database that has a switchboard in it and I need to know how to create a command button that you can press to work your way back through the switchboards that you have viewed. Does anyone know the code that I can use to do this?

    Thanks.


  13. on August 12th, 2008 at 4:20 am

    Accounting chick,
    Can you explain in a little more detail what you have already and what you are trying to achieve and maybe I can create/help you find a solution for you?

  14. jerry said,

    on August 13th, 2008 at 4:01 am

    hi just wanna ask how to put database on visual basic !_!
    need reply asap

  15. Mike Maguire said,

    on August 13th, 2008 at 5:01 am

    You want to store it in visual basic, or you want to connect to it using VB?

  16. Soso said,

    on August 16th, 2008 at 10:51 am

    I m usng VB on microsoft Office bt all my forms are not doing what they are suppose to so plz help me about what l must do

  17. ibrahim said,

    on September 18th, 2008 at 3:36 pm

    can you help me to turn the digital image into paint oil or water color form in visual basic 6

  18. Catherine said,

    on February 3rd, 2009 at 11:36 pm

    Hi!!

    guys i wanted to know the code of the search button
    in visual basic….
    can you help me??

  19. Ben said,

    on February 23rd, 2009 at 7:26 pm

    I’m having the worst time trying to find code for (a+) and (a-) buttons to make the text bigger and smaller. Computer science is hard.

  20. Ben said,

    on February 23rd, 2009 at 7:27 pm

    Email me at Bennymyster@comcast.net if there’s any possible way you can help me.

  21. Kathlena said,

    on May 22nd, 2009 at 9:48 am

    Hii,

    All I want 2 know is, how do you program a “Next Button(command button) ” to turn to another part of a program??

  22. Mike Maguire said,

    on May 22nd, 2009 at 10:35 am

    What exactly do you mean by turning to another part of the program. If you are a little more descriptive, I should be able to help you with what you need.

  23. ammerigader said,

    on June 21st, 2009 at 1:51 pm

    Hi there, If you don’t like topics with many links, just delete this topic.
    Thankyou.

  24. richard said,

    on August 18th, 2009 at 4:54 am

    Hi im richard do you have any sample code for the vbgames like checkers..and how to code the search button using datacontrol can u please help me about that, tHANKS….

  25. grace said,

    on August 18th, 2009 at 4:57 am

    hi there…wats up…can u please help me about my problem can you please give me sample program for the vbgames like chinese checkers..and also the for searching using the data control..

  26. grace said,

    on August 18th, 2009 at 4:58 am

    how to code in searching using the data control…..can you please answer that for me …

  27. jazilel said,

    on August 18th, 2009 at 5:00 am

    hi can you please give me a sample codes for vbgames like checkers thanks…please put a sample program to your website that more understandable thanks…

  28. jazilel said,

    on August 18th, 2009 at 5:05 am

    to jerry it very simple you know just like this you need to put a data to your form then set your databasename to the vb and also the recordset..then you need to code for it…

  29. jammi said,

    on August 31st, 2009 at 5:39 am

    can u tell me all about compilers of c++

  30. THuso said,

    on October 22nd, 2009 at 5:39 am

    Please assist me with a code for a search button using Visual Basic and searching multiple fields from SQL

Leave a Comment