异常拖动问题
我有一个图片框,用户可以向上或向下拖动它。
所讨论的程序是一个复杂的钢琴音乐五线谱编辑器,因此实现五线谱上音符移动的唯一方法是使用多个 if 语句和修改坐标。
问题是用户无法向下移动 PictureBox 组件,但向上拖动该对象时却没有任何反应。该类继承自PictureBox。
我想强调的是,向下拖动时 PictureBox 可以工作,但向上拖动时则不会移动。拖动是按间隔进行的,即PictureBox 只能放置在某些位置(因此需要特定的坐标)。
I've got a Picturebox, which the user can drag up or down.
The program in question is a complex music staff editor for a piano, and thus the only way to achieve the moving of notes on a staff is with a number of if-statements and by modifying co-ordinates.
The issue is that the user is unable to move the PictureBox component down, but when the object is dragged up, nothing happens. The class inherits from PictureBox.
I would just like to emphasise that the PictureBox works when dragged downwards, but does not move when dragged upwards. The dragging is done in intervals, i.e. the PictureBox can only be placed in certain places (hence the need for specific co-ordinates).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您当前的解决方案有时可能会起作用,但是当您尝试拖动控件并将其恢复到您在
if
语句中指定的坐标时,该事件可能会经常被调用。建议的解决方案:
我建议您在包含要拖动的控件的表单或父级中使用
MouseMove
事件。这些值还应该是可配置的,而不是硬编码的。该代码只需更改一点点(比较当前鼠标坐标而不是控件的
Left
和Top
属性),它应该可以正常工作。更新:
我已经更正了您的代码,因此它现在可以允许您将控件放在三个位置之一(y等于138、148 或 158)。我只对其进行了一点更改,并不要求您更改大量代码,但我强烈建议您使用所描述的第一种方法:)。
然而,之前的解决方案可能更符合您的要求。
Your current solution may work sometimes, however the event can be invoked very often when you try to drag the control and snap it back to the coordinates you specified in
if
statements.Suggested solution:
I would suggest you to use
MouseMove
event in the form or parent that contains the control you want to drag. The values should also be configurable, not hardcoded.The code would change only a little (comparing current mouse coordinates instead of control's
Left
andTop
properties) and it should work correctly.UPDATE:
I've corrected your code, so it now works allowing you to put a control in one of the three places (y equals to 138, 148 or 158). I change it only a bit not to require you to change a lot of code, but I strongly recommend you to use the first method described :).
The previous solution would, however, work probably more as you want it to.