Friday, 16 August 2013
You can now develop software for both iOS and Android
I've just read on the Embarcadero blog that you can take an iOS app developed in Delphi and retarget it to work as an Android app. Delphi for Android is still in beta, but this is a significant step to developing software in a single language and deploying it multiple platforms, and importantly the software run natively. If this works as well as they give the impression, Delphi will have solved one of the biggest issues with software development, that is employing different developers to develop similar software for different platforms. Because of this Delphi must surely become more popular.
Thursday, 18 July 2013
How NOT to handle exceptions
Just been working with some code and saw this little gem.
try
{Some code}
except
showmessage('error');
end;
This is a good example of the developer being lazy and not being bothered to do something better. Can you imagine the user ringing up support and saying they are getting a 'error' message, it's not even with a capital 'E'.
There are a couple of issues I have with this bit of code. Firstly I have a rule that showmessage() is used while developing and should be removed when a deployment build is done, if a message needs to be displayed it should be in an message dialog (MessageDlg). The reason for this is that a message dialog looks better with icons specify what the message is, I use GExperts to create the code. Also, when developing I can quickly search for 'showmessage' and can see what messages need to be removed, this means there are no embarrassing debug messages displayed to the user.
Secondly is the message just says error, which it not helpful to the user or the developer when it is reported. The developer could have done something better by giving a more useful message like 'An error has occurred while...', with what the operation is doing. The developer could have also included the exception message like the following.
try
{Some code}
except
on e: exception do
MessageDlg('An error has occured in...' + e.Message, mtError, [mbOK], 0);
end;
try
{Some code}
except
showmessage('error');
end;
This is a good example of the developer being lazy and not being bothered to do something better. Can you imagine the user ringing up support and saying they are getting a 'error' message, it's not even with a capital 'E'.
There are a couple of issues I have with this bit of code. Firstly I have a rule that showmessage() is used while developing and should be removed when a deployment build is done, if a message needs to be displayed it should be in an message dialog (MessageDlg). The reason for this is that a message dialog looks better with icons specify what the message is, I use GExperts to create the code. Also, when developing I can quickly search for 'showmessage' and can see what messages need to be removed, this means there are no embarrassing debug messages displayed to the user.
Secondly is the message just says error, which it not helpful to the user or the developer when it is reported. The developer could have done something better by giving a more useful message like 'An error has occurred while...', with what the operation is doing. The developer could have also included the exception message like the following.
try
{Some code}
except
on e: exception do
MessageDlg('An error has occured in...' + e.Message, mtError, [mbOK], 0);
end;
Labels:
Delphi,
error,
exception,
Handle,
messagedlg,
not,
showmessage
Friday, 5 July 2013
Delphi XE4 - The future of software development?
Delphi XE4 - The future of software development?: I have been working in software development for 15 years and have seen some changes, but nothing as much as what has happened recently. Over...
Monday, 1 July 2013
Delete files with a wildcard
Here is how to delete files with a wildcard. It requires SysUtls in the uses clause.
procedure DeleteFiles(FilePath, Wildcard: string);
var cSearchRec: TSearchRec;
iFind : integer;
sPath : string;
begin
sPath := IncludeTrailingPathDelimiter(FilePath);
iFind := FindFirst(sPath + Wildcard, faAnyFile, cSearchRec);
while iFind = 0 do
begin
DeleteFile(sPath + cSearchRec.Name);
iFind := FindNext(cSearchRec);
end;
FindClose(cSearchRec);
end;
In the while loop you can add additional conditions, like only files that have a specific prefix e.g.
if Pos(fPrefix, Copy(cSearchRec.Name, 0, Length(fPrefix))) > 0 then
procedure DeleteFiles(FilePath, Wildcard: string);
var cSearchRec: TSearchRec;
iFind : integer;
sPath : string;
begin
sPath := IncludeTrailingPathDelimiter(FilePath);
iFind := FindFirst(sPath + Wildcard, faAnyFile, cSearchRec);
while iFind = 0 do
begin
DeleteFile(sPath + cSearchRec.Name);
iFind := FindNext(cSearchRec);
end;
FindClose(cSearchRec);
end;
In the while loop you can add additional conditions, like only files that have a specific prefix e.g.
if Pos(fPrefix, Copy(cSearchRec.Name, 0, Length(fPrefix))) > 0 then
Friday, 21 June 2013
Delphi After Resize Event
Here is the way to handle what happens after a user has resized a form. This works fine for standard forms, but does not work for forms within a form.
procedure WMEXITSIZEMOVE(var Message: TMessage); message WM_EXITSIZEMOVE;
procedure TForm1.WMEXITSIZEMOVE(var Message: TMessage);
begin
ShowMessage('Just Resized');
end;
I would like to know if there is a way to capture an event when resizing a form in a form.
There is also a similar Windows message for when a user starts to resize a form, which is WM_ENTERSIZEMOVE.
There is also a similar Windows message for when a user starts to resize a form, which is WM_ENTERSIZEMOVE.
Labels:
After,
Delphi,
Event,
form,
Handle,
Message,
resize,
wm_entersizemove,
WMEXITSIZEMOVE
Monday, 20 May 2013
How to store application settings in an INI file
Usually when you write a piece of software you want to store off user information about the software, for example window positions and sizes, there are multiple ways of doing this, here is a simple example using an INI file.
uses iniFiles;
procedure TForm.SaveSettings;
var
iniFile: TIniFile;
begin
iniFile := TIniFile.Create(GetCurrentDir + '/Settings.ini');
try
iniFile.WriteInteger('MAINFORM', 'LEFT', Left);
iniFile.WriteInteger('MAINFORM', 'TOP', Top);
iniFile.WriteInteger('MAINFORM', 'WIDTH', Width);
iniFile.WriteInteger('MAINFORM', 'HEIGHT', Height);
finally
iniFile.Free;
end;
end;
procedure TForm.LoadSettings;
var
iniFile: TIniFile;
begin
iniFile := TIniFile.Create(GetCurrentDir + '/Settings.ini');
try
Left := iniFile.ReadInteger('MAINFORM', 'LEFT', Left);
Top := iniFile.ReadInteger('MAINFORM', 'TOP', Top);
Width := iniFile.ReadInteger('MAINFORM', 'Width', Width);
Height := iniFile.ReadInteger('MAINFORM', 'Height', Height);
finally
iniFile.Free;
end;
end;
There are 2 procedures here that load and save the forms size and position. The values are stored in the INI file located in the same directory as the executable. Other ways of storing these values would be in the registry or in rare cases you may want to store these sort of settings in a database.
uses iniFiles;
procedure TForm.SaveSettings;
var
iniFile: TIniFile;
begin
iniFile := TIniFile.Create(GetCurrentDir + '/Settings.ini');
try
iniFile.WriteInteger('MAINFORM', 'LEFT', Left);
iniFile.WriteInteger('MAINFORM', 'TOP', Top);
iniFile.WriteInteger('MAINFORM', 'WIDTH', Width);
iniFile.WriteInteger('MAINFORM', 'HEIGHT', Height);
finally
iniFile.Free;
end;
end;
procedure TForm.LoadSettings;
var
iniFile: TIniFile;
begin
iniFile := TIniFile.Create(GetCurrentDir + '/Settings.ini');
try
Left := iniFile.ReadInteger('MAINFORM', 'LEFT', Left);
Top := iniFile.ReadInteger('MAINFORM', 'TOP', Top);
Width := iniFile.ReadInteger('MAINFORM', 'Width', Width);
Height := iniFile.ReadInteger('MAINFORM', 'Height', Height);
finally
iniFile.Free;
end;
end;
There are 2 procedures here that load and save the forms size and position. The values are stored in the INI file located in the same directory as the executable. Other ways of storing these values would be in the registry or in rare cases you may want to store these sort of settings in a database.
Friday, 17 May 2013
Delphi XE4 - The Future Of Software Development
I have been using Delphi for some years now and have also done development with XCode and DotNet, so was very interested in the latest version of Delphi. It seems to do something unique, you can develop software to be deployed to various different devices and operating systems. From what I have seem developers are successfully using Delphi to produce Apps for iPhone and iPad, the surprising thing is that they are true native apps and do not rely on any virtual machine like Mono.
It does sound almost like development nirvana, where you can develop the business code and from a single code base deploy to different operating systems and devices, this surely is the future of software development. Recently when I saw demos of PhoneGap I thought this was the future, but after reading an article about Facebook and their problems with HTML5 and the fact they are moving back to native apps, I was not sure which way was the future, HTML5, PhoneGap, Mono or XCode, Android and DotNet. I have not used XE4 yet, but I am looking forward to using it and exploring what potential it might have.
It does sound almost like development nirvana, where you can develop the business code and from a single code base deploy to different operating systems and devices, this surely is the future of software development. Recently when I saw demos of PhoneGap I thought this was the future, but after reading an article about Facebook and their problems with HTML5 and the fact they are moving back to native apps, I was not sure which way was the future, HTML5, PhoneGap, Mono or XCode, Android and DotNet. I have not used XE4 yet, but I am looking forward to using it and exploring what potential it might have.
Subscribe to:
Posts (Atom)