vb.net關(guān)聯(lián)事件的簡單介紹

VB.NET 事件的含義

TextBox1_TextChanged() 'TextBox1.text屬性改變時發(fā)生

創(chuàng)新互聯(lián)建站專注為客戶提供全方位的互聯(lián)網(wǎng)綜合服務(wù),包含不限于網(wǎng)站建設(shè)、網(wǎng)站制作、新榮網(wǎng)絡(luò)推廣、微信小程序、新榮網(wǎng)絡(luò)營銷、新榮企業(yè)策劃、新榮品牌公關(guān)、搜索引擎seo、人物專訪、企業(yè)宣傳片、企業(yè)代運(yùn)營等,從售前售中售后,我們都將竭誠為您服務(wù),您的肯定,是我們最大的嘉獎;創(chuàng)新互聯(lián)建站為所有大學(xué)生創(chuàng)業(yè)者提供新榮建站搭建服務(wù),24小時服務(wù)熱線:18982081108,官方網(wǎng)址:muchs.cn

?Label1_Click() 'Label1被鼠標(biāo)點擊時發(fā)生

?MenuItem1_Click() 'MenuItem1被鼠標(biāo)點擊時發(fā)生

?Label1_MouseDown() '鼠標(biāo)左鍵在Label1上按下時發(fā)生

?Label1_DoubleClick() '有點難我也不太清楚,在MSDN上查了下:

雙擊操作由用戶操作系統(tǒng)的鼠標(biāo)設(shè)置確定。用戶可以設(shè)置兩次單擊鼠標(biāo)按鈕之間的時間以便將這兩次單擊認(rèn)為是雙擊而不是兩次單擊。每當(dāng)雙擊控件時,就會引發(fā) Click 事件。例如,如果您有 Form 的 Click 和 DoubleClick 事件的事件處理程序,則當(dāng)雙擊該窗體并同時調(diào)用這兩個方法時,會引發(fā) Click 和 DoubleClick 事件。如果雙擊一個控件并且該控件不支持 DoubleClick 事件,則 Click 事件可能被引發(fā)兩次。

Label1_MouseUp() '鼠標(biāo)左鍵在Label1上放開時發(fā)生,一般與Label1_MouseDown()搭配使用

?TextBox2_MouseMove() '鼠標(biāo)停留在TextBox2上時發(fā)生

?Form1_load() '加載窗體時發(fā)生

?Form1_click() '點擊窗體時發(fā)生

?Form1_Resize() '窗體調(diào)整大小后發(fā)生

Form1_KeyPress() '當(dāng)窗體有焦點鍵盤有操作時發(fā)生

?Form1_KeyDown() '當(dāng)窗體具有焦點并鍵盤有按鍵按下時發(fā)生

?Form1_KeyUp() '當(dāng)窗體焦點并鍵盤有按鍵放開時發(fā)生

vb.net 剪切控件再粘貼時控件對應(yīng)的代碼無效

這反而是vb.net智能的地方,刪除后自動刪除關(guān)聯(lián)事件。在C#里不會自動刪除關(guān)聯(lián),導(dǎo)致很多初學(xué)者移除了控件就運(yùn)行不了了。其實平時我們很少使用剪切粘貼的方式。如果想跨項目應(yīng)用窗體設(shè)計,只需要把相關(guān)的3個文件(以Formxxx開頭,vb結(jié)尾)同時添加到目標(biāo)項目即可。

vb.net中如何用事件和委托,會C#中的事件和委托,但不知VB.net中的語法,望給個簡單的例子熟悉語法。

一委托:此示例演示如何將方法與委托關(guān)聯(lián)然后通過委托調(diào)用該方法。

創(chuàng)建委托和匹配過程

創(chuàng)建一個名為 MySubDelegate 的委托。

Delegate Sub MySubDelegate(ByVal x As Integer)

聲明一個類,該類包含與該委托具有相同簽名的方法。

