How to make commandbuttons

In this small tutorial I'm going to explain how command-buttons work and what you can do with it. I think everyone who has a little patience can understand this tutorial.

  1. First create a new Visual Basic project by clicking on File, New Project. Select Standard Exe and click on OK. On the left you see the toolbox. With this toolbox you can make the interface of your application. In this toolbox, select the icon third from above on the right. Go with your mouse on the form and click the left-mouse button. While holding the left-mousebutton move the mouse. You see a black rectangle. If you let go of the mousebutton, you've made a commandbutton with the size of the rectangle.

  2. Now, make another commandbutton. Name the first commandbutton 'cmdButton1' and the second 'cmdButton2'. You can do this with the help of the properties-window on the right of the screen. First select the commandbutton and then click on the property (name). It now says 'Command1', but change this into 'cmdButton1'. Do the same for Command2.

  3. The caption of cmdButton1 is 'Command1'. Change this into 'Button1' by selecting the property caption and then changing the caption 'Command1' in 'Button1'. Change the caption of cmdButton2 in 'Button2'. The letters of the commandbuttons are a bit small. You can make them bigger by selecting the property Font and change the size from 8 to 12 or something.

  4. Now we're going to make use of the commandbuttons. Let's say you want to show a text when you click on a button. This can easily be done. Make a label on the same way as the commandbutton. The label is the thing second from above on the left side of the toolbox.

  5. Name the label lblInfo and change the caption into 'Click on button 1'. Change the fontsize into 12. Make another label next to the second commandbutton and change the fontsize into 12, name it lblInfo2 and change the caption into 'Click on button 2'.

  6. We haven't really programmed yet, but we're going to start with it now. Double click on cmdButton1. What you see is the coding window. You see the text Private Sub cmdButton1_Click() and a few lines below that you see End sub. Between these two lines comes the code that says what will happen when you click on the commandbutton. Type the simple code 'Beep' here. If you run the project by pressing the play button above the code window, you can click on the command button and you will hear a beep.

  7. This is very nice, but not exactly what we want. In the code window, delete the text beep and type in the code: lblInfo.Caption = "You've clicked on command button 1!!" What happens when you click on cmdButton1 is that the caption of lblInfo changes from 'Click on button 1' into "You've clicked on command button 1!!".

  8. Now we're going to make the code for cmdButton2. Doubleclick on cmdButton2 and type the following code: X = X + 1
    lblInfo2.Caption = "The value of X is: " & X

    The variable X will be one higher every time you click on cmdButton2. This value of X will be shown on lblInfo2. But this code is incomplete. We have to declare X first, otherwise the computer won't know what X is. In the code window, click on the highest line. Here you can declare a variable X. Type the following line of code: Dim X As Integer. Now we have declared a variable X that is an integer. Start the project and see what happens.

If you don't understand everything or if you just want to download the application, then click HERE for the download.

If you have any questions or comments, please contact me. Click HERE to sent me an e-mail.

<-- Back to tutorials