Documents

NextGrid & NextDBGrid

NextGrid

NextDBGrid

NextInspector

NextSheet

NextCollection

Misc

Labs

NextGrid .NET

NxAutoCompletion Tutorial

Related Articles:

Info
  • Skill:
  • Version: 1.0.0
  • Author: Wim van der Vegt
  • Created: 2008-07-22
  • Updated: 2008-07-22

Introducing NxAutocompletion



Autocompletion is a feature used often in windows. For instance the address bars of both explorer and internet explorer and the start|run dialog feature autocompletion. Besides the GUI handling of the autocompletion, Windows knows a number of sources for autocompletion suggestions. By choosing the appropriate sources one can tap into the history or file and folder names.



NxAutoCompletion brings this feature into the Next Suite.

NxAutoCompletion works quite simple, associate an NxAutoCompletion component with a TWinControl descendant control and selected from which sources windows has to supply suggestions for autocompletion. There are only a few options that have to do with appearance.

Available TNxAutoCompletion Sources are:

Table 1: Sources flags
Name Meaning
asShell Shell, i.e. files and folder names.
asHistory History folder.
asMRU Most recently run programs.
asList Custom list of strings defined by the Strings property.


Multiple sources are allowed but keep in mind the option can be slow for all except the asList option as the registry has to be accessed to retrieve the values!

Most interesting source for programs is perhaps the asList where a TStrings property called Strings can be used to store suggestion values for autocompletion.

Options property:

Table 2: Options flags
Name Meaning
aoAutoAppend Automatically append text.
aoAutoSuggest Use a dropdown box as seen in the Start|Run dialog.
aoUseArrowKey Use arrow keys to navigate through the suggestions.


Using NxAutoCompletion



There are three basic ways of using the NxAutoCompletion.

Associate a single TNxAutoCompletion component with a single TWinControl component.

Use a design time created TNxAutoCompletion for multiple TWinControl components.

Use a runtime created TNxAutoCompletion for one or more TWinControl components.

If NxAutoCompletion is used to supply autocompletion for a single component, itís enough to associate the controls in the object inspector and customize the settings of the NxAutoCompletion component. No programming is required.

The result looks like (note that the entries shown are originating from the Strings property):



If NxAutoCompletion should be used with multiple components, one can either switch the associated control to whatever is in edit mode but itís also possible to create a TNxAutoCompletion on the fly at runtime.

In the following code a TNxAutoCompletion object is created and associated with TNxEdit called NxEdit1 when editing starts.

procedure TForm1.NxEdit1Enter(Sender: TObject);
begin
  if not Assigned(NxAutoCompletion2) then
  begin
    NxAutoCompletion2 := TNxAutoCompletion.Create(Self);
    NxAutoCompletion2.Sources := [asList, asShell, asHistory, asMRU];
    NxAutoCompletion2.Strings.Add('aaaa');
    NxAutoCompletion2.Strings.Add('aabb');
  end;
  
  if (NxAutoCompletion2.Associate <> TNxEdit(Sender)) then
  begin
    NxAutoCompletion2.Associate := TNxEdit(Sender);
    NxAutoCompletion2.Enabled := True;
  end;
end;


When we want to enable autocompletion for a TNextGrid cells we can use the following code (in case we dropped a TNxAutoCompletion component onto the form):

procedure TForm1.NextGrid1Edit(Sender: TObject; ACol, ARow: Integer; Value:
  WideString);
Begin
  if Assigned(TNextGrid(Sender).InplaceEdit)
    and (NxAutoCompletion1.Associate <> TNextGrid(Sender).InplaceEdit)
    then NxAutoCompletion1.Associate := TNextGrid(Sender).InplaceEdit;
End;

procedure TForm1.NextGrid1AfterEdit(Sender: TObject; ACol, ARow: Integer; Value:
  WideString);
begin
  NxAutoCompletion1.Associate := nil;
end;


Itís also possible to combine autocompletion with a dynamically created TNxAutoCompletion as is seen in the following code:

procedure TForm1.NextGrid1Edit(Sender: TObject; ACol, ARow: Integer; Value:
  WideString);
Begin
  if not Assigned(NxAutoCompletion2) then
  begin
    NxAutoCompletion2 := TNxAutoCompletion.Create(Self);
    NxAutoCompletion2.Sources := [asList, asShell, asHistory, asMRU];
    NxAutoCompletion2.Strings.Add('aaaa');
    NxAutoCompletion2.Strings.Add('aabb');
  end;

  if Assigned(TNextGrid(Sender).InplaceEdit) and
    (NxAutoCompletion2.Associate <> TNextGrid(Sender).InplaceEdit) then
  begin
    NxAutoCompletion2.Associate := TNextGrid(Sender).InplaceEdit;
    NxAutoCompletion2.Enabled := True;
  end;
end;

procedure TForm1.NextGrid1AfterEdit(Sender: TObject; ACol, ARow: Integer; Value:
  WideString);
begin
  if Assigned(NxAutoCompletion2) then FreeAndNil(NxAutoCompletion2);
end;


The result looks like (Note that Entries from the History List are shown):



Note: currently there is no support for settings the root folder from which the asShell provider should start supply suggestions.

Was This Article Useful?

Only constructive comments, code contributions... will be publishes. Questions, non-official discussion will not be published.