Create ComboBox

<< Click to Display Table of Contents >>

Navigation:  Programming > Mail Merge (replace fields with data) and data forms >

Create ComboBox

Now we show how to create a pick list for a merge field:

 

     

 

We use a panel with a speed button to display the drop down button. This panel is moved to the correct location in the MouseMove event. A timer is used to hide this panel if it is not required anymore. The picklist itself is a listbox, also placed on the form. It would be better to display this list on a seperate form located over the mainform but we wanted to make this example as simple as possible.

 

The name of the current field (for which the picklist is displayed) is stored in the string variable CurrField.

 

The MouseMove event handler for the editor. It sets 'CurrField'

 

procedure TWPEdTest.DataEditMouseMove(Sender: TObject; Shift: TShiftState;

 X, Y: Integer);

var fieldobj, fieldobjend : TWPTextObj;   px, py : Integer;

begin

 fieldobjend := nil;

 fieldobj := DataEdit.CodeInsideOf(x, y, wpobjMergeField);

if (fieldobj <> nil) and ((CompareText(fieldobj.Name,'name')=0) or

                           (CompareText(fieldobj.Name,'company')=0)) then

begin

    fieldobjend := fieldobj.EndTag;

    if fieldobjend<>nil then

    begin

       DataEdit.GetParXYBaselineScreen(fieldobjend.ParentPar, fieldobjend.ParentPosInPar, px, py  ) ;

       PickPanel.Left := px - PickPanel.Width div 2;

       PickPanel.Top  := DataEdit.Top + py - MulDiv(PickPanel.Height,4,5);

       CurrField := fieldobj.Name;

    end;

end;

if fieldobjend=nil then

      HidePickPanelTimer.Enabled := TRUE // we hide it 500ms delayed

else

begin

    PickPanel.Visible := true;

    HidePickPanelTimer.Enabled := FALSE;

end;

end;

 

The timer HidePickPanelTimer disables the button

 

procedure TWPEdTest.HidePickPanelTimerTimer(Sender: TObject);

begin

 HidePickPanelTimer.Enabled := FALSE;

 PickPanel.Visible := false;

end;

 

The OnExit event of the listbox  (it is called when the listbox looses the focus)is used to hide the listbox when the user clicks on the editor:

 

procedure TWPEdTest.PicklistExit(Sender: TObject);

begin

 Picklist.Visible := FALSE;

end;

 

A click on the speedbutton display the listbox:

 

procedure TWPEdTest.PickDropClick(Sender: TObject);

begin

 Picklist.Left := PickPanel.Left + PickPanel.Width - Picklist.Width;

 

if CompareText(CurrField,'name')=0 then

      Picklist.Items.Assign(PicName.Lines)

else if CompareText(CurrField,'company')=0 then

    Picklist.Items.Assign(PicCompany.Lines)

else Picklist.Items.Clear;

 

if PickPanel.Top>EditTab.Height-100 then

begin

   Picklist.Height := PickPanel.Top -20;

  if Picklist.Height>150 then Picklist.Height := 150;

   Picklist.Top    := PickPanel.Top - Picklist.Height;

end else

begin

   Picklist.Top  := PickPanel.Top + PickPanel.Height;

   Picklist.Height := EditTab.Height - Picklist.Top-20;

  if Picklist.Height>150 then Picklist.Height := 150;

end;

 Picklist.Visible := TRUE;

 Picklist.SetFocus;

end;

 

Last but not least - a click in the listbox should update the current field. We use the function [b]CodeListTags[/b] to find all the field with a given name and simply update the property EmbeddedText.

 

procedure TWPEdTest.PicklistClick(Sender: TObject);

var objlst : TWPTextObjList;

   i : Integer;

   s : string;

begin

  if Picklist.ItemIndex>=0 then

       s := Picklist.Items[Picklist.ItemIndex]

  else exit;

  objlst := DataEdit.CodeListTags(wpobjMergeField,CurrField, true);

  try

    for i:=0 to objlst.Count-1 do

      objlst[i].EmbeddedText := s;

  finally

    objlst.Free;

    DataEdit.ReformatAll(false, true);

  end;

  DataEdit.Setfocus; // also hides Picklist!

end;

 

You can download the complete example here

http://www.wpcubed.com/ftp/ex/EditFieldDemo.zip