Programming in Any Language

Password Keeper

Password Keeper is an application that keeps passwords for you. Basically, via an add function, the program allows you to enter a company or web site name along with your login ID and password. It then stores that information on disk. PDFs: Slides, Code

Click to enlarge

Password Keeper is an application that keeps passwords for you. Basically, via an add function, the program allows you to enter a company or web site name along with your login ID and password. It then stores that information on disk. It also has a search mechanism that lets you locate the web site or company you’re interested in and returns your login ID and password. The delete function lets to totally remove a record. The Edit function lets you make changes to the company name, login ID and password. And you could easily add other fields to this application if you are interested.

To make this simple there is no security on this application. We can implement that after we’ve covered it. For now, this is just a useful application that will let you gain experience with panels, arrays, list boxes, binary search, sorting and more. We’ll cover all that in a series of steps, the first one being Analysis.

Form Design comes next. The more work you did in analysis, the more complete your form is going to look. This does not have to be perfect. As you code you can make all the form changes you like. A good form design, however, will minimize the number of changes you will have to make. (PDF: Design of the form and 4 panels)

When this application is first used there is no text file. Later there will be. Form_Load next to be aware of this and handle it. And so does the exit button. This application uses a tab delimited text file. Every time the program is used there could be a change to the data. The array will need to be saved every time the program is exited.

The Add button takes data from the text boxes and stores the data in the array. But the user can cancel the Add function if desired.

Edit, Delete and just a simple Search all rely on the records being in sequence. And Search will used to find the record that needs to be deleted or edited. It makes sense to make sort a part of the Search operation.

We originally thought we would need to search in order to find the record we needed to delete. In effect all we needed to do was save the pointer in the normal search function and make it available to both delete and edit. Delete is actually pretty easy.

When we go into Edit, we simply populate the text boxes on the form with three pieces of data from the three arrays. Just like delete, where we place the last record into the open spot in the array, edit replaces the new data into the same array location. No problem if the data is not in alphabetical order, the next time Search is used the data is automatically sorted to its proper order.

It doesn’t take too much to pretty-up an application and make it attractive to the user. Usually, a nice graphic or a touch of color does the trick nicely. You don’t want to go overboard on this, but you do want the application to sort of standout pleasantly.

As a part of Cosmetics, we are going to add one more panel to the form. This panel will do nothing more than hold a graphic of a small safe. Since this application holds password information a safe makes a nice cover for our application. And that’s how it will function. When the program is not in use the graphic will be the focal point. While the application is in use the graphic will disappear and not get in the way.

Programming, needs to be done logically. Think of your buttons - the Add, Search, Exit, and the others - as separate programs. In a sense they are. Each button is charged with doing a specific task. The first things we’ll code are Form_Load and Exit button.


Tic-Tac-Toe Simulator

This Tic-Tac-Toe Simulator is an excellent exercise in handling a 2-dimensional array. We have a set of nested loops that populate a Tic-Tac-Toe board with X’s and O’s. We do that by examining the contents of each array element separately and applying either an X or an O to a matching label on a form. We also use Booleans to effectively interrogate the contents of the labels holding the X’s and O’s to see if we have any three in a row. This is not like the regular game here. We can have a tie! We can even have a board of all X’s.

Naturally we’ll use methods to make efficient use of our code. This simulator is broken down into five parts.

Click to enlarge

Part 1 introduces the application. It analyzes what we are about to build

Part 2 shows us how to design the form. The Tic-Tac-Toe Simulator form consists of a set of nine labels that will directly correspond to a 2-dimensional array in code. We’ll end up naming these labels with names that use the array coordinates as part of the name. This makes it tons easier when referring to the labels in code.

In Part 3 we code the New Game button. Part of that programming will consist of calls to methods that will do quite a bit of the work. We’ll also add a few things to the global area. Up there we will establish the Boolean variables and the integer array.

The key piece that will be coded here is the evaluation of the Boolean values. Either X will win, O will win, or we’ll have a tie. The tie could be that both X and O win, or no one wins. Not exactly your standard tic-tac-toe game.

Many new programmers have a hard time dealing with Boolean values. This technique should clear it up for anyone who is having that difficulty.

Part 4 has us code the GenerateGame method. GenerateGame is the first method we will tackle. Here we use a set of nested loops and a random number generator to place either a 0 or 1 in each element of the three-by-three array.

Next, we take each of the elements in the array, check to see if the value is a zero or one and based on that value place an X or a Y in a label that corresponds to the array element. This is where the name of our labels makes a big difference.

