How to change Hyperlink's Text in event (create picklist)

  • Hi,

    Using Delphi 5, Windows XP and 2000 Pro.
    I have WPT 3.x that I have not used in 2+ years. Starting a new project so I want to use the latest WPTools. I will upgrade to WPTools 5.x just for the following requirement which relates to changing Text of the Hyperlink in a Hyperlink event at runtime.

    My questions has been touched in the following post by Erir (ehimmer):
    --> Mar 21, 2005 11:12 pm Post subject: How to create embedded picklists? Apparently, Eric was pleased with the solution he found but he did not share it with mere mortals.

    Just to be clear about my vocabulary, Hyperlink's url = "http://www.yahoo.com" and Hyperlink's Display Text = "Yahoo". Following is what I need to do.

    At startup, I want to save a tokenized list in Hyperlink's url (e.g., Tiny, Small, Medium, Large, XL) and show"Medium" (one of the list items) as hyperlink's Display Text. The HTML equivalent would be <a href="Tiny, Small, Medium, Large, XL"> Medium </a>

    When user clicks on Hyperlink ("Medium" at this point), program reads the associated URL and detokenizes the list. Thus, I can show a CheckListBox with items: Tiny, Small, Medium, Large, XL. Let us say, user selects: Large and XL. I want to change Hyperlink's Diplay Text to "Large and XL". Now, I should have HTML equivalent of <a href="Tiny, Small, Medium, Large, XL"> Large and XL </a>

    How will I do this with WPTools 5.x ?
    Which is the best Event to use to do this with one click from user ?
    Which object to use and which properties will need to be set to accomplish the above ?
    Do I need to call "Refresh" or something similar to update the display ?

    I have been away from WPTools too long, so explicit/clear example code will be highly appreciated.

    Thank you very much

    Jay

    • Offizieller Beitrag

    To start I would use a popup menu. Having a popup checklistbox is possible, too. But then you need to create it on a floating form which hides itself when it looses the focus. The implementation requires an asyncron opening of the popup - otherwise I found out that some kind of loop is created. I am using a timer for this.

    On the form we need the editor and a TTimer and the TPopupMenu: HyperPopup. In the interface of the form we need:

    Code
    hyperobj : TWPTextObj;    procedure DoItemClick(Sender : TObject);

    No add a MouseDown event handler to the WPRichText to hande the hyperlinks yourself:

    Code
    procedure TForm1.WPRichText1MouseDown(Sender: TObject;  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);begin  hyperobj := WPRichText1.CodeInsideOf(x, y, wpobjHyperlink);  if (hyperobj <> nil) and (hyperobj.ObjType = wpobjHyperlink) then  begin    Timer1.Interval := 100;    Timer1.Enabled := TRUE;       // ShowMessage(hyperobj.Source);    WPRichText1.IgnoreMouse;  end;end;

    Now we need the timer event which does the work:

    Code
    procedure TForm1.Timer1Timer(Sender: TObject);var  str: TStringList;  i: Integer;  men: TMenuItem;begin    Timer1.Enabled := FALSE;  if hyperobj <> nil then  begin    HyperPopup.Items.Clear;    HyperPopup.AutoHotkeys := maManual;    str := TStringList.Create;    try      str.CommaText := hyperobj.Source;      for i := 0 to str.Count - 1 do      begin        men := TMenuItem.Create(HyperPopup);        men.Caption := str[i];        men.OnClick := DoItemClick;        HyperPopup.Items.Add(men);      end;      if HyperPopup.Items.Count > 0 then      begin        HyperPopup.Popup(          Mouse.CursorPos.X,          Mouse.CursorPos.y          );      end;         // no: hyperobj := nil;    finally      str.Free;    end;  end;end;


    Of course we need the event which reacts on the click. It simply has to modify the property "EmbeddedText" of the hyperlink object.

    Code
    procedure TForm1.DoItemClick(Sender : TObject);begin  if hyperobj<>nil then  begin     hyperobj.EmbeddedText := (Sender as TMenuItem).Caption;     WPRichText1.DelayedReformat;  end;end;

    To create a hyperlink you can use

    Code
    procedure TForm1.Button1Click(Sender: TObject);begin  WPRichText1.InputHyperlink('link',Edit1.Text);end;

    The above code will open the popup simply at the mouse coordinates. To show it under the link text (like a drop down) you can claculate the position (p : TPoint) like this:

    Code
    p := WPRichText1.GetPointFromParLin(hyperobj.ParentPar,hyperobj.ParentPosInPar);
        inc(p.y, MulDiv(hyperobj.ParentPar.CharPos[hyperobj.ParentPosInPar+1].Height,
           WPScreenPixelsPerInch, WPRichText1.Memo.CurrentYPixelsPerInch));
        p := WPRichText1.ClientToScreen(p);

    I hope this helps,

    Julian Ziersch

  • Julian:

    Thank you very much for taking time to post such a detailed reply. This will not only help me but also many others who will be looking for a similar solution.

    Highly appreciated. I am ordering WPT 5.x upgrade this weekend.

    JaySpock

  • Jay,

    As I shared with Julian privately today, this reply was extremely helpful to me, and the topic itself is of no personal relevence to my application!

    Although I don't use Hypertext links or pick lists in my application, reading through Julian's code provides examples of different ways to use the WP5 "atoms" that might not necessarily occur to me. I make a point of reading ALL replies from Julian, they're almost like mini-tutorials...:) This one was no exception.

    You can't go wrong in upgrading, especially in view of the unparalled support Julian provides. The more I learn WP 5, the more impressed I am - and I've been using the system since WP 2, when Julian distributed the system on a single diskette (which I've kept as a souvenier) ...:)

    Regards,
    richard diamond