Simple Steps in VB.NET. Part 2 - Address Book
Article source code: ssteps2_addressbook.zip
Part 1 introduced very basic, but essential functions. Our next project will also be very simple. However, it is a step up for the absolute beginner and takes more time to complete. You wouldn't expect me to stay at one and the same level for the next 154,374 years, would you?
It's important that you realize that no matter how advanced you are - or not, you must build on your knowledge. Building on knowledge means that you use what you know to expand. Don't worry. You don't need to be like Albert Einstein to succeed with this project. Speaking of ol' Al; I'm a mixture of Albert Einstein and Arnold Schwarzenegger. The only trouble is that I got Einstein's body and Schwarzenegger's brain.
It may be of advantage to be familiar with what we learned in part 1. We will use most of those functions again during this project, but some of them in a different way. That also means that I'm only going to explain differences when absolutely essential, but I won't start from scratch. If you miss an explanation, you will certainly find it in part 1. If you don't, feel free to mail me. For this project, you should know how to:
- Open VB.NET/change the default settings under "My Profile"
- Create a project and give it a name
- Change the title and the startup position of a form
- Insert buttons, move, rename, and centralize them.
- Change the text of buttons
- Code a button to close a program
- Insert single-line tool tips
- Build a program in debug and release mode
You need a Microsoft Access file. Don't worry if you don't know how to do this. Just download my sample code and copy the Access file from the bin-folder to your documents folder. For further information on running the sample code, please scroll down to the end. At this moment, I want to keep it as simple as possible. Later on, we will learn all the necessary steps to make a data table with Access. If you do know how to use Microsoft Access, create a table with the following items:
We will not yet deal with error handling. That's why you can let all fields accept text input. Save your Access file under a unique name, and do the same with the table. I called my Access file AddressBook (extraordinarily creative, isn't it?), and I called the table AddressBooktb.
What we are going to learn today is how to:
- Use the Data Form Wizard to create a data-access program
- Change the startup object
- Reorganize the layout (labels, textboxes, buttons, size)
- Make a textbox read-only to avoid user input
- Change the color of a form/textbox
Don't think that this will finish the project. I have divided this project into parts so that you won't be overwhelmed with new information. I'm also thinking of adding some fun-features. Individual parts should take approximately one hour for a beginner to complete. I know, if you're not a real beginner any more you will be able to do all this in a couple of minutes. That's why I tried the project with my wife - my guinea pig - because she doesn't have the slightest idea on programming. Well, she's actually more like a house-dragon, but that's another subject.
Start a Visual Basic Windows Application called Addresses. You don't have to do anything with Form1 right now as we need it later. Instead, click on the File Menu and then click on Add New Item. The window that opens should be this (Figure1):
Click on Data Form Wizard in the right pane and then open it. You can also simply double-click the Data Form Wizard (Figure2).
After double-clicking, the Wizard should appear - I mean, the Data Form Wizard - what did you think? The Wizard of Oz, or what? (Figure3)
Hit Next to continue, or hit Cancel if you're fed up. When you click Next, the following window will appear (Figure4):
Where it says "Create a new dataset named:" input a name you want to use. I called mine "MyAddresses" as you can see in Figure 5:
Click on Next to continue. The wizard will ask you, "Which data connection should the wizard use?". If you have never tried any data access, the drop-down box will be empty. To continue, click New Connection (Figure6).
In the following window, click the Provider tab and double-click Microsoft Jet4.0 OLE DB Provider as shown in Figure 7. Ah, yes, and don't worry about the strange language you see in my figures; it's because my Windows is a Portuguese edition. That shouldn't prevent you from doing it right. He-he - wait until you see my Access, which is in German. I got some Russian and Chinese for you later - I'm just kidding.
After double-clicking the Microsoft Jet4.0 OLE DB Provider option, the Connection tab will be displayed (Figure8).
On the right from "1.Select or insert a name of a database" you will see an ellipsis (Figure9).
Click on the ellipsis, search for the Access file (AddressBook), and click it. Click on Test connection to see if everything is okay. A message box will tell you if the connection was successful or not. Should you receive an error message, do it all over again to find out where you went wrong. I promise you - it works. If that sweet little message told you that everything is just fine, click the OK - button. C'mon, there is no other button on that message box. Then click OK to display this window (Figure10):
It's the same window that you saw in Figure6, but now it's displaying the connection in the drop-down box. Hit Next to see this window (Figure11):
Click the AddressBooktb and then Figure12, or double-click AddressBooktb.
Click Next to see the following window (Figure13):
You will see all the items from the table you have created, plus an additional item called ID. The additional item (ID) generates an entry-number automatically. You could uncheck this item if you don't want to have it in your form. For this project, though, leave it there and click Next.
The next - and finally last window looks like this (Figure14):
The only change you should make is to click the radio button "Single record in individual controls". This will create several buttons, aside from individual textboxes with the corresponding labels. It's a good idea to read through so that you know what it will do exactly with this option. Finally - Aha! - click Finish and watch Oz (The Wizard) do the hard job for you. The wizard will code all essential things for you. That's why I have chosen this kind of project. Later we will see how to do it manually, and we will see the advantages/disadvantages of both ways. We will then use error handling and other finer things as well. Now, when the good guy is done, you will see the result as in Figure15:
Now, just for fun, press F5 to see what happens. Oops! The only thing that comes up is an empty form! What happened? Default, dear Brutus! The problem here is that Form1 is the default startup object that was set when you created the new project. Let's change that.
Click on the X of your beauty to close it. Alternatively, you could click Debug in the main menu, and choose Stop debugging. In either event, the form will close and get you back to the editor.
Go to the Solution Explorer and right-click Addresses as shown in Figure 16.
Click Properties. The next window is this (Figure17):
There's a drop-down box named Startup object. It shows you that Form1 is the startup object. Click this drop-down box and choose DataForm1 as your startup object (Figure18). Then click OK. You could now delete Form1. Since we will need another form later, leave it there for now.
Press F5 again, and see what happens - nothing! That's because you didn't build the project. First build a project, and then run it. When you click Build AddressBook, a message box will come up and ask you: "Do you want to stop debugging?" - Say "Yes, honey". Your "yes" won't get you married, but it will stop debugging so that you can run the program - isn't that something?
By the way, I'm in the habit of building my projects after all changes - before running them. Another habit is to have a backup of any project before I make major changes, just in case the system goes pfft. Of course, this includes the Access file. You might think now, "Oh, that guy is nuts!" No, I'm not - not entirely. If you have ever lost work that took hours and hours, then you know why I do this.
Fact is we haven't done anything special yet. If you like, change the startup position. DataForm1 is not the best title for an address book, so you might want to change the title to something more suitable - like Address Book - wow! I am sooooooooo creative today.
The form looks a bit messy - at least to me it does. To tidy this a bit, we need a larger form. Click any empty space on the form - if you find one - and make sure you don't click a button, textbox, or whatever. I usually click the title bar because there are no objects that could be clicked by accident.
Move your mouse over the handle on the right so that the cursor changes as in Figure19.
Click the left button of your mouse and hold it clicked while you drag the handle. Release the button of your mouse when the width is appropriate. Do the same thing with the lower handle to adjust the height. You can also adjust the size in the properties window. To do so, go to the Properties and find size. Click the + sign, and change the numbers. Play with the numbers to see the proportional changes. Later, you can make finer adjustments by dragging the handles and/or editing the size in the properties window.
Reorganize the objects of your form so that it looks a bit more organized. Have a look at my form (Figure20). When you change the position of the labels (the black writing before a textbox), make sure you don't mess around with the corresponding textboxes. To avoid this, drag over pairs together - the label and the textbox - with your mouse and keep the left button clicked. Then release the button and drag the items to their new place.
The first thing that will become clear is that the text in some of the labels isn't exactly finest English; like StreetAddress should be Street Address. That's easy to change; it works the same way as you would change the text of a button or your form. Click the label once. Then go to the properties window and change the text accordingly. Don't forget to do the same with the labels for the Zip Code, Phone Number, and E-mail.
There is another problem. Although you can enter longer text in the textboxes, the textbox for the street address might not display the entire address. Before you change this, you have to widen your form and move the following items to the right: Zip Code, State, Phone Number, E-mail, plus the corresponding textboxes and - of course - the buttons.
Now you can click one of the textboxes once and go to the properties window to find the size. Click the + sign and change the width from 100 to 200 - or whatever you think is suitable. The result could look something like this (Figure21):
If you like it that way, okay. Some people like to have everything the same size. There are two ways to do so; you can change the width of each textbox individually. That is, if you don't have anything better to do with your free time. Drag your mouse - with the left button clicked - over the textboxes on the left. Click the widest textbox once. Then click the item Make same width as shown in Figure22. The textboxes will now have the same size. If you like, do the same with the other textboxes. I know that there is a way to do it all at once. However, it sometimes doesn't work as predicted and would need additional explanations. I promise you, we will come to the finer things, but not in this project.
Right now, the address book could look something like this (Figure23):
Insert a button, and change the text to Close, or to anything else that would tell a user that this button closes the program. Rename the button, too. Something appropriate could be btnClose. But you can name it whatever you want, like btnthisisthebuttonthatclosestheprogramwhenyouclickit. It's a bit long though and of little use for quick identification. If you rename all your buttons like this, you'll spend more time renaming your buttons than coding a program. Drag the button to an appropriate place. Don't forget to code it so that it closes the program. By the way, you can use the code as we did it in the previous article:
Private Sub btnClose_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnClose.Click
You can do that quicker, too:
Private Sub btnClose_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnClose.Click
You see, many ways lead to Rome. You will later also learn their differences because there can be disadvantages depending on the circumstances. At this moment, you shouldn't experience any problems.
Have a look at what the (almost) final form could look like (Figure24).
You could now run the program and click Load to open the data access. The number in the ID-textbox is automatically generated. Some users - like me - can't resist typing into textboxes. That could be a problem. There are two good ways to avoid trouble with unwanted input. One way is to disable the textbox. This would also disable any additional functions, like displaying tool tips. You could make it a read-only text box, which has the same effect, but it would still display tool tips when the mouse moves over it.
Click the ID textbox once and go the properties window. Find the property ReadOnly and set it to true. If you want to disable the textbox, go to the item Enabled and change its property to false. Build and run the program. You will see that you now cannot write in the textbox any more.
When you make a program, you should also consider the color of your forms. However, before you change the color, you should carefully think about the effect it might have on other users. Neon green is most probably not the best choice for a commercial program. The default color is a bit dull, I know. The best rule to live by in programming is: "Never assume anything." Don't think a user will like the color just because you do. Murphy's Law applies to all consumers: "If there is a theoretical possibility that something might go wrong, be assured it will go wrong." With this in mind, please remember that you should be consistent with this. The color you apply to one form should usually be the color you apply to all forms. Of course, there may be exceptions.
To change the color, make sure your form is chosen by clicking on the title bar. Go to the properties window and find the property BackColor. Click once where it reads Control, then click the drop-down box to open the colors. You have three choices: Custom, Web, and System. Most system colors are neutral, so let's forget them for a while. The custom colors are in fact standard color. Have a look at Web; there you will find very interesting colors, too. Just click any color and you will see the changes almost immediately. Your form has now a different color, including the buttons. The textboxes have stayed white, with the exception of the read-only textbox. If you want to change the color of the textbox, too, do the following: click the textbox once and go to the properties to change the item BackColor. Look at what the program might look like now (Figure25):
What we have done today should be enough. The break here will also give you the opportunity to review part 1. Our project is not finished yet, so don't delete it.
NB. If you download and run the demo solution attached, you will need to change the connection string so that it points to where the Access database file is on your computer, otherwise you will get an error. The error message is a textbox that will tell you that the file could not be found. So let's look at how the connection can be changed.
Click the OleDbConnection1 once as shown in Figure26.
Go to the properties window and click the property for the connection string. Click the drop-down box and choose <New Connection ...>. From then on, just follow the steps already described in this article. To start with the steps, click HERE
Related devCity.NET articles: