Thursday, January 26, 2012

From The Beginning: Decision Making - If Then

This post is incomplete: My PC is currently useless so I may have to leave it as is for a while. It will be finished.

The from the beginning posts will focus on making the early chapters of the "Enjoy Delphi" book easier to understand, I will be starting with Chapter four, which is decision making. This includes if - then - else statements, Case Statements, Nested If Statements, Checkboxes, Radiogroups and the Timer.

In a program with no decision making, where the same event always occurs when the program runs regardless of what the input is, every line of code for an event that is triggered will be used. An If-Then statement allows you to choose which sections of the code are run, if a certain piece of input does not match the if part of your statement, then the rest will not run.

Here is an example of a piece of Code that will show what grade a person got based on their marks, and the form:


Tuesday, October 25, 2011

Encryption Program: #3 in Notes

I have been asked to put the homework program for shifting every character 1 up here for people to look at the code.

The code is quite simple, the only new idea is using Inc with characters, exactly as you would for integers.


I will add explanations later, I hope this helps.

Monday, September 5, 2011

Components Guide: Basic Input Components.

Often in a program you cannot simply use edits and buttons as your only forms of input. There are many cases where you need the user to make a choice and specify items using only the mouse. You may also need the user to give a logical (Boolean, colour etc.) or numerical (Integer, Real etc.) as opposed to text (String, Char) input as conveniently as possible. Maybe you just want to add flare to your program. To do all this, you need to use more advanced components, and have a better understanding of the basic ones. The best way to figure these components out is to experiment and practice yourself, but this guide will hopefully give you a good foot to start that experimenting on.

All the pictures show the component on the form during editing, and then the component on an active form using XP Manifest (a Component that sets your form to an XP / Vista / 7 theme) on Windows 7.
Here are the input components, what they do, their names and how to use them:

Button. btn.
Button: btn
The button is the most basic form of input. To write code for it simply double click the button on the form. This will create the OnClick Procedure and your code will execute when the button is clicked on.
The button is used as a form of confirmation, the user activates an event with the button when they wish to start a new part of the program.
Other events that can be useful for the Button are the OnMouseDown and OnMouseUp events, this is the clicking and releasing of the left mouse button.
You can change the title of the button with the "Caption" Property, putting the "&" symbol before a letter in the Caption will make that letter with the <Alt> key a shortcut for that button, activating the OnClick event without the user needing their mouse.
=====================================================================================
BitButton. bmb.
BitMapButton: bmb
The BitButton / BitMapButton is very similar to the plain button. It uses the same events and properties as a simple button, can use a shortcut letter the same way (for example pressing <Alt> + A would trigger the code in the standard OnClick event for this BitButton).
What a BitButton does have that a plain button doesn't, is a glyph. A glyph is the Delphi term for a BitMap image. BitMap images do not resize well, but they are supported by most programs. You can change the image by selecting the "Kind" property (Warning, choosing the close kind adds code to your button to close the form when the button is clicked) or by loading your own. To do this select the three dots at the end of the "Glyph" property and navigate to any BitMap image you have.

=====================================================================================
SpeedButton. sbn.
SpeedButton: unsure of the proper name, I personally use: sbn
The SpeedButton works much the same as the BitButton and Simple Button, however it has two extra properties: The Down and Flat properties. When they are both set to False, it will look like a normal BitButton. When Flat is set to True, it looks like the ones in the pictures on the left, Transparent. The down property makes the button automatically look pressed down, to use this property you need to change the "AllowAllUp" property to True and the "GroupIndex" property to a number bigger than 1. Play around with these settings to get a button that stays pressed in or can be pressed in and out like a checkbox.

