
正文
vb.net委托回调 vb返回代码
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
vb.net 如何用进度条显示数据查询进度?
有不是多线程的,.NET有一个异步控件来完成这个事情,BackgroundWorker。
多线程其实比你想象的简单,你计划操作步骤,打比方说我们处理一个文件,用进度条来显示进度。我们可以定义解压他进度为5%,解密文件后哦进度完成20%,读取数据后进度完成30%,重新整理插入数据后进度完成60%,再加密,进度为80%,再压缩进度为90%,再发送进度为95%,删除历史记录结束动作进度为100%。这种事就要多线程去完成,调用控件你就需要委托或者回调控件。
进度条给他赋值就好了,程序运行后你会发现进度条在动。
这是现在90%程序进度条原理。
相关问答
Q1: vb的回调函数转为vb.NET
先声明一个委托:
Public Delegate Function EnumChildProc(hwnd As Integer, iParam As Integer) As Integer
然后才能使用回调函数,回调函数的参数要和委托完全一致。
然后那个 API 函数需要这样声明:
Declare Function EnumChildWindows Lib "user32" (hWndParent As Integer, lpEnumFunc As EnumChildProc, lParam As Integer) As Integer
Q2: vb 回调函数 详解
所谓回调函数,也就是你调用的那个函数内部,执行你传给他的你自己的函数。
比如有个一函数是 A,现在调用A并且把B函数(或者C函数D函数等任意一个函数)当参数传给A,在A的内部执行你传给他的函数,其实函数本身也可以当参数传递。
以一个简单的例子
'---------------------------窗口代码
Private Sub Form_Load()
Call A(AddressOf B)
Call A(AddressOf C)
Call A(AddressOf D)
'可以传递任意一个函数作为参数传递给A,并且在A函数中执行作为参数传递进来的函数
End Sub
'---------------------------模块代码
Declare Function CallWindowProc Lib "User32" Alias "CallWindowProcA" (ByVal lpPrevWndFunc As Long, ByVal hWnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Public Sub A(FunAddress As Long)
CallWindowProc FunAddress, 0, 0, 0, 0
End Sub
Public Sub B()
MsgBox "BBBB"
End Sub
Public Sub C()
MsgBox "CCCC"
End Sub
Public Sub D()
MsgBox "DDDD"
End Sub
Q3: vb.net中如何用事件和委托,会C#中的事件和委托,但不知VB.net中的语法,望给个简单的例子熟悉语法。
一委托:此示例演示如何将方法与委托关联然后通过委托调用该方法。
创建委托和匹配过程
创建一个名为 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
定义一个方法,该方法创建该委托的实例并通过调用内置的 Invoke 方法调用与该委托关联的方法。
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
二、事件
下面的示例程序阐释如何在一个类中引发一个事件,然后在另一个类中处理该事件。AlarmClock 类定义公共事件 Alarm,并提供引发该事件的方法。AlarmEventArgs 类派生自 EventArgs,并定义 Alarm 事件特定的数据。WakeMeUp 类定义处理 Alarm 事件的 AlarmRang 方法。AlarmDriver 类一起使用类,将使用 WakeMeUp 的 AlarmRang 方法设置为处理 AlarmClock 的 Alarm 事件。
该示例程序使用事件和委托和引发事件中详细说明的概念。
示例
' 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
Q4: vb.net 中在模块(module)里如何实现委托
委托三个步骤
1、声明委托 用Delegate 声明一个委托 类型 参数要和 被委托的方法一样 例如 Delegate Function a(byval x as string) as string
2、实例化委托 dim t as new a(AddressOf Function Name)
3.通过 t(参数) 或者 t.Invoke(参数调用委托)
示例:
Module module1
Delegate Function a(ByVal x As Integer, ByVal y As Integer) As Integer '声明委托类型 委托可以使一个对象调用另一个对象的方法
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) '执行委托
Console.WriteLine(s.ToString())
s = d(1, 2) '执行委托
Console.WriteLine(s.ToString())
MsgBox("")
End Sub
End Module
关于vb.net委托回调和vb返回代码的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。







