TextBox1_TextChanged() 'TextBox1.text屬性改變時(shí)發(fā)生
創(chuàng)新互聯(lián)建站專注為客戶提供全方位的互聯(lián)網(wǎng)綜合服務(wù),包含不限于網(wǎng)站建設(shè)、網(wǎng)站制作、新榮網(wǎng)絡(luò)推廣、微信小程序、新榮網(wǎng)絡(luò)營(yíng)銷、新榮企業(yè)策劃、新榮品牌公關(guān)、搜索引擎seo、人物專訪、企業(yè)宣傳片、企業(yè)代運(yùn)營(yíng)等,從售前售中售后,我們都將竭誠(chéng)為您服務(wù),您的肯定,是我們最大的嘉獎(jiǎng);創(chuàng)新互聯(lián)建站為所有大學(xué)生創(chuàng)業(yè)者提供新榮建站搭建服務(wù),24小時(shí)服務(wù)熱線:18982081108,官方網(wǎng)址:www.cdcxhl.com
?Label1_Click() 'Label1被鼠標(biāo)點(diǎn)擊時(shí)發(fā)生
?MenuItem1_Click() 'MenuItem1被鼠標(biāo)點(diǎn)擊時(shí)發(fā)生
?Label1_MouseDown() '鼠標(biāo)左鍵在Label1上按下時(shí)發(fā)生
?Label1_DoubleClick() '有點(diǎn)難我也不太清楚,在MSDN上查了下:
雙擊操作由用戶操作系統(tǒng)的鼠標(biāo)設(shè)置確定。用戶可以設(shè)置兩次單擊鼠標(biāo)按鈕之間的時(shí)間以便將這兩次單擊認(rèn)為是雙擊而不是兩次單擊。每當(dāng)雙擊控件時(shí),就會(huì)引發(fā) Click 事件。例如,如果您有 Form 的 Click 和 DoubleClick 事件的事件處理程序,則當(dāng)雙擊該窗體并同時(shí)調(diào)用這兩個(gè)方法時(shí),會(huì)引發(fā) Click 和 DoubleClick 事件。如果雙擊一個(gè)控件并且該控件不支持 DoubleClick 事件,則 Click 事件可能被引發(fā)兩次。
Label1_MouseUp() '鼠標(biāo)左鍵在Label1上放開時(shí)發(fā)生,一般與Label1_MouseDown()搭配使用
?TextBox2_MouseMove() '鼠標(biāo)停留在TextBox2上時(shí)發(fā)生
?Form1_load() '加載窗體時(shí)發(fā)生
?Form1_click() '點(diǎn)擊窗體時(shí)發(fā)生
?Form1_Resize() '窗體調(diào)整大小后發(fā)生
Form1_KeyPress() '當(dāng)窗體有焦點(diǎn)鍵盤有操作時(shí)發(fā)生
?Form1_KeyDown() '當(dāng)窗體具有焦點(diǎn)并鍵盤有按鍵按下時(shí)發(fā)生
?Form1_KeyUp() '當(dāng)窗體焦點(diǎn)并鍵盤有按鍵放開時(shí)發(fā)生
這反而是vb.net智能的地方,刪除后自動(dòng)刪除關(guān)聯(lián)事件。在C#里不會(huì)自動(dòng)刪除關(guān)聯(lián),導(dǎo)致很多初學(xué)者移除了控件就運(yùn)行不了了。其實(shí)平時(shí)我們很少使用剪切粘貼的方式。如果想跨項(xiàng)目應(yīng)用窗體設(shè)計(jì),只需要把相關(guān)的3個(gè)文件(以Formxxx開頭,vb結(jié)尾)同時(shí)添加到目標(biāo)項(xiàng)目即可。
一委托:此示例演示如何將方法與委托關(guān)聯(lián)然后通過委托調(diào)用該方法。
創(chuàng)建委托和匹配過程
創(chuàng)建一個(gè)名為 MySubDelegate 的委托。
Delegate Sub MySubDelegate(ByVal x As Integer)
聲明一個(gè)類,該類包含與該委托具有相同簽名的方法。
Class class1
Sub Sub1(ByVal x As Integer)
MsgBox("The value of x is: " CStr(x))
End Sub
End Class
定義一個(gè)方法,該方法創(chuàng)建該委托的實(shí)例并通過調(diào)用內(nèi)置的 Invoke 方法調(diào)用與該委托關(guān)聯(lián)的方法。
Protected Sub DelegateTest()
Dim c1 As New class1
' Create an instance of the delegate.
Dim msd As MySubDelegate = AddressOf c1.Sub1
' Call the method.
msd.Invoke(10)
End Sub
二、事件
下面的示例程序闡釋如何在一個(gè)類中引發(fā)一個(gè)事件,然后在另一個(gè)類中處理該事件。AlarmClock 類定義公共事件 Alarm,并提供引發(fā)該事件的方法。AlarmEventArgs 類派生自 EventArgs,并定義 Alarm 事件特定的數(shù)據(jù)。WakeMeUp 類定義處理 Alarm 事件的 AlarmRang 方法。AlarmDriver 類一起使用類,將使用 WakeMeUp 的 AlarmRang 方法設(shè)置為處理 AlarmClock 的 Alarm 事件。
該示例程序使用事件和委托和引發(fā)事件中詳細(xì)說(shuō)明的概念。
示例
' EventSample.vb.
'
Option Explicit
Option Strict
Imports System
Imports System.ComponentModel
Imports Microsoft.VisualBasic
Namespace EventSample
' Class that contains the data for
' the alarm event. Derives from System.EventArgs.
'
Public Class AlarmEventArgs
Inherits EventArgs
Private _snoozePressed As Boolean
Private nrings As Integer
'Constructor.
'
Public Sub New(snoozePressed As Boolean, nrings As Integer)
Me._snoozePressed = snoozePressed
Me.nrings = nrings
End Sub
' The NumRings property returns the number of rings
' that the alarm clock has sounded when the alarm event
' is generated.
'
Public ReadOnly Property NumRings() As Integer
Get
Return nrings
End Get
End Property
' The SnoozePressed property indicates whether the snooze
' button is pressed on the alarm when the alarm event is generated.
'
Public ReadOnly Property SnoozePressed() As Boolean
Get
Return _snoozePressed
End Get
End Property
' The AlarmText property that contains the wake-up message.
'
Public ReadOnly Property AlarmText() As String
Get
If _snoozePressed Then
Return "Wake Up!!! Snooze time is over."
Else
Return "Wake Up!"
End If
End Get
End Property
End Class
' Delegate declaration.
'
Public Delegate Sub AlarmEventHandler(sender As Object, _
e As AlarmEventArgs)
' The Alarm class that raises the alarm event.
'
Public Class AlarmClock
Private _snoozePressed As Boolean = False
Private nrings As Integer = 0
Private stopFlag As Boolean = False
' The Stop property indicates whether the
' alarm should be turned off.
'
Public Property [Stop]() As Boolean
Get
Return stopFlag
End Get
Set
stopFlag = value
End Set
End Property
' The SnoozePressed property indicates whether the snooze
' button is pressed on the alarm when the alarm event is generated.
'
Public Property SnoozePressed() As Boolean
Get
Return _snoozePressed
End Get
Set
_snoozePressed = value
End Set
End Property
' The event member that is of type AlarmEventHandler.
'
Public Event Alarm As AlarmEventHandler
' The protected OnAlarm method raises the event by invoking
' the delegates. The sender is always this, the current instance
' of the class.
'
Protected Overridable Sub OnAlarm(e As AlarmEventArgs)
RaiseEvent Alarm(Me, e)
End Sub
' This alarm clock does not have
' a user interface.
' To simulate the alarm mechanism it has a loop
' that raises the alarm event at every iteration
' with a time delay of 300 milliseconds,
' if snooze is not pressed. If snooze is pressed,
' the time delay is 1000 milliseconds.
'
Public Sub Start()
Do
nrings += 1
If stopFlag Then
Exit Do
Else
If _snoozePressed Then
System.Threading.Thread.Sleep(1000)
If (True) Then
Dim e As New AlarmEventArgs(_snoozePressed, nrings)
OnAlarm(e)
End If
Else
System.Threading.Thread.Sleep(300)
Dim e As New AlarmEventArgs(_snoozePressed, nrings)
OnAlarm(e)
End If
End If
Loop
End Sub
End Class
' The WakeMeUp class has a method AlarmRang that handles the
' alarm event.
'
Public Class WakeMeUp
Public Sub AlarmRang(sender As Object, e As AlarmEventArgs)
Console.WriteLine((e.AlarmText + ControlChars.Cr))
If Not e.SnoozePressed Then
If e.NumRings Mod 10 = 0 Then
Console.WriteLine(" Let alarm ring? Enter Y")
Console.WriteLine(" Press Snooze? Enter N")
Console.WriteLine(" Stop Alarm? Enter Q")
Dim input As String = Console.ReadLine()
If input.Equals("Y") Or input.Equals("y") Then
Return
Else
If input.Equals("N") Or input.Equals("n") Then
CType(sender, AlarmClock).SnoozePressed = True
Return
Else
CType(sender, AlarmClock).Stop = True
Return
End If
End If
End If
Else
Console.WriteLine(" Let alarm ring? Enter Y")
Console.WriteLine(" Stop Alarm? Enter Q")
Dim input As String = Console.ReadLine()
If input.Equals("Y") Or input.Equals("y") Then
Return
Else
CType(sender, AlarmClock).Stop = True
Return
End If
End If
End Sub
End Class
' The driver class that hooks up the event handling method of
' WakeMeUp to the alarm event of an Alarm object using a delegate.
' In a forms-based application, the driver class is the
' form.
'
Public Class AlarmDriver
Public Shared Sub Main()
' Instantiates the event receiver.
Dim w As New WakeMeUp()
' Instantiates the event source.
Dim clock As New AlarmClock()
' Wires the AlarmRang method to the Alarm event.
AddHandler clock.Alarm, AddressOf w.AlarmRang
clock.Start()
End Sub
End Class
End Namespace
Private Sub Text1_KeyUp(KeyCode As Integer, Shift As Integer)
If Text1.Text = "1" Then Combo1.Text = "增加"
If Text1.Text = "2" Then Combo1.Text = "修改"
If Text1.Text = "3" Then Combo1.Text = "刪除"
End Sub
那同樣可以關(guān)聯(lián),和這個(gè)思路反過來(lái),只是事件不是KeyUp,而是combo的change過程
Private Sub Combo1_Change()
select case combo1.text
case "增加"
text1.text=1
case "修改"
text1.text=2
case "刪除"
text1.text=3
case else
text1.text=""
end select
End Sub
在VB.Net當(dāng)中,事件的"關(guān)聯(lián)"是需要明顯標(biāo)志的,不像vb6當(dāng)中,聲明一個(gè)過程就是事件執(zhí)行過程了
Handles用來(lái)靜態(tài)"關(guān)聯(lián)"一個(gè)或多個(gè)事件到一個(gè)過程
"關(guān)聯(lián)"時(shí),過程的簽名必須與事件的簽名相同(簽名的意義請(qǐng)查看相關(guān)文檔)
在VB.Net當(dāng)中,事件也是一個(gè)對(duì)象(VB.Net當(dāng)中一切皆為對(duì)象)
使用Handles時(shí)實(shí)際就相當(dāng)于創(chuàng)建了一個(gè)對(duì)委托的實(shí)現(xiàn)(委托的意義請(qǐng)查看相關(guān)文檔)
可以簡(jiǎn)單的認(rèn)為,當(dāng)事件被觸發(fā)時(shí),被Handles"關(guān)聯(lián)"了的那些過程代碼將被執(zhí)行
而且,被"關(guān)聯(lián)"的過程可以任意起名,不需要與事件和對(duì)象的名稱相對(duì)應(yīng)
上述代碼如果去掉Handles 及其后面的內(nèi)容,它將是一個(gè)最普通不過的過程,與其它過程沒有兩樣,也不會(huì)有任何事件被觸發(fā)時(shí)去執(zhí)行這段代碼,正因?yàn)榧尤肓薍andles 及其后面的一堆事件,它才會(huì)因事件被觸發(fā)而被執(zhí)行
說(shuō)得再簡(jiǎn)單一點(diǎn): 當(dāng)PictureBox1.DoubleClick, PictureBox4.DoubleClick, PictureBox3.DoubleClick,PictureBox2.DoubleClick當(dāng)中的任何一個(gè)事件被觸發(fā)時(shí),上述這個(gè)過程的代碼都將被執(zhí)行
再說(shuō)說(shuō)參數(shù): sender表示觸發(fā)了此事件的對(duì)象,在這里就是PictureBox1/PictureBox2/PictureBox3/PictureBox4當(dāng)中的某一個(gè),利用它能知道到底是哪個(gè)對(duì)象觸發(fā)了此事件,e在這里沒有用處,利用不到什么,之所以有它,是因?yàn)镺bject/EventArgs是.Net事件的基本簽名方式,它的好處在你以后對(duì).Net深入之后能體會(huì)到.
這些參數(shù)的值都是通過被觸發(fā)的事件傳遞過來(lái)的,可以簡(jiǎn)單的認(rèn)為[事件調(diào)用了此過程并為參數(shù)賦了值]