vb.net关联事件的简单介绍

VB.NET 事件的含义

TextBox1_TextChanged() 'TextBox1.text属性改变时发生

创新互联建站专注为客户提供全方位的互联网综合服务,包含不限于网站建设、网站制作、新荣网络推广、微信小程序、新荣网络营销、新荣企业策划、新荣品牌公关、搜索引擎seo、人物专访、企业宣传片、企业代运营等,从售前售中售后,我们都将竭诚为您服务,您的肯定,是我们最大的嘉奖;创新互联建站为所有大学生创业者提供新荣建站搭建服务,24小时服务热线:18982081108,官方网址:www.cdcxhl.com

Label1_Click() 'Label1被鼠标点击时发生

MenuItem1_Click() 'MenuItem1被鼠标点击时发生

Label1_MouseDown() '鼠标左键在Label1上按下时发生

Label1_DoubleClick() '有点难我也不太清楚,在MSDN上查了下:

双击操作由用户操作系统的鼠标设置确定。用户可以设置两次单击鼠标按钮之间的时间以便将这两次单击认为是双击而不是两次单击。每当双击控件时,就会引发 Click 事件。例如,如果您有 Form 的 Click 和 DoubleClick 事件的事件处理程序,则当双击该窗体并同时调用这两个方法时,会引发 Click 和 DoubleClick 事件。如果双击一个控件并且该控件不支持 DoubleClick 事件,则 Click 事件可能被引发两次。

Label1_MouseUp() '鼠标左键在Label1上放开时发生,一般与Label1_MouseDown()搭配使用

TextBox2_MouseMove() '鼠标停留在TextBox2上时发生

Form1_load() '加载窗体时发生

Form1_click() '点击窗体时发生

Form1_Resize() '窗体调整大小后发生

Form1_KeyPress() '当窗体有焦点键盘有操作时发生

Form1_KeyDown() '当窗体具有焦点并键盘有按键按下时发生

Form1_KeyUp() '当窗体焦点并键盘有按键放开时发生

vb.net 剪切控件再粘贴时控件对应的代码无效

这反而是vb.net智能的地方,删除后自动删除关联事件。在C#里不会自动删除关联,导致很多初学者移除了控件就运行不了了。其实平时我们很少使用剪切粘贴的方式。如果想跨项目应用窗体设计,只需要把相关的3个文件(以Formxxx开头,vb结尾)同时添加到目标项目即可。

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

vb.net中文本框怎么从下拉列表框中调用数据,相关联

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

那同样可以关联,和这个思路反过来,只是事件不是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当中,事件的"关联"是需要明显标志的,不像vb6当中,声明一个过程就是事件执行过程了

Handles用来静态"关联"一个或多个事件到一个过程

"关联"时,过程的签名必须与事件的签名相同(签名的意义请查看相关文档)

在VB.Net当中,事件也是一个对象(VB.Net当中一切皆为对象)

使用Handles时实际就相当于创建了一个对委托的实现(委托的意义请查看相关文档)

可以简单的认为,当事件被触发时,被Handles"关联"了的那些过程代码将被执行

而且,被"关联"的过程可以任意起名,不需要与事件和对象的名称相对应

上述代码如果去掉Handles 及其后面的内容,它将是一个最普通不过的过程,与其它过程没有两样,也不会有任何事件被触发时去执行这段代码,正因为加入了Handles 及其后面的一堆事件,它才会因事件被触发而被执行

说得再简单一点: 当PictureBox1.DoubleClick, PictureBox4.DoubleClick, PictureBox3.DoubleClick,PictureBox2.DoubleClick当中的任何一个事件被触发时,上述这个过程的代码都将被执行

再说说参数: sender表示触发了此事件的对象,在这里就是PictureBox1/PictureBox2/PictureBox3/PictureBox4当中的某一个,利用它能知道到底是哪个对象触发了此事件,e在这里没有用处,利用不到什么,之所以有它,是因为Object/EventArgs是.Net事件的基本签名方式,它的好处在你以后对.Net深入之后能体会到.

这些参数的值都是通过被触发的事件传递过来的,可以简单的认为[事件调用了此过程并为参数赋了值]


本文标题:vb.net关联事件的简单介绍
文章URL:http://myzitong.com/article/hgeggg.html