How to create dropdown for merge field (picklist #2)

    • Offizieller Beitrag

    WPTools has very powerful merge end 'editfield' capabilities. With a little coding you can also show drop down buttons to display "picklists".

    mergepic.png

    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'

    Code
    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

    Code
    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:

    Code
    procedure TWPEdTest.PicklistExit(Sender: TObject);begin  Picklist.Visible := FALSE;end;

    A click on the speedbutton display the listbox:

    Code
    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 CodeListTags to find all the field with a given name and simply update the property EmbeddedText.