單擊按鈕,或在文本框中輸入某些文本,或單擊菜單項(xiàng),都是事件的示例。 事件是調(diào)用函數(shù)或可能導(dǎo)致另一個(gè)事件的操作。
事件處理程序是指示如何響應(yīng)事件的函數(shù)。
鼠標(biāo)事件 Mouse events
鍵盤(pán)事件 Keyboard events
鼠標(biāo)事件發(fā)生與鼠標(biāo)移動(dòng)形式和控件。以下是與Control類(lèi)相關(guān)的各種鼠標(biāo)事件:
MouseDown -當(dāng)按下鼠標(biāo)按鈕時(shí)發(fā)生
MouseEnter -當(dāng)鼠標(biāo)指針進(jìn)入控件時(shí)發(fā)生
MouseHover -當(dāng)鼠標(biāo)指針懸停在控件上時(shí)發(fā)生
MouseLeave -鼠標(biāo)指針離開(kāi)控件時(shí)發(fā)生
MouseMove -當(dāng)鼠標(biāo)指針移動(dòng)到控件上時(shí)
MouseUp -當(dāng)鼠標(biāo)指針在控件上方并且鼠標(biāo)按鈕被釋放時(shí)發(fā)生
MouseWheel -它發(fā)生在鼠標(biāo)滾輪移動(dòng)和控件有焦點(diǎn)時(shí)
鼠標(biāo)事件的事件處理程序獲得一個(gè)類(lèi)型為MouseEventArgs的參數(shù)。 MouseEventArgs對(duì)象用于處理鼠標(biāo)事件。它具有以下屬性:
Buttons-表示按下鼠標(biāo)按鈕
Clicks-顯示點(diǎn)擊次數(shù)
Delta-表示鼠標(biāo)輪旋轉(zhuǎn)的定位槽的數(shù)量
X -指示鼠標(biāo)點(diǎn)擊的x坐標(biāo)
Y -表示鼠標(biāo)點(diǎn)擊的y坐標(biāo)
下面是一個(gè)例子,它展示了如何處理鼠標(biāo)事件。執(zhí)行以下步驟:
在表單中添加三個(gè)標(biāo)簽,三個(gè)文本框和一個(gè)按鈕控件。
將標(biāo)簽的文本屬性分別更改為 - 客戶(hù)ID,名稱(chēng)和地址。
將文本框的名稱(chēng)屬性分別更改為txtID,txtName和txtAddress。
將按鈕的文本屬性更改為“提交”。
在代碼編輯器窗口中添加以下代碼:
Public Class Form1 Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load ' Set the caption bar text of the form. Me.Text = "tutorialspont.com" End Sub Private Sub txtID_MouseEnter(sender As Object, e As EventArgs)_ Handles txtID.MouseEnter 'code for handling mouse enter on ID textbox txtID.BackColor = Color.CornflowerBlue txtID.ForeColor = Color.White End Sub Private Sub txtID_MouseLeave(sender As Object, e As EventArgs) _ Handles txtID.MouseLeave 'code for handling mouse leave on ID textbox txtID.BackColor = Color.White txtID.ForeColor = Color.Blue End Sub Private Sub txtName_MouseEnter(sender As Object, e As EventArgs) _ Handles txtName.MouseEnter 'code for handling mouse enter on Name textbox txtName.BackColor = Color.CornflowerBlue txtName.ForeColor = Color.White End Sub Private Sub txtName_MouseLeave(sender As Object, e As EventArgs) _ Handles txtName.MouseLeave 'code for handling mouse leave on Name textbox txtName.BackColor = Color.White txtName.ForeColor = Color.Blue End Sub Private Sub txtAddress_MouseEnter(sender As Object, e As EventArgs) _ Handles txtAddress.MouseEnter 'code for handling mouse enter on Address textbox txtAddress.BackColor = Color.CornflowerBlue txtAddress.ForeColor = Color.White End Sub Private Sub txtAddress_MouseLeave(sender As Object, e As EventArgs) _ Handles txtAddress.MouseLeave 'code for handling mouse leave on Address textbox txtAddress.BackColor = Color.White txtAddress.ForeColor = Color.Blue End Sub Private Sub Button1_Click(sender As Object, e As EventArgs) _ Handles Button1.Click MsgBox("Thank you " & txtName.Text & ", for your kind cooperation") End Sub End Class
當(dāng)使用Microsoft Visual Studio工具欄上的“開(kāi)始”按鈕執(zhí)行并運(yùn)行上述代碼時(shí),將顯示以下窗口:
嘗試在文本框中輸入文字,并檢查鼠標(biāo)事件:
以下是與Control類(lèi)相關(guān)的各種鍵盤(pán)事件:
KeyDown -當(dāng)按下一個(gè)鍵并且控件具有焦點(diǎn)時(shí)發(fā)生
KeyPress -當(dāng)按下一個(gè)鍵并且控件具有焦點(diǎn)時(shí)發(fā)生
KeyUp -當(dāng)控件具有焦點(diǎn)時(shí)釋放鍵時(shí)發(fā)生
KeyDown和KeyUp事件的事件處理程序獲得一個(gè)類(lèi)型為KeyEventArgs的參數(shù)。此對(duì)象具有以下屬性:
Alt -它指示是否按下ALT鍵/ p>
Control-它指示是否按下CTRL鍵
Handled-它指示事件是否被處理
KeyCode- 存儲(chǔ)事件的鍵盤(pán)代碼
KeyData-存儲(chǔ)事件的鍵盤(pán)數(shù)據(jù)
KeyValue -存儲(chǔ)事件的鍵盤(pán)值
Modifiers-指示按下哪個(gè)修飾鍵(Ctrl,Shift和/或Alt)
Shift-表示是否按下Shift鍵
KeyDown和KeyUp事件的事件處理程序獲得一個(gè)類(lèi)型為KeyEventArgs的參數(shù)。此對(duì)象具有以下屬性:
Handled-指示KeyPress事件處理
KeyChar -存儲(chǔ)對(duì)應(yīng)于按下的鍵的字符
讓我們繼續(xù)前面的例子來(lái)說(shuō)明如何處理鍵盤(pán)事件。 代碼將驗(yàn)證用戶(hù)為其客戶(hù)ID和年齡輸入一些數(shù)字。
添加一個(gè)帶有文本屬性為“Age”的標(biāo)簽,并添加一個(gè)名為txtAge的相應(yīng)文本框。
添加以下代碼以處理文本框txtID的KeyUP事件。
Private Sub txtID_KeyUP(sender As Object, e As KeyEventArgs) _ Handles txtID.KeyUp If (Not Char.IsNumber(ChrW(e.KeyCode))) Then MessageBox.Show("Enter numbers for your Customer ID") txtID.Text = " " End If End Sub
添加以下代碼以處理文本框txtID的KeyUP事件。
Private Sub txtAge_KeyUP(sender As Object, e As KeyEventArgs) _ Handles txtAge.KeyUp If (Not Char.IsNumber(ChrW(e.keyCode))) Then MessageBox.Show("Enter numbers for age") txtAge.Text = " " End If End Sub
當(dāng)使用Microsoft Visual Studio工具欄上的“開(kāi)始”按鈕執(zhí)行并運(yùn)行上述代碼時(shí),將顯示以下窗口:
如果將age或ID的文本留空,或輸入一些非數(shù)字?jǐn)?shù)據(jù),則會(huì)出現(xiàn)一個(gè)警告消息框,并清除相應(yīng)的文本:
更多建議: