C++ Builder Tutorials

These tutorials assume that you know your way around C++ Builder 4 Pro and that you own the software in some form or fashion. The tutorials are not designed for absolute beginners, so if that is what you are, I reccomend that you go somewhere else!!! They do not assume that you have any knowledge of programming in any language, but if you have then that will obviously be useful, I will try to explain the code as much as possible as we go along.

Tutorials Index
Currently, we have 1 Tutorial(s)

Notepad-Style Text Editor

All Of The Application Code Contained herein is derrived from code that I have written before and therefore is freeware, you may use this code in any size, shape or form of an application, but all that I ask is that you give me some credit such as putting my name at the bottom of your Readme.txt file or something like that! Thankyou.

QuickPad - Notepad Style Text Editor

First of all, create a new blank application with a single form. Now insert a "MainMenu" component, a "Status Bar", a "RichEdit" control, a "SaveDialog", a "OpenDialog" and a System Timer object. Now we have to change some property values! Click on the richedit control and in the object inspector, go to the alignment property and change it to alClient, the richedit control will now fill the form, whatever size the form is resized to. Now change the Plaintext property to true and the ScrollBars Property to SSVertical, this forces scrollbars to appear. Now double-click on the MainMenu component, a menu editor window will appear. Click on the first menu space and insert a File menu by changing the caption to &File, the ampersand inserts an underscore under the letter it precedes (F), this now allows you to call up the menu by pressing Alt+F, now add a New, an Open, a Save, then add a dash (-) which inserts a break and then a Quit and remember to add in an ampersand before each of these words except for the dash of course! :-) Now, following the same rules add an Edit menu containing Cut, Copy, Paste a break and then a Clear All, that's all for the menu editor for now, so close it up.

Now from the form that looks like it's taking shape, click on the filemenu that has just been created and then click on quit. And now insert this piece of code, you can cut and paste if you want:-

Note: Remember in all code that all Capitals and Lower-Case letters MUST be in the correct places otherwise the code will not work! (Don't ask why!)

int GetButton = Application->MessageBox("Are you sure that you want to quit?","Quit?",MB_YESNO + MB_DEFBUTTON2 + MB_ICONEXCLAMATION);
if (GetButton == IDYES)
{
Application->Terminate();
}
else
{}

Once you actually run the application, you will get the idea of which piece of code does but for now, here is a nice list!

int - This specifies that the result will be an integer value (a whole number) such as True or False (0 or 1) respectively an integer in programming can also be a value which sets a property such as a colour name or a result to a question.

GetButton - This is the integer value specified that gets the result of the messagebox that appears before the application will actually let you quit.

MB_YESNO - Specifies that the messagebox should have two buttons - YES and NO

MB_DEFBUTTON2 - Specifies that the default selected button should be number two, No in this case.

MB_ICONEXCLAMATION - Specifies that the icon displayed in the box should be that of a Question mark to symbolise that a question is being asked.

The above 3 values were integers.

Now we will make the function that will open a text document for you, we have to change some more properties here! So click on the open-dialog and double-click on the filter section in the object inspector, we want this application to open and edit plain-text files so on the left of the table, in the first space add, "Plain Text Files (*.txt)" and then in the space on the right of that, enter "*.txt" and click on OK. Now click on the filemenu on the application you are creating and click on open, this will bring up the code window enter in here:-

if (OpenDialog1->Execute())
{
RichEdit1->Lines->LoadFromFile(OpenDialog1->FileName);
Form1->Caption = ("CrayZDog Notepad");
Form1->Caption=(Form1->Caption + " - ");
Form1->Caption=(Form1->Caption + OpenDialog1->FileName);
}

This code brings up an opendialog, and once you have chosen a file, it takes the output from filename to load the file into the richedit control, it then reads the filename again and adds it to the caption of the window, simple, yet proffesional looking! Once you have done this and run the program, you should be able to load any plain text files such as readme.txt etc. You can even go into C:\WINDOWS on most machines and load up something like license.txt.

Now we will write the code that saves files and again, we have to change a view properties.
So Click on the savedialog1 box and change the filter to the same as before, change DefaultExt to txt and FileName to *.txt (Just to make it a bit more proffesional, but if you don't change DefaultExt, you won't have FileName.txt but instead: FileName with no extension.

Now you need to add in this code by clicking File->Save:-

if (SaveDialog1->Execute())
{
RichEdit1->Lines->SaveToFile(SaveDialog1->FileName);
Form1->Caption = ("CrayZDog Notepad");
Form1->Caption=(Form1->Caption + " - ");
Form1->Caption=(Form1->Caption + SaveDialog1->FileName);
}

Now we can create the code which makes a new file, so here it is(No properties here!! :-) ):-

int GetButton = Application->MessageBox("Do you reall wish to Start on a new file?","New?",MB_YESNO + MB_ICONQUESTION);
if (GetButton == IDYES)
{
RichEdit1->Clear();
Form1->Caption=("CrayZDog Notepad");
}
else
{

}

We have now finished the file menu, so now we go onto the edit menu!

So first click Edit->Cut:-

RichEdit1->CutToClipboard();

Now click Edit->Copy:-

RichEdit1->CopyToClipboard();

Now Click on Edit->Paste:-

RichEdit1->PasteFromClipboard();

Now Click On Clear:-

RichEdit1->Clear();

 

Now we just have to change a few minor details to make the application complete!

Firstly, Using the object inspector's drop-down menu, goto Form1 and change the caption to CrayZDog Notepad or to whatever you changed it to in the code, now go to RichEdit in the object inspector and Double-Click the Lines property and Remove all of the text.

Now click on the timer object and click on interval, change this to about 250 milliseconds, now double-click the timer and add in the code:-

StatusBar1->SimpleText = (Application->Hint);
For this to work, you must change the StatusBar's SimplePanel property to true and then you must add hint properties to each control, once done, the application will display the hint at the bottom of the window in the panel!

You now have written (Sort of!) a useful small text editor, possibly in a smaller file-size than Microsoft's attempt. So it has a few less features, we'll sort that out someday!!!

Below, you can download the source code that I have written and the actual executable!

 

To Use These Files You Will Need an Archiving application such as ZipCentral or Winzip, Zipcentral is a free alternative to winzip that works in almost the same way and reads and writes the same files!

CrayZDog Notepad
CrayZDog Notepad (Src + Bin)