Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

drag and drop help

Can any one tell me how i would write a program that allows me to drag labels from a panel and place them into a edit box?

i've tried but cannot figure it out!

Thanx

Comments

  • zibadianzibadian Member Posts: 6,349
    : Can any one tell me how i would write a program that allows me to drag labels from a panel and place them into a edit box?
    :
    : i've tried but cannot figure it out!
    :
    : Thanx
    :
    For this code to work, you'll need to set the DragMode of all the label you want to be dragged to dmAutomatic.
    [code]
    procedure TForm1.Edit1DragDrop(Sender, Source: TObject; X, Y: Integer);
    begin
    if Source is TLabel then
    (Sender as TEdit).Text := (Source as TLabel).Caption;
    // Set the edit text to the label caption,
    // if you want some other behaviour on the drop, then you'll
    // need to change this code.
    end;

    procedure TForm1.Edit1DragOver(Sender, Source: TObject; X, Y: Integer;
    State: TDragState; var Accept: Boolean);
    begin
    Accept := Source is TLabel;
    // Necessary to enable the drop capability
    end;
    [/code]
    Notes:
    * OnDragDrop:
    -- DO NOT REFERENCE THE LABEL BY ITS NAME!!! This would only allow one label to be dropped.
    -- remember to type cast the Source (dropped object) to a label. There are two ways for this: TLabel(Source) or (Source as TLabel). The second method is saver, because it will first check if the source is of the correct class.
    -- if you want to do several things with the label, you should use the with-do statement.
    -- You can also use the same method to drag other objects over the edit. Just add an "else if Source is ... then" statements to the procedure.
    *OnDragOver:
    -- Remember to accept all object types you want to drop. The best way is to use the IS statement as in the code above, combined with one or more OR's. Example:
    [code]
    Accept := (Source is TLabel) or (Source is TButton);
    [/code]
Sign In or Register to comment.