The Skip Days program
There are times when you need to know exactly what day is 30 days, 90 days or 180 days into the future. When you do, you have to find a calendar and start counting days. It's easy to make a mistake. The program we'll write will show you the exact day with one click of a button.
What are the goals?
- Understanding what a "Windows Forms Application" is
- Learning how to use Visual Studio
- Using a Visual Basic control
- Understanding "event oriented" programming
- Writing the program code
Understanding what a "Windows Forms Application" is
You can write any kind of program using VB.NET. Some programs are designed to work on the web. Others work "behind the scenes" and you don't see them at all. The next lesson discusses how to create these other types of programs. A "Windows Forms Application" is the kind of program that is most familiar to you. The program works using a window on a screen and familiar "objects" like buttons and textboxes.
VB.NET programs consist of "programming statements" that you can understand. Here are two statements as an example:
Dim myVariable As String
myVariable = "A String"
After you have written your part of the program, VB.NET merges your part and the part created automatically and then "compiles" the program. The word "compile" means that it is converted from statements you can read into statements that are designed for the computer to read. As we learned earlier, computers only understand 1's and 0's. Compiling a program is the first step in turning it into 1's and 0's.
The first statement above reserved a name, "myVariable" so you can save information and refer to it using that name. There are specific "types" of information that you have to use when you write a program and "String" is one of them. Another example is "Decimal". The second statement associates specific information with the name you reserved. If you code a later statement:
Debug.WriteLine(myVariable)
VB.NET will display
A String in the Immediate Window because that's what the name myVariable refers to.
Learning how to use Visual Studio
The statements in your part of the program are kept in a file that is similar to a file you might make using Notepad. But VB.NET keeps everything organized for you and you can see it all in the Solution Explorer part of VB.NET. The illustration you saw earlier shows you where Solution Explorer is in VB.NET. VB.NET uses the word "Solution" instead of "Program" because a lot of programs can be combined together into a bigger program and they needed a different word.
When you write a Windows Forms Application, you will select and customize the visual parts of your program (the form and things like buttons and textboxes) using the Design window that you have already seen. These parts of the program are already there for you to use in the VB.NET Toolbox and they're called "components" or "controls". (A "control" is a "component" that has a visible interface like a Button. A "component" doesn't have any visible interface like a Timer.) You can see the Toolbox lurking over on the left side in a previous illustration. Because you only need the Toolbox when you're adding controls to your application, it snaps out of the way when you're not using it. If you leave the mouse pointer over the Toolbox, it will snap out. You can then click and drag controls onto your form. When you do that, VB.NET adds the programming for the control to your Solution.
(Just a note of history here. This innovation of adding controls to a program by dragging them from a Toolbox was the thing that made the very first version of Visual Basic such a hit because, for the first time, programmers didn't have to write the detailed code to create the controls themselves.)
We're going to jump right in and add all of the controls that our SkipDays program will need right now. This may be confusing if it's your first time and you may need to do it more than once to get it right. If you get totally confused, close VB.NET and "Discard" the program and start over. No problem. You're here to learn.
- Make sure that the Design Window is displayed
- Open the Toolbox and find the MonthCalendar control
- Left-click and drag the MonthCalendar control to the form and release the mouse button to drop it on the form.
- Find the Button control. Drag and drop it on the form.
- Find the GroupBox control. Drag and drop it on the form.
- Find the RadioButton control. Drag and drop it on the GroupBox control. Drag and drop two more RadioButton controls onto the GroupBox control.
That's all we'll need. Here's the way my program looks after doing that. Notice that I didn't bother to arrange the controls neatly (we'll do that next). I just dropped them anywhere on the form (except for the RadioButton controls - they have to be dropped on the Groupbox). The MonthCalendar control isn't even fully on the form. Even though you haven't written a line of code yet, the program still runs. You can click F5 again and see. (I show mine running in the lower right corner.)
--------
Click Here to display the illustration
Click the Back button on your browser to return
--------
Here's where you put your "designer" hat on. Arrange all the controls so they look good on the form.
You can move all of the controls around on the form after selecting them (by clicking inside them). A control is "selected" when you can see the "sizing handles" around the edge. (The MonthCalendar control only has three. The RadioButton controls only have one.) You can drag a control when the mouse pointer turns into a four-way arrow. Visual Studio helps by snappping them in place when they're lined up with other controls or the form. Notice that when you move the GroupBox control, all of the RadioButton controls move with it. That's why you dropped them on the GroupBox control. Adjust the RadioButton controls inside the GroupBox but make sure you don't move them outside or they won't work the way we want them to.
Here's my form after I arranged all the controls. Yours doesn't have to look just like mine. You can design it any way you like.
--------
Click Here to display the illustration
Click the Back button on your browser to return
--------
