十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
Public Class 用户控件
成都创新互联公司是一家专注于成都网站制作、成都网站设计、外贸营销网站建设与策划设计,会昌网站建设哪家好?成都创新互联公司做网站,专注于网站建设10余年,网设计领域的专业建站公司;建站业务涵盖:会昌等地区。会昌做网站价格咨询:18982081108
Inherits System.Windows.Forms.Panel
Public Sub New() '初始化
End Sub
Private Sub 用户控件_KeyDown(sender As Object, e As Forms.KeyEventArgs) Handles Me.KeyDown
' ……
End Sub
'……
End Class
Public Class UserControl1
#Region "变量"
Dim Down_Color As Color = Color.Blue
Dim UP_Color As Color = Color.Gray
Dim Mode As Short = 0
Dim flag As Boolean
Dim offset_X As Integer
Dim offset_Y As Integer
Dim Mouse_P As Point
#End Region
#Region "属性"
'按下颜色
Public Property _DownColor As Color
Get
Return Down_Color
End Get
Set(ByVal value As Color)
Down_Color = value
End Set
End Property
'弹起颜色
Public Property _UpColor As Color
Get
Return UP_Color
End Get
Set(ByVal value As Color)
UP_Color = value
End Set
End Property
'滑动模式 0-横 1-竖
Public Property _Mode As Short
Get
Return Mode
End Get
Set(ByVal value As Short)
Mode = value
End Set
End Property
#End Region
Private Sub UserControl1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Me.BackColor = UP_Color
End Sub
'鼠标按下
Private Sub UserControl1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseDown
Me.BackColor = Down_Color
Mouse_P = e.Location
flag = True
End Sub
'鼠标移动
Private Sub UserControl1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove
If flag = False Then Exit Sub
Select Case Mode
Case 0 '横向·
offset_X = e.X - Mouse_P.X
If Me.Location.X + offset_X + Me.Width = Me.ParentForm.Width Or Me.Location.X + offset_X = 0 Then
flag = False
Else
Me.Location = New Point(Me.Location.X + offset_X, Me.Location.Y)
End If
Case 1 '竖向·
offset_Y = e.Y - Mouse_P.Y
If Me.Location.Y + offset_Y + Me.Height + 30 = Me.ParentForm.Height Or Me.Location.Y + offset_Y = 0 Then
flag = False
Else
Me.Location = New Point(Me.Location.X, Me.Location.Y + offset_Y)
End If
End Select
End Sub
'鼠标弹起
Private Sub UserControl1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseUp
Me.BackColor = UP_Color
flag = False
End Sub
End Class
关于用户控件的使用,.net与v6环境下是有那么小小的区别。v6环境下的用户控件编写后可以直接使用,而在.net环境下,则需要先生成控件库(dll,应用程序扩展格式),然后再将这个控件库导入到.net编程环境的工具箱上方可使用。其具体方法如下(以VS 2010,Visual Basic.net 为例):1、新建项目-Visual Basic-Windows-Windows 窗体控件库-键入控件库名称(默认为Usercontrol1)确定;2、设计你的用户控件,完成后按“F7”进行编译;3、在该解决方案目录下找到控件库扩展(默认为UserControl1.dll),确认项目已经生成。在上三项为控件库DLL的生成方式,下面是调用方式:4、新建项目-Visual Basic-Windows-Windows 窗体应用程序-确定;5、打开工具箱,以任意一个控件上点击右键-选择项-.net framework组件-浏览-定位到生成的控件库扩展-确定;6、在工具箱中找到新添加的用户控件,将其拖至窗体中进行设计。经测,在.net环境下编译的用户控件是通用的。也就是说使用Visual Basic.net编写的用户控件,也可以在C#工程里使用,反之亦然。这充分体现了.net语言的交互性,在我看来,.net下各种语言能力相当,它们都是作为一种中间语言,编译出中间代码。所以,在.net环境下,采用混合编程也未尝不是一样提高效率的一种可选的方式。
控件不一定有界面,类也不一定有界面。
以Visual Studio 2012为例
把你的控件封装到类里面,生成exe或dll,在要调用的项目中,进入工具箱,右键任意项,选择项,在.Net Framework组件中浏览到生成的exe或dll,在上面列表中相应的内容,勾选,确定