=====================================================================================
Edit. edt.
Edit: edt
The edit is used for the entering of text. It can also be used as an output component with the "ReadOnly" property set to True, but a label is better suited to this. What you see in the Edit is the "Text" property, this is a string. If ever you need to have a number entered, remember to use the StrToInt or StrToFloat code to change  the Text to a number that Delphi can work with. The most useful event for the edit is the "OnChange" event, which allows you to execute code every time the text in the edit changes.
You can modify the way an edit displays information using the "PasswordChar" and "CharCase" properties.
=====================================================================================
updown. udn.
UpDown: unsure of the proper name, I personally use: udn
The updown is useless as a component on its own, but when attached to another component, such as an edit or label, it becomes useful. The updown requires no code, you simply place it on the form and then choose the object it must affect using the "Associate" property. For example, you can associate it to an edit to make it a type of spin edit. The text is still a string through. You can change the interval that each press gives ( +- whatever the interval is), what the lowest and highest values are and what number the updown starts at. Set any edits to read only if you want to make sure that the user only uses the updown.
=====================================================================================

This will be continued with Advanced Input Components where I will discuss the MaskEdit, ComboBox, RadioGroup and CheckBox. I will also try to do two sections on output components as well as a section on form design components.
The best way to learn about these components is to try and use them yourselves, experiment and remember that most components that use the same logic or idea, use the same code, allowing you to leanr many extra components without touching new code.

Thank-you for reading this, I hope it helps. If you have any advice, need help or anything else I can do via this blog, please say so in the comments.

Wednesday, August 31, 2011

Neat Coding

Green blocks show indentation, the red lines show skeleton code that
Delphi creates for you when you start coding for a new procedure in the
event handler.
When you are coding, particularly if your program is complicated or makes use of many if and case statements, it is vital that you are able to read your code quickly and easily. Understanding what you are typing through neat coding means that you can code faster and that others will be able to help you if there is a mistake in what you have typed.

The most important thing to remember when keeping your code neat, is proper indentation (<Tab> key). The green on the picture to the left shows indentation. It is easiest to understand if the only text that touches the grey line is the skeleton code that Delphi gives you, the Procedure, Begin and End code.

