Mega Code Archive

 
Categories / Delphi / Examples
 

Simple context sensitive help

Title: Simple context-sensitive help Question: I am creating a not so big application and I want to give the users a context-sensitive help without creating a big windows helpsystem or using all kind of tools Answer: Most of the times its overkill to use tools to create a real Windows-Helpsystem for smaller applications. However, often youd like to give the user some help besides a printed manual. Here is a small example of the way I do it: Create a new application with 2 forms. The second form will be the Help-form in this example. On the second form is a RichEdit-control called RedHelp with the ReadOnly-property on True and the OnKeyUp-event of RedHelp is implemented as follows: procedure TForm2.redHelpKeyUp(Sender: TObject; var Key: Word; Shift: TShiftState); begin if key = VK_ESCAPE then close; end; For the first form I set the KeyPreview-property on True and I placed three components on the form as example. Of course there are other ways and more sofisticated ways to do this, but I wanted to keep it simple. The complete code of Unit1 is here below: unit Unit1; interface uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, ExtCtrls; type TForm1 = class(TForm) Edit1: TEdit; Memo1: TMemo; RadioGroup1: TRadioGroup; procedure FormKeyUp(Sender: TObject; var Key: Word; Shift: TShiftState); private { Private declarations } public { Public declarations } end; var Form1: TForm1; implementation uses Unit2; {$R *.DFM} procedure TForm1.FormKeyUp(Sender: TObject; var Key: Word; Shift: TShiftState); var dir, s, HelpFile : string; begin if key = VK_F1 then //if a F1 is pressed begin s := Form1.ActiveControl.Name; //look which control is active if s = '' then //when e.g. an item off a RadioGroup is active s := Form1.ActiveControl.Parent.Name; if s = '' then s := Application.ExeName; //always get something dir := ExtractFilePath(Application.ExeName);//get the applications-directory HelpFile := format('%s%s.rtf',[dir, s]); //make the complete filename if FileExists(HelpFile) then //if it's exists, load it begin Form2.redHelp.Lines.LoadFromFile(HelpFile); Form2.Show; end else //if it's not there, create it begin Form2.redHelp.Lines.Clear; Form2.redHelp.Lines.Add(format('Help for %s-control', [s])); Form2.redHelp.Lines.Add('Add your helptext here'); Form2.redHelp.Lines.SaveToFile(HelpFile); end; end; end; end.