|
First Program Lesson 2: Button & MsgBox: Your First Program In this lesson, we will walk through starting Visual Basic, creating a new Standard EXE project, and building your first program. I will show you how to add a command button to a form, write a simple line of code to display a message box when the button is clicked, run your program, and save your work properly. We will also briefly discuss the Visual Basic interface and some key windows within it, but most of the focus is on building and running your first program step by step. NavigationKeywords, Visual Basic 6.0, VB6, create first program, Hello World, Visual Basic project, Visual Basic form, command button, msgbox, code window, event handling, save VB project, FRM file, project file, My Documents, beginner programming
IntroIn this lesson, we will walk through starting Visual Basic, creating a new Standard EXE project, and building your first program. I will show you how to add a command button to a form, write a simple line of code to display a message box when the button is clicked, run your program, and save your work properly. We will also briefly discuss the Visual Basic interface and some key windows within it, but most of the focus is on building and running your first program step by step.TranscriptSo that is enough theory for today. Now let's start Visual Basic and build our first program.Here I am at the Windows Desktop. I am going to click on Start. All Programs. You should see Microsoft Visual Studio 6.0 or Visual Basic 6.0 depending on what you installed. Then you will see Microsoft Studio 6.0 Tools and Microsoft Visual Basic 6.0. This is what we are going to use today. We are not going to talk much about the tools, although we will use some of them in future classes. Go ahead and now click on Microsoft Visual Basic 6.0. The first window we get up is the New Project window. Now there are many different ways to start new projects. A project in Visual Basic is essentially your program. We will talk more about what projects are in a future class. But you can have standard programs, ActiveX programs. You can use the VB Application Wizard. There are many different ways to get started. For today, make sure Standard EXE is selected. Then come down here and click on Open. Now we are sitting at Project 1, a blank project, in Microsoft Visual Basic. You will notice the Visual Basic interface is very similar to other Windows programs. We have our title bar across the top, our menu bar, a toolbar. There is also a toolbox going down the side here. We will talk about some of these things in a little while. Over here is our big blank empty form. This is where our program will go. We will show you that in just a moment. Over here on the right we have our minimize, maximize, and close buttons. If you do not know what these are, you are in the wrong class. Get your hands on one of our Windows Basics courses and watch that one first. Down below we have a couple of different windows: a Project Window, a Properties Window, and a Form Layout Window. We will use some of these in a little while. Turn your attention for a moment, if you will, to this Form 1 window in the center. Just like the calculator program that we looked at a minute ago, most Visual Basic applications will run on a form. In fact, most Visual Basic applications will have one or more forms. Here is calculator again. Just picture in the background behind all these buttons, the big blank empty form. That is what we have here. This form is essentially our canvas, and on it we are going to paint different controls. Those controls are over here. We have buttons, labels, text boxes, pictures, and so on. Using these different controls, we can create our own custom programs, just like calculator. Let's start by building our very first program by putting a button on this form and making this button give us a little message when the user clicks on it. Sounds pretty simple. Let's dig our mouse and find this guy right here. This is a Command button. Go ahead and click on it. Take your mouse over here over the form. Notice that my mouse pointer has changed into a plus, instead of the pointer icon. Go ahead now and click and drag to draw a little box. Let it go, and that will create a Command button. The name of this button is Command1. In Visual Basic, all of the different objects that you will create, whether they are buttons, pictures, or text boxes, will all have a name. That name is one of their properties. In fact, you can see the name over here. Here is the Properties window. It says Properties for Command1. Right down here it says Name: Command1. Now that we have a button on our form, let's go ahead and run our program. In other words, we are going to start off our program and see what happens when we click on that button. To run your program, find this little button right here. It looks like a little triangle pointing to the right. Go ahead and click on that button now. You can see our program is now running. Here it says Form1 and Command1. Let's click on the button. I am clicking on the button. There we go. Click on it again and nothing is happening. Why is nothing happening? We have not told our program to do anything yet when the user clicks on that button. We have not given this button an event yet. So let's see how we do that. Let's go ahead and close our program just as we would close any normal Windows program. Notice how we are now back in the Visual Basic Design Editor. Now in order to tell this button to do something when we click on it, we have to give it an event. We have to do some programming. So let's double-click on this button. Take your mouse and double-click on it. Notice a code window opens up. This is called a Code window. Now here it says Private Sub Command1_Click() and then End Sub. What does all this mean? Well, for today's class, you do not have to know what all this stuff means. All you really have to know is that everything that happens between this line and this line will run whenever you click on that button. This is a Sub or subroutine that will run when the user clicks on the Command1 button. It starts here and it ends down here at the End Sub. Everything we type in the middle is going to go off when the user clicks on the Command1 button. The word Private just means that the subroutine is private to this specific form. Later on, we will be building programs with more than one form in future classes. Private just means that only this form can call this particular subroutine. But again, you do not have to worry about any of that stuff. We are going to skip most of the theory in today's class because I want to get you writing programs. All you have to worry about is that anything I type in here is going to run when you click on this button. All right, so let's put an actual command in here and see the program do something. Now I like to get myself some extra room because they only give you one line in there. So I am going to hit Enter a couple of times on my keyboard. There we go. Now I have plenty of blank space up in here. I am going to click back up here somewhere in the middle and I am going to tab in with the Tab key. Press the Tab key. That is going to move my little blinking cursor over to the right a little bit. It is just considered good programming form to indent your commands inside the Private Sub. Do you have to do this? Do you have to indent your code inside the subroutine? No, you do not have to. It is just when you build more complex and more complicated programs, the code can sometimes get difficult to read. So it makes it easier to read if it is indented. But you do not have to do that if you do not want to. Now I am going to give you your first command. I want you to type this in: message box. MSGBOX. That is message box. And then a space. Now you will notice a little yellow tool tip pops up. Do not worry about that. It is just giving you some suggestions to tell you what goes next. I want you to press open quotes and then Hello World and then close quotes and press Enter. We have now entered in our first command. Essentially, message box just pops a message up on the screen. This specific message box is going to say Hello World whenever the user clicks on this button. All right, let's give it a try. Let's come over here and now click on the Start button and see what happens. Here is our program. Our program is running now. Let's click on the Command1 button and see what we get. There it is. There is our message box that says Hello World. Click on the OK button. That will close the message box and put you back in your program. Congratulations. You have just written your first Visual Basic program. Give yourself a round of applause. Just not too loud because then the people around you will think there is something wrong with you. But be proud because you have just written your first program. Let's go ahead and close this. Now we are back in the Visual Basic editor. Let's go ahead and save our work. Let's click on the floppy disk button right here to save our program. Now for some strange reason, the folks at Microsoft decided to save Visual Basic data files in the VD98 folder. Why they did that, I have no idea. But of course, if you have taken any of my other classes, I tell you that it is important to save your data in your My Documents folder. So let's come in here and find our My Documents folder. There it is. Let's create a folder in here to store our VB stuff. I am going to click on the Create Folder button and I will type in VB Files. Then we can double-click on the folder to go into our VB Files folder. Now let's save the form. I will give the form a file name. I will call this My First Program. This will save the form as an FRM file. I will hit Save. Now we have to save the project file. The project file is an overall master file that keeps all the information about this particular program in one place. Again we can call this Our First Project. Let's call this Our First Project and hit Save. There we go. So now you see in just a few minutes you were able to create your own program, run it, make it do some stuff, and then save that program. You are now a programmer. QuizQ1. What is the purpose of the blank form in Visual Basic when you first start a new project?A. It acts as a canvas for adding controls to build your program interface B. It displays the output of your code C. It serves as the code editor for your project D. It is only used to save your project files Q2. Which control did the tutorial instruct you to place on the form for your first program? A. Label B. Text Box C. Command Button D. Picture Box Q3. What does the Properties window in Visual Basic allow you to do? A. Change project file locations B. Edit the properties of selected controls or objects C. View the final output of the program D. Draw shapes on the form Q4. What is the default name of the first Command Button you place on a form? A. Button1 B. Cmd C. Command D. Command1 Q5. What does the "Run" button (triangle pointing to the right) do in the Visual Basic interface? A. Opens the New Project window B. Starts running your current program C. Saves your project files D. Closes your application Q6. After placing a button on the form and running your program, why did nothing happen when the button was clicked? A. The button was disabled B. The program was not saved C. No event code was attached to the button yet D. The form was minimized Q7. What happens when you double-click a control, such as Command1, in the Visual Basic design window? A. A new copy of the control is created B. The Properties window closes C. A code window for an event handler opens D. The control is deleted Q8. What command did you type into the Command1_Click event handler to display a message box? A. PRINT "Hello World" B. SHOW "Hello World" C. MSGBOX "Hello World" D. ALERT "Hello World" Q9. What is the purpose of indenting code inside a Sub or Function in Visual Basic? A. To make the code easier to read and follow B. To speed up the program execution C. To increase the number of controls on the form D. To add more memory to the project Q10. Which folder does the tutorial recommend you use for saving your Visual Basic projects? A. The Windows folder B. The Program Files folder C. The My Documents folder D. The Desktop Q11. What file extension is used when saving a Visual Basic Form? A. .bas B. .frm C. .vbp D. .doc Q12. In Visual Basic, what does the word "Private" mean when used in Private Sub Command1_Click()? A. The subroutine is hidden from the user B. The code is encrypted C. The subroutine is only accessible by this specific form D. Anyone can call this subroutine from anywhere Q13. What is a Visual Basic Project? A. A graphic design tool B. The main file that organizes and manages your program C. A kind of database D. A Microsoft Word template Answers: 1-A; 2-C; 3-B; 4-D; 5-B; 6-C; 7-C; 8-C; 9-A; 10-C; 11-B; 12-C; 13-B DISCLAIMER: Quiz questions are AI generated. If you find any that are wrong, don't make sense, or aren't related to the video topic at hand, then please post a comment and let me know. Thanks. SummaryToday's video from Windows Learning Zone is all about getting started with Visual Basic and creating your very first program. We have already covered the foundational theory, so now it is time to jump into the software and put some of that knowledge into practice.To begin, I am assuming you are sitting at your Windows Desktop and have either Microsoft Visual Studio 6.0 or Visual Basic 6.0 installed, depending on your setup. The program we will use is Microsoft Visual Basic 6.0, not the Studio Tools that are also part of the package. While those tools will become useful in later lessons, today our focus is on building a basic program. When you first open Visual Basic, you will see the New Project window pop up. There are several project types to choose from, including standard programs, ActiveX projects, and different wizards. For what we are doing today, make sure that Standard EXE is selected, then open it. This starts you off with a new, blank project called Project 1 in the Visual Basic environment. The layout should look familiar if you have used other Windows programs. Along the top, you will see the title bar, menu bar, and a toolbar for quick access to common tasks. Down the left side is the toolbox, which we will use to add controls to our form. The large blank area in the middle is the form itself, which acts as the main window for your application. This is where all of your interface elements will live. On the right, you have the standard window control buttons - minimize, maximize, and close. If you are not comfortable with those, it might be worth revisiting a basic Windows course first. At the bottom, you will notice the Project Window, Properties Window, and Form Layout Window, all of which will become useful as your projects become more complex. The form in the center, currently named Form1, serves as your blank canvas, much like the background of a calculator app. All Visual Basic applications consist of at least one form, and often several. You can place all sorts of controls on this canvas, such as buttons, labels, text boxes, and images. For now, we will keep it simple and add just a button to the form. Select the Command Button control from the toolbox, then drag to draw it onto your form. This creates a new button, which by default is named Command1. Every object you place in your program, whether it is a button, text box, or something else, must have a unique name. You can see this reflected in the Properties window, where it currently shows Name: Command1. With the button in place, let us see what happens when we run our program. There is a run button in the toolbar, indicated by a small triangle pointing to the right. This compiles and starts your program in a new window. You will see your blank form with the button, but right now clicking the button does nothing. That is because there is no event associated with it yet; nothing has been programmed to respond when the button is pressed. To add functionality, close the running program and return to the design editor. To make the button do something, you need to tell Visual Basic what should happen when it is clicked. Do this by double-clicking the button. This opens the Code window, where you see a section labeled Private Sub Command1_Click() and End Sub. Anything written between these two lines will execute when the user clicks the button. You do not need to understand all of the technical details right now. Just know that a 'Sub' or subroutine here is a chunk of code that runs upon the specific event of clicking Command1. The word Private simply means this code is attached only to this particular form. Later in the course, we will look more closely at how private and public methods work between multiple forms. Inside this routine, I recommend indenting your code for readability, though this is not required if you prefer not to. Indenting becomes particularly useful as your programs grow larger and more complex. Now, for your first line of code, enter the command that displays a message box with some text. This is the classic 'Hello World' program. The idea is that, when the button is pressed, a pop-up will appear showing your message. Once you have typed the command, run your program again. When you click the button, you should see a message box appear with 'Hello World' displayed. Click OK, and it disappears, bringing you back to your running program. At this point, you have written and executed your very first Visual Basic program, a milestone that you should be proud of. Now it is time to save your work. Normally, Visual Basic will try to save your files in a default system folder, but I always recommend saving your programming projects in your My Documents directory to make them easier to find later. Create a folder there specifically for your Visual Basic files, open that folder, then save your work. You will first be prompted to save the form itself as an FRM file, and then the overall project as a VBP file. Give them meaningful names, such as My First Program for the form, and Our First Project for the project. In just a few minutes, you have managed to create a simple program, run it, interact with it, and save your work for future use. You are officially on your way to becoming a programmer. You can find a complete video tutorial with step-by-step instructions on everything discussed here on my website at the link below. Live long and prosper, my friends. Topic ListStarting Visual Basic from the Windows DesktopOpening a new project in Visual Basic Selecting the Standard EXE project type Overview of the Visual Basic interface Identifying the form as the main program area Using the toolbox to add controls to the form Adding a Command Button to the form Understanding control names and the Properties window Running the program in Visual Basic Explaining why the button does nothing by default Opening the code window by double-clicking the button Writing code for the Command1_Click event Using the MsgBox command to display a message Indenting code for readability Testing the program with a message box Saving the form and project files Creating a custom folder to store Visual Basic projects ArticleToday, we will put aside the theory and get started with practical Visual Basic programming. Let's walk through building your very first program together, starting from the Windows Desktop.Begin by finding Microsoft Visual Basic on your computer. Click on Start, then All Programs, and look for Microsoft Visual Studio or Microsoft Visual Basic, depending on what you installed. Once you find it, click on Microsoft Visual Basic to launch the program. When the program opens, you will see the New Project window. Here, you have several options for what kind of project you can create, but for our first program, make sure Standard EXE is selected. Then click Open. Now you will see Project 1, which is a blank project in Visual Basic ready for you to work on. The Visual Basic interface will probably look familiar. Across the top, you have a title bar, menu bar, and toolbar. Down the side, you will see a toolbox with different controls you can add to your program. In the center, you will see a large empty area called Form1. This form is the canvas where you will build your application, similar to the background of a calculator program where the buttons sit. Just like any other Windows window, Form1 has minimize, maximize, and close buttons in the top right corner. You will also notice several small windows below for managing your project, its properties, and the layout. For now, our main focus will be on Form1 and the toolbox. Most programs you build in Visual Basic will use one or more forms like this. Controls such as buttons, text boxes, or pictures can be placed on the form to make your program interactive. Think of the form as the blank area behind everything else, a canvas where you can design and customize your program. Let's start by adding a button that, when clicked, will display a message. This is a simple but classic first program, and it is a great introduction to how Visual Basic works. Find the command button control in the toolbox (it typically looks like a standard button). Click on it, and then move your mouse over to Form1. You will notice the mouse pointer changes to a crosshair. Click and drag on the form to draw a small rectangle, then let go. This puts a button on the form named Command1 by default. Each object you add to the form in Visual Basic will automatically get a name, which you will see in the Properties window. The Properties window displays different options for your selected control, including its name, which is useful when programming interactions. Now that you have a button, let's run the program to see how it looks. To do this, find and click the button with a right-pointing triangle icon in the toolbar. This starts your program. When the program runs, you will see your form and the button labeled Command1. Try clicking on the button. You might notice that nothing happens yet. That is because, so far, the button does not know what to do when it is clicked. We have not added any programming code or events to it. To make the button do something, you need to give it an event handler. Close your running program window to return to the Visual Basic design editor. Now, double-click on Command1 on your form. This opens the code window, where you can write simple instructions for your button. In the code window, you will see something like this: Private Sub Command1_Click() End Sub This section defines a subroutine, or Sub, that will run every time the user clicks the Command1 button. Any code you write between these lines will execute when the button is clicked. For now, you do not need to worry about the details of Private or exactly how Sub works - just understand that this is where you tell the button what should happen when it is clicked. To keep your code readable, it is helpful to indent the code inside the Sub. You can do this by pressing the Tab key before typing your command, but this is optional for small programs. Now, let's write your first line of code. Type the following inside the Command1_Click Sub, pressing Enter after you finish the line: MsgBox "Hello World" That is the only code you need. The MsgBox command displays a simple message box with whatever text you put inside the quotes. In this example, whenever the button is clicked, your program will show a message box saying Hello World. Let's test your program. Click the Start button with the triangle icon again to run your program. Click the Command1 button. You should see a message box pop up with the words Hello World. This confirms your first command is working exactly as intended. Click OK to close the message box and return to your program. Well done - you have just written your very first Visual Basic program. This is a great first step as a programmer. Once you are finished, close your program by clicking the close button, and you will return to the Visual Basic editor. Now, it is important to save your work. Click the floppy disk icon on the toolbar to save. You may be prompted for a location to save your files. Although Visual Basic might suggest saving in a default folder you do not usually use, it is good practice to save your files in your Documents folder so you can easily find them later. Create a folder called VB Files, if you like, to keep your Visual Basic projects organized. When saving, you will be asked to save your form file first. Give it a name such as My First Program. This file will have the .frm extension. Then you will be asked to save the main project file, which stores information about the project as a whole. You can name this something like Our First Project. Click Save. Now you have created, run, and saved your first Visual Basic program. You are officially a programmer, having successfully built and saved a working project. With these basics under your belt, you are ready to dive deeper into programming and begin making more complex applications. |
||
| |||||||||||||
| Keywords: , Visual Basic 6.0, VB6, create first program, Hello World, Visual Basic project, Visual Basic form, command button, msgbox, code window, event handling, save VB project, FRM file, project file, My Documents, beginner programming PermaLink How To Create Your First Program With a Button and Message Box in Microsoft Visual Basic 6 |