Assigning of values to you variables and all basic processing and output should be indented once, and everything that goes below the first line of an if or case statement should be indented once more. The begins and ends of an if statement should line up, as should the then and else sections, as shown below.
The next way to make your code easy to understand is to use comments (//) and line-breaks (<Enter> key). Put comments above each different section of your code and separate these sections with line-breaks: The assigning of values to variables, resetting everything on the form, initialising variables, your processing and your outputting. Also place comments above each piece of more complex processing, such as case statements. It can also be helpful to place a comment on the same line as each begin and end (after the end, use "//" and say what piece of code the end is for).

If statement where begin and end are indented to the same degree, as well as then and else indented to the
same degree. This makes it easier to follow normal and nested if statements.

Another aspect to neat code is spacing, capital letters variable naming. Look at the if statement above, between the operator and the variables (iSecondsPassed  >  iSeconds) there is spacing, this makes it easier to read. Between the variable and the divide sign, there is another space. This again makes it easier to read, particularly if you have a long mathematical equation that you are using.
Capital letters help keep your code legible when you have a variable or component with multiple words in the name, putting a capital letter at the beginning of each word makes it that little bit easier to read. Also when converting data types, Such as with StrToInt or FloatToStrF it is easier to read and see what you are converting if there are capital letters.
When naming variables, use your capital letters, and give them names that make sense to you. Give the variables names that describe their function and what is held by them. Use a small letter in front of the variable to denote what type of variable it is. For example put a small r in front of a Real variable or an s in front of a string.

Finally, if you have many extra numbers in your code, such as 30 for the number of seconds or 0.14 for VAT, don't simply leave these as numbers in your code, as they will seem arbitrary if you need to look at the code later. Declare these as constants between Procedure and Begin and give them values there.

Thank-you for reading and remember that neat code is the only code that you can understand more than a month later.

Monday, August 29, 2011

Naming Your Components

A structure tree showing organised
components.
This is a point that cannot be emphasised enough. In order to understand your own code and form layout, you must have a good idea of what everything is, where it is and what it does. 
The first step to making your code easier is using the basic naming convention in which each component gets a 3 letter name followed by a description. For instance Label = lbl; edit = edt; panel = pnl; checkbox = cbx; radiogroup = rgp; button = btn; timer = tmr; bitMapButton = bmb et cetera.
The description will normally be along the lines of input, output or explaining which input or output where necessary.
This alone makes it easier for you to code and for someone else to help you if you are unable to spot your error.

To truly make a difference in a complex program with many components though, you naming system needs more than just a basic description and 3-letter code, in a complex program it is often useful to know where the component is and to group components accordingly, this is made easier using components like panels and groupboxes, which will add a "branch" to your structure tree (you can see a structure tree on the left, each new colour shows a new level where it "branches" off). You can also simplify what you look at in your structure tree if you have panels and groupboxes by minimising the panel or groupbox. You can see on the left how all the components in panels under tabsheet 2 (2 {tbs2}) are hidden and how all the panels and their components are hidden in tabsheet 3 (3{tbs3}).

As previously stated, sometimes the component description will need more than "input" or "output". Often you will have multiple labels or edits that all serve different functions, and it is useful to give them names that describe exactly what these functions are. If you look at the labels on the left for frmMathLogic -> pgctrl -> 1{tbs1} -> pnl_pg1_Results you see that there are two result labels, One for the number entered and one for whether or not the number is real, differentiating between the two in the name of the label allows you to code faster and more easily as you will not get confused and thus not have to check your form.

It is also important to make your code easy to read and recognise, meaning that your component names have to be easy to read and recognise, stick to one convention for all components of a certain type or location in all your forms ( for example, don't use "output" in one program and then "result" in the next ), and use capital letters and underscore signs ( _ ) where you feel necessary.

When you combine proper naming of components with other useful tips and functions in and for Delphi, you can code a lot faster and more easily. You can understand your code at a glance and can get help if you go wrong as anyone can understand your code if it is neat and self-explanatory.

Thank-you for reading and I hope this proves helpful.


Sunday, August 28, 2011

A Few More Things

After that rather odd introduction, I just need to add a few more things:

-1: Sorry about the background pushing around the edges of the page, if that is happening to you. I tried, Blogger even said 1800p would be wide enough... ah well.
-2: I will not necessarily be posting to a rigid schedule, if any at all. I will post if it is requested, when I feel I need to, and when I am bored.
-3: If I offend anyone in any way, sorry, but you do choose to read this, and I will not necessarily edit my post or pull back my comment. 
-4: Yes, it is Blogger. What? Don't look at me like that. I am honestly just too lazy to even go find anything else. And Blogger is convenient, I don't have to know any more than <img></img> to get what I want out of it. 

Thank-you, and I hope this helps to at the very least make your programs / sense of humour shinier.

Wow... Finally... The Introduction

As far as things that are overdue are concerned, this post serves as a role-model to the rebellious masses that have terrible role-models. I think I began designing this thing weeks ago. First there was that design... you know... the dark, horrifically contrasting, eye-bleedingly annoying to look at... thing that had a colour scheme worse than clown puke. Then eventually it got to this, not great, but you can read it without your head spontaneously exploding.

Now that that is out of the way, hello. I guess I had to get to, you know, the introduction some time in the introductory post...
So here it is, Delphi Newbie, probably 1 of the only 3 comprehensible blog names not yet devoured by the masses of people using this thing. The idea behind this, if it doesn't fail horrifically and lead to my dead body hanging limply from a broken window 5 stories up, is to help some people in my IT class with Delphi coding. I also intend to throw a little bit of Image editing (limited knowledge is still something useful...) to those willing to read it.

If you want details, fine, I am Brandon, not that the bar at the bottom of the blog wouldn't tell you that. If you want to criticise, be constructive about it please, if you have anything to add, I will listen and may include it in the posts.

So thank-you all, hope this doesn't lead to my bloody end, enjoy and name your components.