如何在图片框中移动多个用户控件C# [英] How I do move multiple user control in picture box C#

查看:88
本文介绍了如何在图片框中移动多个用户控件C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我制作了一个用户控件。

相同的用户控件在图片框中拖动3次,现在我想在运行时将多个用户控件移到图片框中



我尝试了什么:



我试过这段代码,但它正在处理按钮控制..





公共类测试

Dim a As Boolean = False



Private Sub Button1_MouseUp(ByVal sender As System.Object,ByVal e As System.Windows.Forms.MouseEventArgs)Handles Button1.MouseUp

a = False

End Sub



Private Sub Button1_MouseDown(ByVal sender As System.Object,ByVal e As System.Windows.Forms.MouseEventArgs)Handles Button1.MouseDown

a = True

End Sub



Private Sub Button1_MouseMove(ByVal sender As System.Object,ByVal e As System.Windows.Forms.MouseEventArgs)处理Button1.MouseMove



如果a = True然后



Button1.Left = Control.MousePosition.X - Me.Left - 10

Button1.Top = Control.MousePosition.Y - Me.Top - 40



结束如果



结束子



结束类

I made a user control.
same user control drag 3 time in picture box and now i want to move multiple user control in picture box at runtime

What I have tried:

I have tried this code but its working on button control..


Public Class test
Dim a As Boolean = False

Private Sub Button1_MouseUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Button1.MouseUp
a = False
End Sub

Private Sub Button1_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Button1.MouseDown
a = True
End Sub

Private Sub Button1_MouseMove(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Button1.MouseMove

If a = True Then

Button1.Left = Control.MousePosition.X - Me.Left - 10
Button1.Top = Control.MousePosition.Y - Me.Top - 40

End If

End Sub

End Class

推荐答案

首先,那是VB,而不是C# - 正确标记你的问题很重要!

其次,如果它适用于Button,则它应该适用于UserControl。

最好的方法是用一般控件替换Button1上的固定依赖项,并使用 sender 用于标识所涉及控件的参数:

Firstly, that's VB, not C# - it's important to tag your question correctly!
Second, if it works for a Button then it should work for a UserControl.
The best way to do it is to replace the fixed dependency on Button1 with a generic Control, and use the sender parameter to identify the control that is involved:
Private Sub Control_MouseMove(sender As System.Object, e As System.Windows.Forms.MouseEventArgs)
	Dim c As Control = TryCast(sender, Control)
	If a = True Then
		c.Left = Control.MousePosition.X - Me.Left - 10
		c.Top = Control.MousePosition.Y - Me.Top - 40
	End If
End Sub

并使用它来处理您的活动。

And use that to handle your events.


这篇关于如何在图片框中移动多个用户控件C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