VB.NET自定义组件方法及注意事项

前端之家收集整理的这篇文章主要介绍了VB.NET自定义组件方法及注意事项前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

Imports System.ComponentModel
Imports System.IO
Imports System.Windows.Forms
Imports YeWuBill


Public Class TwoLed
Inherits System.Windows.Forms.PictureBox

'切记 属性必须对其赋值,否则导致系统崩溃。

Public Enum TwoLedState
None_Led = 0 'XF
Right_Led = 1 'XB
Mid_Led = 2 'XD
Mid_Right_Led = 3 'X9
Left_Led = 4 'XE
Left_Right_Led = 5 'XA
Left_Mid_Led = 6 'XC
Left_Mid_Right_Led = 7 'X8
Bad_Led = 8 '坏了
End Enum

Dim AddStated As Boolean
Dim SaveStated As Boolean

''' <summary>
''' 灯塔号
''' </summary>
''' <remarks></remarks>

Private mvarTitle As String = "1#灯塔"
<Browsable(True)> _
Public Property Title() As String
Get
Return mvarTitle
End Get
Set(ByVal value As String)
mvarTitle = value
Me.Invalidate()
End Set
End Property

''' <summary>
''' 灯塔地址
''' </summary>
''' <remarks></remarks>
Private mvarAddress As Integer = 1
<Browsable(True)> _
Public Property Address() As Integer
Get
Return mvarAddress
End Get
Set(ByVal value As Integer)
mvarAddress = value
Me.Invalidate()
End Set
End Property

''' <summary>
''' 启动状态
''' </summary>
''' <remarks></remarks>
Private mvarStartState = True
Public Property StartState() As Boolean
Get
Return mvarStartState
End Get
Set(ByVal value As Boolean)
mvarStartState = value
Me.Invalidate()
End Set
End Property

''' <summary>
''' 灯的输入字符,表示灯的输入状态
''' </summary>
''' <remarks></remarks>
Private mvarIOin As String = "0F"
<Browsable(True)> _
Public Property IOin() As String
Get
Return mvarIOin
End Get
Set(ByVal value As String)
mvarIOin = value
End Set
End Property

''' <summary>
''' 灯的输出字符,表示灯的输出状态
''' </summary>
''' <remarks></remarks>
Private mvarIOout As String = "00"
<Browsable(True)> _
Public Property IOout() As String
Get
Return mvarIOout
End Get
Set(ByVal value As String)
mvarIOout = value
End Set
End Property

''' <summary>
''' 灯的通道数量
''' </summary>
''' <remarks></remarks>
Private mavChannelNum As Integer = 3
<Browsable(True)> _
Public Property ChannelNum() As Integer
Get
Return mavChannelNum
End Get
Set(ByVal value As Integer)
mavChannelNum = value
Me.Invalidate()
End Set

End Property

''' <summary>
''' 灯塔值
''' </summary>
''' <remarks></remarks>
Private mvarValue As TwoLedState = TwoLedState.None_Led
Public Property LedValue() As TwoLedState
Get
Return mvarValue
End Get
Set(ByVal mValue As TwoLedState)
mvarValue = mValue
If mValue = 0 Then
Me.Image = My.Resources._0_0_0 'XF
ElseIf mValue = 1 Then
Me.Image = My.Resources._0_0_1 'XB
ElseIf mValue = 2 Then
Me.Image = My.Resources._0_1_0 'XD
ElseIf mValue = 3 Then
Me.Image = My.Resources._0_1_1 'X9
ElseIf mValue = 4 Then
Me.Image = My.Resources._1_0_0 'XE
ElseIf mValue = 5 Then
Me.Image = My.Resources._1_0_1 'XA
ElseIf mValue = 6 Then
Me.Image = My.Resources._1_1_0 'XC
ElseIf mValue = 7 Then
Me.Image = My.Resources._1_1_1 'X08
ElseIf mValue = 8 Then
Me.Image = My.Resources.x_x_x
End If
If mValue <> TwoLedState.None_Led And Not AddStated Then
AddData()
AddStated = True
SaveStated = False
End If
If mValue = TwoLedState.None_Led And Not SaveStated And AddStated Then
SaveData()
SaveStated = True
AddStated = False
End If
Me.Invalidate()
End Set
End Property

'添加提示信息
Private Sub TwoLed_MouseHover(ByVal sender As Object,ByVal e As System.EventArgs) Handles Me.MouseHover
Dim mTooltip As New ToolTip
If mvarAddress > 0 Then
mTooltip.SetToolTip(Me,"标题:" & mvarTitle.ToString & "通道号:" & mavChannelNum.ToString)
End If
End Sub

'Private Sub TwoLed_MouseDoubleClick(ByVal sender As Object,ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseDoubleClick
' If Me.Enabled Then
' Dim frm As New FrmLedKZ
' ' frm.Text = "地址:" & Me.Address ' mvarAddress
' frm.TwoLed1 = Me 'me的引用最为关键呀
' frm.Text = Address
' frm.ShowDialog()
' Else
' MsgBox("系统无电或信号不正常!,请检查")
' End If
'End Sub


Private Sub MyPic_Paint(ByVal sender As Object,ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
Me.SizeMode = PictureBoxSizeMode.StretchImage
End Sub



Public Sub New()

' 此调用是 Windows 窗体设计器所必需的。
InitializeComponent()
Me.Image = My.Resources._0_0_0

'此处为透明背景
Me.BackColor = Color.Transparent
' 在 InitializeComponent() 调用之后添加任何初始化。

End Sub

Private Sub AddData()

Dim Myfun As New ClsTbLedOpenTime
If Myfun.ExistBy(Me.Title,1) Then
Myfun.ModifyOpenLedTime(Me.Title,1)
Else
Myfun.AddOpenLedTime(Me.Title,1)
End If

Myfun = Nothing
End Sub


Private Sub SaveData()
Dim Mydata As New tbLedTimed
Dim Myfun1 As New ClsTbLedOpenTime
Dim Myfun As New ClsTbLedTimed
Dim TmpDate1 As Date

TmpDate1 = Myfun1.ReadLedOpenTime(Me.Title)

Mydata.灯名称 = Me.Title
Mydata.关灯时间 = Now
Mydata.开灯时间 = TmpDate1
Mydata.合计时间 = DateDiff(DateInterval.Minute,TmpDate1,Now)

Myfun.AddOpenLedTime(Mydata)

End Sub

End Class

原文链接:https://www.f2er.com/vb/261192.html

猜你在找的VB相关文章