你如果使用VB.NET,點(diǎn)擊其中一個(gè)“tab order”的圖標(biāo),此時(shí)您可以單擊每個(gè)控件的數(shù)字(越小越表示越容易被聚焦),NET默認(rèn)就可以用Tab進(jìn)行焦點(diǎn)切換(順序就是按照數(shù)字從小到大的順序)。 至于按Enter自動(dòng)切換,您需要自行在支持按鍵的控件事件中(比如TextBox)的KeyPress事件中編寫代碼: private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
創(chuàng)新互聯(lián)公司專業(yè)提供四川主機(jī)托管服務(wù),為用戶提供五星數(shù)據(jù)中心、電信、雙線接入解決方案,用戶可自行在線購買四川主機(jī)托管服務(wù),并享受7*24小時(shí)金牌售后服務(wù)。
{
if (e.KeyChar == 13)
{
XXX(控件).Focus();
}
}
最正統(tǒng)的方法是把TextBox1的TabIndex屬性設(shè)為0(其他控件的TabIndex不能是0)
要在Load事件里用TextBox1.Focus()也可以,但要先Me.Show(),否則不會(huì)起作用。因?yàn)榭丶玫浇裹c(diǎn)需要控件”可見可用“,在FormLoad階段整個(gè)窗體是“不可見"的,需要強(qiáng)制使用Me.Show()。
五.繪制個(gè)性化菜單
先執(zhí)行以下操作步驟 下列步驟是通過菜單編輯器設(shè)計(jì)一個(gè)簡單的菜單 為后面重新繪制做基礎(chǔ)
啟動(dòng)Visual Studio Net
選擇菜單【文件】|【新建】|【項(xiàng)目】后 彈出【新建項(xiàng)目】對(duì)話框
將【項(xiàng)目類型】設(shè)置為【Visual Basic項(xiàng)目】
將【模板】設(shè)置為【W(wǎng)indows應(yīng)用程序】
在【名稱】文本框中輸入【自己畫菜單】
在【位置】的文本框中輸入【E:\VS NET項(xiàng)目】 然后單擊【確定】按鈕 這樣在 E:\VS NET項(xiàng)目 目錄中就產(chǎn)生了名稱為 自己畫菜單 的文件夾 并在里面創(chuàng)建了名稱為 自己畫菜單 的項(xiàng)目文件
把Visual Studio Net的當(dāng)前窗口切換到【Form vb(設(shè)計(jì))】窗口 并從【工具箱】中的【W(wǎng)indows窗體組件】選項(xiàng)卡中往Form 窗體中拖入下列組件
一個(gè)MainMenu組件 名稱為 MainMenu
選中 MainMenu 組件 單擊鼠標(biāo)右鍵 在彈出的菜單中選擇 編輯菜單 并按照?qǐng)D 所示界面設(shè)計(jì)菜單
圖 【自己畫菜單】項(xiàng)目設(shè)計(jì)界面之一
此時(shí)保存上述步驟 并單擊快捷鍵F 則得到圖 所示界面
圖 【自己畫菜單】運(yùn)行界面之一
這樣通過菜單編輯器就完成了一個(gè)非常普通的菜單 下面就對(duì)此菜單進(jìn)行改造 在改造之前 要先設(shè)定項(xiàng)目中的三個(gè)MenuItem類實(shí)例的OwnerDraw屬性值為 True 因?yàn)橹挥写藢傩灾禐?True 才會(huì)觸發(fā)繪制菜單時(shí)所需要的DrawItem事件和MeasureItem事件 之后再在上面項(xiàng)目的基礎(chǔ)上執(zhí)行下一步操作
把Visual Stuido Net的當(dāng)前窗口切換到Form vb的代碼編輯窗口 并在InitializeComponent過程之后添加下列代碼 下列代碼是繪制 文件 菜單項(xiàng) 其作用是改變 文件 菜單項(xiàng)的字體 大小和菜單項(xiàng)的 其具體的繪制方法請(qǐng)參考下列代碼中的注釋
Private Sub MenuItem _DrawItem ( ByVal sender As Object ByVal e As System Windows Forms DrawItemEventArgs ) Handles MenuItem DrawItem Dim rfBound As RectangleF = New RectangleF ( e Bounds X e Bounds Y e Bounds Width e Bounds Height ) 根據(jù)DrawItemEventArgs參數(shù)獲得菜單項(xiàng)矩形區(qū)域并存儲(chǔ)到RectangleF類型實(shí)例中 Dim rfBound As Rectangle = New Rectangle ( e Bounds X e Bounds Y e Bounds Width e Bounds Height ) 根據(jù)DrawItemEventArgs參數(shù)獲得菜單項(xiàng)矩形區(qū)域并存儲(chǔ)到Rectangle類型實(shí)例中 Rectangle類型實(shí)例和RectangleF類型實(shí)例差不多 但在后面代碼中繪制菜單的函數(shù)是有區(qū)別的 e Graphics FillRectangle(New SolidBrush(Color LightGreen) rfBound) 以LightGreen色彩填充MenuItem 菜單項(xiàng)對(duì)應(yīng)的矩形區(qū)域 Dim s As MenuItem = CType ( sender MenuItem ) Dim s As String = s Text 獲得MenuItem 菜單項(xiàng)的名稱 Dim sfTemp As StringFormat = New StringFormat ( ) sfTemp Alignment = StringAlignment Center 設(shè)定要畫的菜單名稱的對(duì)齊方式 中間對(duì)齊 e Graphics DrawString ( s New Font ( 宋體 FontStyle Bold ) New SolidBrush ( Color Black ) rfBound sfTemp ) 以中間對(duì)齊方式 指定字體 大小 在指定的矩形區(qū)域重畫菜單 If e State = ( DrawItemState NoAccelerator Or DrawItemState Selected ) Then 根據(jù)菜單項(xiàng)的當(dāng)前繪制狀態(tài)來繪制菜單項(xiàng) e Graphics FillRectangle ( New SolidBrush ( Color LightYellow ) rfBound ) 對(duì)菜單項(xiàng)所在的矩形區(qū)域進(jìn)行色彩填充 e Graphics DrawString ( s New Font ( 宋體 FontStyle Bold ) New SolidBrush ( Color Black ) rfBound sfTemp ) 對(duì)菜單項(xiàng)名稱繪制 End If e DrawFocusRectangle ( ) 在 DrawItemEventArgs參數(shù)得到矩形范圍內(nèi)繪制聚焦框 e Graphics DrawRectangle ( New Pen ( New SolidBrush ( Color Black ) ) rfBound ) 對(duì)菜單項(xiàng)的矩形區(qū)域繪制矩形框End Sub
操作完成后 保存修改 此時(shí)再單擊快捷鍵F 運(yùn)行程序 可得到如圖 所示的界面
圖 【自己畫菜單】運(yùn)行界面之二
可見繪制的 文件 菜單項(xiàng)并沒有完全顯示出來 并且后面的菜單項(xiàng)也沒有顯示 這是因?yàn)椴藛雾?xiàng)的顯示區(qū)域并沒有設(shè)定 而缺省的空間又不能完全顯示造成的 設(shè)定菜單項(xiàng)的顯示區(qū)域大小是通過MeasureItem事件來完成的 具體操作是在MenuItem 的DrawItem事件后添加下列代碼 下列代碼是是定義MenuItem 的MeasureItem事件 在此事件中設(shè)定菜單項(xiàng)的寬度(當(dāng)然也可以設(shè)定高度等)
Private Sub MenuItem _MeasureItem ( ByVal sender As Object ByVal e As System Windows Forms MeasureItemEventArgs ) Handles MenuItem MeasureItem e ItemWidth = 設(shè)定菜單項(xiàng)的寬度End Sub
保存上述修改后 單擊快捷鍵F 運(yùn)行程序可得到圖 所示界面
圖 【自己畫菜單】運(yùn)行界面之三
可見 文件 菜單項(xiàng)就算繪制出來了 由于其他菜單項(xiàng)沒有繪制處理 所以也未顯示 其他菜單項(xiàng)的繪制方法和 文件 菜單項(xiàng)的繪制方法基本相似 以下是在上述完成的基礎(chǔ)上 對(duì)其他菜單項(xiàng)進(jìn)行繪制 從而得到圖 所示菜單的具體實(shí)現(xiàn)步驟
圖 【自己畫菜單】運(yùn)行界面之四
在Form vb中的MenuItem 的MeasureItem事件處理程序之后添加下列代碼 下列代碼是定義MenuItem 的DrawItem事件 其功能是對(duì) 新建 菜單項(xiàng)重新繪制
Private Sub MenuItem _DrawItem ( ByVal sender As Object ByVal e As System Windows Forms DrawItemEventArgs ) Handles MenuItem DrawItem Dim rfBound As RectangleF = New RectangleF ( e Bounds X e Bounds Y e Bounds Width e Bounds Height ) 根據(jù)DrawItemEventArgs參數(shù)獲得菜單項(xiàng)矩形區(qū)域并存儲(chǔ)到RectangleF類型實(shí)例中 Dim rfBound As Rectangle = New Rectangle ( e Bounds X e Bounds Y e Bounds Width e Bounds Height ) 根據(jù)DrawItemEventArgs參數(shù)獲得菜單項(xiàng)矩形區(qū)域并存儲(chǔ)到Rectangle類型實(shí)例中 Rectangle類型實(shí)例和RectangleF類型實(shí)例差不多 但在后面代碼中繪制菜單的函數(shù)是有區(qū)別的 e Graphics FillRectangle ( New SolidBrush ( Color LightGray ) rfBound ) Dim s As MenuItem = CType ( sender MenuItem ) Dim s As String = s Text 獲得菜單項(xiàng)對(duì)應(yīng)的文本名稱 Dim sfTemp As StringFormat = New StringFormat ( ) sfTemp Alignment = StringAlignment Center 設(shè)定文本在矩形區(qū)域的對(duì)齊方式 sfTemp LineAlignment = StringAlignment Center Dim rcText As RectangleF = rfBound rcText Width = e Graphics DrawString ( s New Font ( 宋體 ) New SolidBrush ( Color Blue ) rcText sfTemp ) e Graphics DrawRectangle ( New Pen ( New SolidBrush ( Color LightGray ) ) rfBound ) If e State = ( DrawItemState NoAccelerator Or DrawItemState Selected ) Thene Graphics FillRectangle ( New SolidBrush ( Color LightYellow ) rfBound ) e Graphics DrawString ( s New Font ( 宋體 FontStyle Bold Or FontStyle Underline ) New SolidBrush ( Color Red ) rcText sfTemp ) e Graphics DrawRectangle ( New Pen ( New SolidBrush ( Color Black ) ) rfBound ) e DrawFocusRectangle ( ) End IfEnd Sub
MenuItem 的DrawItem事件處理代碼之后添加下列代碼 下列代碼是定義MenuItem 的MeasureItem事件 在此事件中實(shí)現(xiàn)設(shè)定 新建 菜單項(xiàng)的長度和高度
Private Sub MenuItem _MeasureItem ( ByVal sender As Object ByVal e As System Windows Forms MeasureItemEventArgs ) Handles MenuItem MeasureItem e ItemWidth = 設(shè)定菜單項(xiàng)的寬度 e ItemHeight = 設(shè)定菜單項(xiàng)的高度End Sub
在完成上述操作步驟后 再在MenuItem 的MeasureItem事件處理程序之后添加下列代碼 下列代碼是定義MenuItem 的DrawItem事件 其功能是對(duì) 打開 菜單項(xiàng)重新繪制
Private Sub MenuItem _DrawItem ( ByVal sender As Object ByVal e As System Windows Forms DrawItemEventArgs ) Handles MenuItem DrawItemDim rfBound As RectangleF = New RectangleF ( e Bounds X e Bounds Y e Bounds Width e Bounds Height ) 根據(jù)DrawItemEventArgs參數(shù)獲得菜單項(xiàng)矩形區(qū)域并存儲(chǔ)到RectangleF類型實(shí)例中Dim rfBound As Rectangle = New Rectangle ( e Bounds X e Bounds Y e Bounds Width e Bounds Height ) 根據(jù)DrawItemEventArgs參數(shù)獲得菜單項(xiàng)矩形區(qū)域并存儲(chǔ)到Rectangle類型實(shí)例中 Rectangle類型實(shí)例和RectangleF類型實(shí)例差不多 但在后面代碼中繪制菜單的函數(shù)是有區(qū)別的Dim s As MenuItem = CType ( sender MenuItem ) Dim s As String = s TextDim sfTemp As StringFormat = New StringFormat ( ) sfTemp Alignment = StringAlignment CentersfTemp LineAlignment = StringAlignment CenterDim rcText As RectangleF = rfBoundrcText Width = e Graphics DrawString ( s New Font ( Veranda ) New SolidBrush ( Color Blue ) rcText sfTemp ) e Graphics DrawRectangle ( New Pen ( New SolidBrush ( Color LightGray ) ) rfBound ) If e State = ( DrawItemState NoAccelerator Or DrawItemState Selected ) Then e Graphics FillRectangle ( New SolidBrush ( Color LightYellow ) rfBound ) e Graphics DrawString ( s New Font ( Veranda FontStyle Bold Or FontStyle Underline ) New SolidBrush ( Color Red ) rcText sfTemp ) e Graphics DrawRectangle ( New Pen ( New SolidBrush ( Color Black ) ) rfBound ) e DrawFocusRectangle ( ) End IfEnd Sub
MenuItem 的DrawItem事件處理代碼之后添加下列代碼 下列代碼是定義MenuItem 的MeasureItem事件 在此事件中實(shí)現(xiàn)設(shè)定 新建 菜單項(xiàng)的長度和高度
Private Sub MenuItem _MeasureItem ( ByVal sender As Object ByVal e As System Windows Forms MeasureItemEventArgs ) Handles MenuItem MeasureItem e ItemWidth = 設(shè)定菜單項(xiàng)的寬度 e ItemHeight = 設(shè)定菜單項(xiàng)的高度End Sub
在上述步驟都正確完成后 本文介紹的手工繪制菜單就完成 此時(shí)單擊快捷鍵F 運(yùn)行 程序就可以得到圖 所示的運(yùn)行界面
六.總結(jié)
本文主要內(nèi)容是介紹VB NET設(shè)計(jì)和創(chuàng)建菜單 其中不僅介紹了使用菜單設(shè)計(jì)器來靜態(tài)設(shè)計(jì)菜單 還介紹了使用MainMenu類 MenuItem類和ContextMenu類動(dòng)態(tài)創(chuàng)建菜單的實(shí)現(xiàn)方法 在動(dòng)態(tài)創(chuàng)建時(shí) 首先要了解要?jiǎng)?chuàng)建的菜單類型 是下拉菜單 首先要?jiǎng)?chuàng)建一個(gè)MainMenu實(shí)例 是彈出菜單 首先要?jiǎng)?chuàng)建一個(gè)ContextMenu實(shí)例 然后根據(jù)菜單中的組成結(jié)構(gòu) 即菜單項(xiàng)中的父子關(guān)系 創(chuàng)建出相應(yīng)菜單 最后就是顯示出菜單 如果是下拉菜單 指派給Form的Menu屬性 如果是彈出菜單 指派給可視組件或Form的ContextMenu屬性 這樣動(dòng)態(tài)創(chuàng)建菜單才能夠顯示出來 動(dòng)態(tài)創(chuàng)建菜單的工作才算完成
此外還介紹了在Visual Basic Net中繪制個(gè)性化菜單的實(shí)現(xiàn)方法和注意事項(xiàng) 在繪制個(gè)性化菜單時(shí)最重要的是掌握DrawItem事件和MeasureItem事件用法 及繪制菜單時(shí)所要使用到的方法 雖然本文繪制的菜單并不美觀 但你可以通過本文介紹的方法來修改 從而實(shí)現(xiàn)更美觀 更有個(gè)性的菜單 最后請(qǐng)記住 在繪制菜單時(shí) 首先把菜單項(xiàng)的 OwnerDraw 屬性設(shè)定為 True
lishixinzhi/Article/program/net/201311/15454
這個(gè)最好是建議你用后臺(tái)發(fā)送數(shù)據(jù)的方法,你可以百度下SendMessage和PostMessage這兩個(gè)函數(shù)
網(wǎng)站題目:vb.net聚焦 vb 文本框聚焦
URL標(biāo)題:http://aaarwkj.com/article46/doodehg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)頁設(shè)計(jì)公司、網(wǎng)站建設(shè)、全網(wǎng)營銷推廣、網(wǎng)站內(nèi)鏈、商城網(wǎng)站、網(wǎng)站設(shè)計(jì)公司
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)