In Part 5 we code our final method. This one would be used in the standard game as well. Here we check each column, each row, and both diagonals for the existence of three X’s. We all check each column, each row, and both diagonals for the existence of three O’s. If we find either, we set the corresponding Boolean to true. That is really all we have to do. The code to determine a winner was already coded back in the New Game button.

Caution is advised here, however. It’s easy to slip up and check the wrong label. Easy to do.

Once you are done it’s a matter of testing the demo for all four possible conditions. PDFs: Slides, Code


Building a 4-D Array App

If you watched the “Multi-Dimensional Arrays” lesson, you have a pretty good idea how 3-D and 4-D arrays can be put to actual use. The lesson was simplified to an application that displays data in three rows and four columns. Looking at that, you could easily visualize a budget screen showing 30 rows and 12 columns of all kinds of assets, income, and expenses across a twelve-month period.

In Part 3 the year’s component is added. This makes it a functioning 3-D array application. There are a lot of minor changes to make in this program. Wherever the 2-D array was used now has to be modified to work with the 3-D array.

Part 4 is the creation of a 4-D array. Companies are added. Adding the fourth dimension to the array is very similar to adding the third dimension. It’s just another level of complexity to get the company piece working. But once it’s complete you have access to all the data for each company, for all ten years easily displayed on the 3 by 4 grid. PDF: Code

Of course that would make the app more complicated. And that’s the problem with multi-dimensional arrays. They can get out of hand easily. And that lesson explained how to tackle the multi-dimensional array one step at a time to eliminate much of the confusion.

This application replicates that lesson by stepping you through the tracking of Assets, Liabilities and Equity for a given company. That’s the 2-D array. It becomes a 3-D array when we add a year’s component to the app. We can now look at Assets, Liabilities and Equity for any year. To build a 4-D array we track the Assets, Liabilities and Equity for any given year for any number of companies.

Click to enlarge

Pay attention to the hierarchy chart. It’s important that most of the methods and events are used to build the initial application. Adding 3-D and 4-D becomes a relatively easy process.

In Part 1 the form is created, and the functionality of the 2-D array is created.

Part 2 focuses on saving and loading the data to and from the array. It’s still a 2-D array.


The List Application

If we look at the top-down design of this application, you’ll notice that it is a little more complex. Search has been added and there is also the addition of navigation buttons. Much of the way this application works, however, is very similar to the list box version.

Parts 2, 3 and 4 build this application. What’s interesting is how search is implemented. Turns out it’s a very simple addition. Navigation is also straightforward and is very similar to how actual databases are navigated. In fact, back in my consulting days, I used this same navigation technique with virtually all of my clients.

We’re going to program the navigation buttons first. It’s really a matter of just moving a pointer along the list. If the list index is zero, we are at the beginning of the file. Add one to the pointer and we’ve moved forward along the file. Subtract 1 from the list and we are moving backward. If we need to go directly to the end of the list, we just have to go one less from the list record count.

Now we don’t want to go beyond the bounds of the list. Less than zero or greater than the number of records would cause our program to blow up. It wouldn’t be pretty. But a simple “IF” statement can avoid that problem.

You will also find that every time we launch the program, we have to re-enter the data. Well, that’s no fun. So, it’s important to code the save and load routines.

In Part 3 the Clear button gets updated.

Delete is the process of simply removing the current record.

When working with edit, the Search function is necessary, especially if you have thousands of records. You don’t want to navigate, one by one, through a long list just to edit one record. Much better to Search for the record.

And once you have activated the record you wish to edit, it is a matter of deleting the current record in the position it is in and inserting its replacement. Really two steps. Smoke and mirrors. Editing is really deleting and adding a new one back in.

Part 4 covers how we make the application presentable to the user. Most of the buttons on the form cannot be used until there are actual records in the List. For example, if the user presses Edit before we load any records the application will blow up. If the user clears the List Edit and the navigation buttons should not be available to the user. How we turn these specific buttons on and off is shown to you here.

Communication with the user is also strengthened here. Every action performed by the user needs acknowledgment from the application that the action was taken. You delete a record the application needs to tell the user that the record has been deleted. We, as programmers, know this has happened, but the user may not. Communication is extremely important.


Compared to the List Box version of The Anything Database (see Speed Track) this List version looks cleaner. The list box is gone and it is replaced by a series of navigation buttons. The data entry area is still there. And all the usual buttons are present with the addition of the Search button. You also might notice that in addition to the record count there is also an indicator that displays the index of the current active record.