Search code examples
c#winformsmovedraglag

Winforms: Form lags when trying to move/Drag it on windows 10


Not sure if the title makes sense. I have a form that has several panels that each has a form associated with them. I call this form the "mainForm", and when I move/drag it across my screen it lags a lot, the mouse also feels extremely unresponsive.

When you first open it and it doesn't lag at all, however after having some controls on screen it lag becomes noticeable and when I have over 100 controls it becomes very laggy. IF I don't move it, the program runs fine and everything in itself is good, it's only when I try to move it.

I'm not entirely sure how this works, but when you move it, does it recalculate something on the form and the controls and are on it? If so is it possible to disable that when trying to move and reenabling it when we stopped moving?

Changing to WPF at this point is not possible due to time constraints.

Hopefully, someone has had an issue like this before. Any tips are extremely appreciated.

Thank you for your time guys!


Solution

  • I ended up solving it. The issue was happening even with the code that Morten Bork shared. It was simply too many controls (I had about 500 controls in the test I did) and the form was going crazy slow when moved.

    The user Jimi input on the matter was key, after looking around in the events I tried the following and it solved my issue. So hopefully someone that needs a solution for something similar can use my solution.

    In my "mainForm" when it moves I clear the main panel I have in "ResizeBegin" and add the sub panels back to the main panel in "ResizeEnd" and it works flawlessly so far. Since I also saved the current sub panel being displayed I can make that one display first.