Beginners often need "bootstrap" examples to get them started. I've written dozens of these simple apps to provide this kind of sample code over the years. This page may provide a place to post those that seem most useful.
Recently I've received several requests for help with a few Delphi techniques that are not too difficult once you do one. Here is the current offerings:
Features and techniques illustrated are used in a number of programs on DFF, but it's easy to lose the essential characteristics in programs that may have 1,000 or more lines of code. Each of these programs contains less than 100 lines.
Scroll down to see all offerings.
The Image program is a "Parking Lot Simulation" that draws some
parking spaces on the canvas of a TImage control. Clicks on the
spaces then show or hide automobile pictures in the spaces. A TImage
has the advantage over a TPaintBox because it can refresh itself when
necessary. Once the canvas has been drawn or modified, the image will be
managed by TImage redrawn without our help if the image is moved or minimized or
has other forms popup over it. This is not the case with
a TPaintBox where it is the programmer's job to make sure that it knows
how to repaint itself at any time.
Click here to download Parking Lot Simulation source code
TChart is a powerful graphing and charting facility distributed with
most versions of Delphi. It is so powerful that the hundreds of
options can be rather intimidating for the beginner. Fortunately most
features have reasonable defaults and need not be changed. Most of the
rest can be defined a design time in a TChart Edit component that is accessible
by a right click or double click on the TChart control image on the form. Any of the
properties can also be defined at run-time. In this demo, TChartDemo1,
line plots of
two functions are drawn and some of the titles etc. are set via code just to
illustrate the technique.
Addendum December 2, 2005: A viewer recently asked about getting user input data from a list box or memo into a chart. Here's TChartDemo2 which shows a few additional techniques for charting user data. It includes a "missing" Delphi function, StrToFloatDef. which tries to convert an input string to a floating point number and returns either the numeric value of the string or a default value if the input string does not represent a valid number.
Click here to download TChart Demos source code
Four
cells in a 4 x 4 array are randomly loaded with the letters 'A', 'B', 'C', and
'D'. Four additional cells are loaded with the letter ''X' - these are
blocked cells and may not be occupied. The other 8 cells may be passed through
while picking up the A, B, C, D cells. We need to find a path from
'S' through all of the A, B, C, D cells by moving horizontally and
vertically and without visiting any square twice.
Click here to download Backtracking Demo source code
Addendum June 8, 2005: Here are three more additions:
A simple program to draw
two random dice each time a button is clicked.
Click Here to download DrawDice source
How do you count words in a text document? Like loving a porcupine - very carefully. Here's simple start though, with a Getword routine which looks for a predefined set of delimiters marking the ends of words. Words from a text file are extracted and listed. There is also a Summarize button that accumulates number of occurrences by word.
Click here to download CountWords source code.
Find all solutions where the sum of two numbers equals 864. And by the way, the
solution, including the "864" sum must contain all of the digits 1 through 9 exactly
once. Other sums to try: 594, 783, 675, 927. You can also
check Brute Force for another
mode generalized approach to solving the problem. Expressions864.prb is
now included among the sample problems.
Click here to download Expressions864 source code.
Addendum September 30, 2005
Given a letter string, find all possible subsets of letters ("words") that can be formed. Optionally, list only words that can validated against a given dictionary. This code is fairly simple because it uses two other pre-built units as helper "tools". UComboV2 contains the class which generates the permutations which help us form the words. UDict contains the dictionary class that lets us validate our trial words.
Click here to download AllWords source
Click here to download AllWords executable
Even though the use of records and pointers is less critical today than in the pre-object days, some schools still teach their use.
And the use of record pointers in the objects field of TStringlists is
sometimes convenient.
The cardinal rule when allocating memory within your program is to make sure that it gets released. One common way to create a
memory leak is to allocate a record using the New function and not
releasing the memory (using Dispose).
Students are sometimes confused by the fact that local variables within functions and procedure are automatically released by
Delphi when you exit the routine. This applies to all of the memory that Delphi
allocated on your behalf, but not to the memory you allocated. If
you have local variable, P, and execute New(P) within the routine, Delphi will delete the 4 bytes occupied by P, but not the memory
that P points to! And if not deleted before you exit, that memory
will remain allocated until the program ends.
Here's a program that allocates 1000 byte records within a procedure with and without releasing them just to illustrate the
problem. It uses the AllocMemSize global variable to show
increasing memory usage as the program runs.
Click here to download MemLeakDemo source
Addendum February 15, 2006
Every
two years. "Olympic Rings" leads the list of search phrases at
DFF. Drawing the rings in Delphi is an interesting drawing exercise,
especially if we want them to appear to be interlaced.
Click here to download Draw Olympic Rings source
Click here to download Draw Olympic Rings executable
Addendum March 12, 2006
We have a decryption program elsewhere at DFF which checks all possible substitution ciphers and check results against a dictionary to determine success. The problem is, this approach can run for hours, and if the text contains words not in the dictionary, may not succeed at all. My Mensa "Puzzle-A-Day" calendar periodically contains messages encrypted with a simple "shift cipher", also known as the "Caesar Cipher" because Julius Caesar used it to encrypt message in ancient Roman times. In the shift cipher, each letter is replaced by a letter at some fixed distance away in the normal "abcd..." listing of letters. For example, if all plain text "a"s were replaced by "c"s, then "b"s, becomes "d"s, "c"s becomes "e"s etc. to produce the encrypted text.
It takes less than 40 lines of user written Delphi code to scan lines of input text using all 25 possible encryption choices (26 if you count the one that maps each letter back to itself). You provide the dictionary check scanning the outputs to determine which one is correct. And, of course it will encrypt as well as it will decrypt!
Click here to download Simple Decrypt source
Click here to download Simple Decrypt executable
| Created: March 11, 2005 |
Modified: November 07, 2008 |