Dynamically generated Controls with Drag & Drop


REFERENCE :  https://docs.wisej.com/docs/controls/general/drag-and-drop-1

Can you programmatically apply (assign)  events   (Drag Drop Mouse Down whatever ) for dynamically created controls ( buttons in my case)

so that they can use the drag and drop features ?

I am specifically having problems defining an event handler for dynamically created buttons:

dynamicallyCreatedControlsNameHere.DragStart += new DragEventHandler( what goes here my method name or  sender,  args)

I understand the difference between invoking the method versus appending a new method to the event’s default .

If there is some required keyword or something like “OnDragStart” that would be helpful  to know  but I don’t think that is req’d.

Neither works for me.

regular Winforms accepts the new MethodName you want hit for that event — I just made a few to confirm.

[ https://docs.wisej.com/api/wisej.web/general/control/wisej.web.drageventhandler ]

Attempting this in WiseJ just will not accept my new method name as a parameter:

private void Form1_Load(object sender, EventArgs e)
//this.button1_Click += new Action<object, EventArgs>
//foo.MyEvent += new EventHandler(this.OnMyEvent);
button1.Click += new EventHandler(PraDuShalaie);
button1.Click += new EventHandler(PraDuShalaie);

//OnClick(EventArgs e);

Button imaSpecialBtn = new Button();
imaSpecialBtn.Click += new EventHandler(PraDuShalaie);
imaSpecialBtn.DragEnter += new DragEventHandler(PraDuShalaie);

I have a method named PraDuShalaie handy for this test

private void PraDuShalaie(object sender, EventArgs e)

thank you


  • You must to post comments


fix is included in our latest release build (3.1.8).

Best regards

  • You must to post comments


I have attached a sample that should work now.

We identified an issue with the Button not starting Drag & Drop because the pointer is captured for the down/up movement.

This issue will be fixed in the next build (3.1.8).



  • You must to post comments

Good morning Luca,

your patience is greatly appreciated thank you again.
from the referenced WiseJ document “Drag & Drop” [ docs.wisej.com/docs/controls/general/drag-and-drop-1 ]
The first paragraph instructs:

set AllowDrag on my dynamically generated buttons that sit in my
SOURCE FlowLayoutPanel, as these buttons should initiate the drag operation.

I made sure my DESTINATION FlowLayoutPanel that is recieving the dragged buttons
to AllowDrop true, such that it CAN receive the dropped data/Buttons.
When the user starts dragging, the control will fire the DragStart event.

There is no DragStart event associated with this new programmatically created control.
You must handle this event and call the DoDragDrop() method, otherwise the drag operation doesn’t start.
So one must apparently manually associate a DragStart event to these newly created buttons.
I have a method available for that but associating it correctly is not working for me as Shown in the image attached.

Thus far, my tests are allowing me to create / assign a method to the programmatically created control,
(but it doesn’t hit the method)

My method is not invoked on attempt to “DragStart”  at runtime this new programmatically created button.

btnDynamic.DragStart += btnDynamic_DraggingButton;

  • You must to post comments

Yes. If by dynamically created controls you mean controls created by code, all controls are always created by code. There is nothing different and no other way to created any instance of any class in .NET.

The error you see is CS0176 and it’s also described in the error log. It’s telling you to use a class name instead of “this”. Simply because you declared the method as static. It’s completely unrelated to events.



  • You must to post comments
Showing 4 results
Your Answer

Please first to submit.