Class class1

Sub Sub1(ByVal x As Integer)

MsgBox("The value of x is: " CStr(x))

End Sub

End Class

定義一個方法,該方法創(chuàng)建該委托的實例并通過調(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

二、事件

下面的示例程序闡釋如何在一個類中引發(fā)一個事件,然后在另一個類中處理該事件。AlarmClock 類定義公共事件 Alarm,并提供引發(fā)該事件的方法。AlarmEventArgs 類派生自 EventArgs,并定義 Alarm 事件特定的數(shù)據(jù)。WakeMeUp 類定義處理 Alarm 事件的 AlarmRang 方法。AlarmDriver 類一起使用類,將使用 WakeMeUp 的 AlarmRang 方法設(shè)置為處理 AlarmClock 的 Alarm 事件。

該示例程序使用事件和委托和引發(fā)事件中詳細(xì)說明的概念。

示例

' 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

vb.net中文本框怎么從下拉列表框中調(diào)用數(shù)據(jù),相關(guān)聯(lián)

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),和這個思路反過來,只是事件不是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高手請幫幫我看看下面這代碼什么意思,詳解哦,先謝了,Handles 作用是什么

在VB.Net當(dāng)中,事件的"關(guān)聯(lián)"是需要明顯標(biāo)志的,不像vb6當(dāng)中,聲明一個過程就是事件執(zhí)行過程了

Handles用來靜態(tài)"關(guān)聯(lián)"一個或多個事件到一個過程

"關(guān)聯(lián)"時,過程的簽名必須與事件的簽名相同(簽名的意義請查看相關(guān)文檔)

在VB.Net當(dāng)中,事件也是一個對象(VB.Net當(dāng)中一切皆為對象)

使用Handles時實際就相當(dāng)于創(chuàng)建了一個對委托的實現(xiàn)(委托的意義請查看相關(guān)文檔)

可以簡單的認(rèn)為,當(dāng)事件被觸發(fā)時,被Handles"關(guān)聯(lián)"了的那些過程代碼將被執(zhí)行

而且,被"關(guān)聯(lián)"的過程可以任意起名,不需要與事件和對象的名稱相對應(yīng)

上述代碼如果去掉Handles 及其后面的內(nèi)容,它將是一個最普通不過的過程,與其它過程沒有兩樣,也不會有任何事件被觸發(fā)時去執(zhí)行這段代碼,正因為加入了Handles 及其后面的一堆事件,它才會因事件被觸發(fā)而被執(zhí)行

說得再簡單一點: 當(dāng)PictureBox1.DoubleClick, PictureBox4.DoubleClick, PictureBox3.DoubleClick,PictureBox2.DoubleClick當(dāng)中的任何一個事件被觸發(fā)時,上述這個過程的代碼都將被執(zhí)行

再說說參數(shù): sender表示觸發(fā)了此事件的對象,在這里就是PictureBox1/PictureBox2/PictureBox3/PictureBox4當(dāng)中的某一個,利用它能知道到底是哪個對象觸發(fā)了此事件,e在這里沒有用處,利用不到什么,之所以有它,是因為Object/EventArgs是.Net事件的基本簽名方式,它的好處在你以后對.Net深入之后能體會到.

這些參數(shù)的值都是通過被觸發(fā)的事件傳遞過來的,可以簡單的認(rèn)為[事件調(diào)用了此過程并為參數(shù)賦了值]

網(wǎng)頁名稱:vb.net關(guān)聯(lián)事件的簡單介紹
網(wǎng)頁網(wǎng)址:http://muchs.cn/article16/hgeggg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)頁設(shè)計公司、品牌網(wǎng)站設(shè)計、服務(wù)器托管、外貿(mào)網(wǎng)站建設(shè)、關(guān)鍵詞優(yōu)化、企業(yè)網(wǎng)站制作

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

綿陽服務(wù)器托管