成人午夜视频全免费观看高清-秋霞福利视频一区二区三区-国产精品久久久久电影小说-亚洲不卡区三一区三区一区

包含vb.net多級委托的詞條

vb.net 中在模塊(module)里如何實現(xiàn)委托

委托三個步驟

成都創(chuàng)新互聯(lián)公司主要從事網(wǎng)站設計制作、成都做網(wǎng)站、網(wǎng)頁設計、企業(yè)做網(wǎng)站、公司建網(wǎng)站等業(yè)務。立足成都服務永康,10余年網(wǎng)站建設經(jīng)驗,價格優(yōu)惠、服務專業(yè),歡迎來電咨詢建站服務:18982081108

1、聲明委托 用Delegate 聲明一個委托 類型 參數(shù)要和 被委托的方法一樣 例如 Delegate Function a(byval x as string) as string

2、實例化委托 dim t as new a(AddressOf Function Name)

3.通過 t(參數(shù)) 或者 t.Invoke(參數(shù)調(diào)用委托)

示例:

Module module1

Delegate Function a(ByVal x As Integer, ByVal y As Integer) As Integer '聲明委托類型 委托可以使一個對象調(diào)用另一個對象的方法

Function sum(ByVal x As Integer, ByVal y As Integer) As Integer

Return (x + y)

End Function

Sub main()

Dim d As New a(AddressOf sum) '實例化委托

Dim s = 0

s = d.Invoke(1, 2) '執(zhí)行委托

Console.WriteLine(s.ToString())

s = d(1, 2) '執(zhí)行委托

Console.WriteLine(s.ToString())

MsgBox("")

End Sub

End Module

VB.NET 中事件委托問題

address of 顧名思義,就是地址指向,每個函數(shù)都有一個地址,address of后面加函數(shù)名稱。

handels 事件,你看看按鈕的單擊事件,窗體的啟動事件,每個后面都有這個。

delegate 就是聲明一個委托了。

我也不好詳細說,其實你上Baidu搜這幾個關(guān)鍵字加上點注解,比如“delegate的用法”,N多!

VB.NET中的多線程和委托是什么關(guān)系? 能不能給我一個通俗易懂的范例,謝謝

委托,Delegate

就是讓你處于這個線程里時,委托另一個線程去執(zhí)行一些動作

我簡單舉一個寫richtextbox的例子:

////////////////////////////////////////////

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

Delegate Sub MySubDelegate(ByVal txt As String)

'寫信息到富文本主窗口

Private Sub txtW(ByVal txt As String)

Dim msgd As New MySubDelegate(AddressOf Me.txtW1)

Dim arg(0) As Object

arg(0) = txt

Me.Invoke(msgd, arg)

End Sub

'委托指向

Private Sub txtW1(ByVal txt As String)

Me.RichTextBox1.AppendText(txt)

End Sub

/////////////////////

這樣,你在多線程應用時,在其他線程里用txtW(str)來寫richtextbox,就不會產(chǎn)生錯誤了。不然,直接垮線程寫richtextbox,可能會出現(xiàn)和UI線程的沖突。

關(guān)于vb.net中實現(xiàn)委托的一個問題

把UI對象當做參數(shù)傳入 比如 Sub a(Form As Form1)

之后再用Form.Invoke(你的委托,參數(shù)) 就可以即時修改Form中的內(nèi)容了

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 方法設置為處理 AlarmClock 的 Alarm 事件。

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

示例

' 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多級委托的詞條
分享URL:http://jinyejixie.com/article24/docpdce.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站設計公司、網(wǎng)站策劃定制開發(fā)、云服務器移動網(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)

h5響應式網(wǎng)站建設
武城县| 桐城市| 汤阴县| 石渠县| 津南区| 沭阳县| 乐清市| 昌图县| 福海县| 武功县| 宜宾县| 扶绥县| 罗山县| 南乐县| 开封县| 淳安县| 平利县| 乐清市| 澜沧| 丰顺县| 建阳市| 左权县| 延边| 沂源县| 北川| 兴国县| 渭源县| 开化县| 思茅市| 长治市| 贵州省| 彰化县| 永嘉县| 遂昌县| 孙吴县| 徐州市| 桓台县| 寿宁县| 句容市| 梅河口市| 孝感市|