VBA的表单控件初接触(2):ActiveX控件的基础功能和基础代码
生活随笔
收集整理的這篇文章主要介紹了
VBA的表单控件初接触(2):ActiveX控件的基础功能和基础代码
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
控件的這幾個(gè)摸索的學(xué)習(xí)日志估計(jì)有錯(cuò)誤,不刪了當(dāng)成歷史放在這吧
?
第1類:文本類: label , textbox
- 我認(rèn)為label? textbox 都屬于文本類控件
- 其中l(wèi)abel,重點(diǎn)是輸入, output?,可以說(shuō)類似于一個(gè)常駐的msgbox這種
- 而textbox,重點(diǎn)是輸出,input,重點(diǎn)是接收用戶輸入
?
1.1 label
- 感覺(jué)好像沒(méi)啥好設(shè)置的,只有一些通用的控件事件
- label不讓用戶修改,定位是 展示,相當(dāng)于output,相當(dāng)于1個(gè)常駐的msgbox
- label可以讀取其他控件,或內(nèi)容的改變而改變顯示
Private Sub Label1_Click()
Label1.BackColor = &O555555
End Sub
?
1.2? textbox
- textbox比label 多的一個(gè)功能是,用戶可以直接在文本框內(nèi)輸入內(nèi)容
- textbox的定位,是接收輸入,input
- textbox 事件上只有一個(gè)特殊的,就是? textbox_change()
Private Sub TextBox1_Change()Debug.Print TextBox1.Value
Debug.Print TextBox1TextBox1.BackColor = RGB(0, 255, 0)End Sub
?
第2類:button 和 togglebutton
2.1 togglebutton? 切換按鈕/ 互斥按鈕
2.1.1 commandbutton 和 togglebutton 的區(qū)別
- 一組togglebutton中會(huì)始終有一個(gè)是按下去的狀態(tài)
- 如果是一組commandbutton一般是獨(dú)立的,互相不影響
?
2.1.2?基本用法
- 首先togglebutton應(yīng)該有多個(gè),并且這多個(gè)邏輯上是一組的
- 基本功能就是,多個(gè)togglebutton只會(huì)有一個(gè)亮起
- 如果想把幾個(gè) togglebutton關(guān)聯(lián),需要在每個(gè) tongglebutton里,關(guān)聯(lián)其他toggblebutton的值
- 比如一般是在togglebutton1里,設(shè)置togglebutton2.value=false,這樣設(shè)置互斥
Private Sub ToggleButton1_Click()ToggleButton1.Caption = "男"
ToggleButton2.Value = False
'ToggleButton1.TripleState = False
'ToggleButton2.TripleState = FalseIf ToggleButton1.Value = True ThenDebug.Print "toggle1按下了"Range("M39").Value = "toggle1按下了"
ElseDebug.Print "toggle1彈起了"Range("M39").Value = "toggle1彈起了"
End IfDebug.Print "toggle1被click執(zhí)行1次"
End SubPrivate Sub ToggleButton2_Click()
ToggleButton2.Caption = "女"
ToggleButton1 = False 'ToggleButton1.value = False
'ToggleButton2.TripleState = False
'ToggleButton1.TripleState = FalseIf ToggleButton2.Value = True ThenDebug.Print "toggle2按下了"Range("M43").Value = "toggle2按下了"
ElseDebug.Print "toggle2彈起了"Range("M43").Value = "toggle2彈起了"
End IfDebug.Print "toggle2被click執(zhí)行1次"
End Sub
?
2.1.3 執(zhí)行次數(shù)問(wèn)題?
- 如果togglebutton2按下了
- 這時(shí)候點(diǎn)擊togglebutton1按下的時(shí)候,只會(huì)把togglebutton2彈起,而tongglebutton1自己并不會(huì)按下,而且自己被執(zhí)行2次。。。
- 參考網(wǎng)上文章,設(shè)置了ToggleButton1.TripleState = False 也不管用
- http://club.excelhome.net/thread-1211638-1-1.html
?
2.1.4 如果讓toggle4來(lái)控制togglebutton3
- togglebutton可以控制 各種內(nèi)容,其實(shí)就當(dāng)成一個(gè)if來(lái)看就行
- 可以用一個(gè) tongglebutton來(lái)控制另外一個(gè)
Private Sub ToggleButton3_Click()If ToggleButton3.Value = True ThenRange("d50").Value = 1Worksheets(2).Select
ElseRange("d50").Value = 0Worksheets(3).Select
End IfEnd SubPrivate Sub ToggleButton4_Click()
ToggleButton3.Value = ToggleButton4.Value
End Sub
?
3? commandButton 和 很多通用事件
- 因?yàn)閏ommandButton用的最多,所以拿這個(gè)來(lái)試驗(yàn)各種通用事件
3.2.1 按鈕的各種觸發(fā)事件總結(jié)
- 點(diǎn)擊類
- _click? :點(diǎn)擊不同于按下? 可能是mouse也可能是KEY造成的點(diǎn)擊(包括keyPress + mouseDown?)
- _doubleClick
- 焦點(diǎn)類
- _gotFoucs :?無(wú)論是鼠標(biāo)還是鍵盤點(diǎn)擊了控件,都算 gotfoucs, 必須是確實(shí)點(diǎn)擊了才算,鼠標(biāo)移動(dòng)過(guò)來(lái)不算
- _lostFocus :?必須是焦點(diǎn)真的切走了,鼠標(biāo)移走沒(méi)選擇其他地方不算
- 鍵盤操作
- _keyDown: key的按下是一個(gè)持續(xù)的狀態(tài),松開(kāi)即結(jié)束。
- _keyUp:? ? ?key按下后放開(kāi),馬上就是keyup狀態(tài)
- _keyPress? ?: 必須是key點(diǎn)擊 , 任意key都可以,應(yīng)該可以識(shí)別是哪個(gè)key把?
- 鼠標(biāo)操作
- _mouseDown:好像有參數(shù)可以識(shí)別是鼠標(biāo)的那個(gè)鍵按下,或同時(shí)按下
- _mouseUp
- _mouseMove? ?:
- 還不清晰的 怎么用的一些
- _error ?
- _beforeDragOver
- _beforeDropOrPaste
?
3.2.2 mouse類,鼠標(biāo)相關(guān)
- mousedown: 按下鼠標(biāo)的那個(gè)狀態(tài),一般除非刻意,mousedown的時(shí)間很短
- mouseup:? ? 這個(gè)和mousedown 互為互斥狀態(tài),也就是一對(duì)開(kāi)關(guān)狀態(tài),一個(gè)button要么處于mousedown,要么mouseup
- mousemove:?鼠標(biāo)在 控件上方滑過(guò)即可,感覺(jué)優(yōu)先級(jí)比mouseup要高
Private Sub CommandButton1_MouseDown(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
CommandButton1.Caption = "鼠標(biāo)按下"
Debug.Print "鼠標(biāo)按下"
Debug.Print Button
Debug.Print Shift '可以傳遞參數(shù),判斷是否按下了SHIFT
Debug.Print X
Debug.Print Y'只有鼠標(biāo)按下不動(dòng)的那個(gè)持續(xù)的狀態(tài)才算,一松開(kāi)就不算了
End SubPrivate Sub CommandButton1_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
CommandButton1.Caption = "鼠標(biāo)滑過(guò)"
'感覺(jué)優(yōu)先級(jí)高于 mouseup
Debug.Print "鼠標(biāo)滑過(guò)"
End SubPrivate Sub CommandButton1_MouseUp(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
CommandButton1.Caption = "鼠標(biāo)松開(kāi)"
'基本是個(gè)常態(tài),鼠標(biāo)一松馬上顯示 鼠標(biāo)松開(kāi),一直處于 鼠標(biāo)松開(kāi)狀態(tài)
Debug.Print "鼠標(biāo)松開(kāi)"End Sub
?
3.2.3 key類,鍵盤操作
- _keyDown:任意鍵按下都可以
- _keyUp? :? ? 任意鍵按下后松開(kāi)都算,和keydown 屬于一對(duì)互斥狀態(tài),
- _keyPress? : 優(yōu)先級(jí)高于keydown,但感覺(jué)keydown==keypress機(jī)會(huì)一樣
Private Sub CommandButton1_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
CommandButton1.Caption = "KEY按下了"
Debug.Print Shift
'實(shí)測(cè)是任何鍵按下了都算End SubPrivate Sub CommandButton1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
CommandButton1.Caption = "KEY點(diǎn)擊了"
Debug.Print Shift
'點(diǎn)擊鍵盤按鍵才觸發(fā)
'有可能keydown 但不觸發(fā) keyPress嗎?好像不能,keydown 幾乎等于keypress吧
'優(yōu)先級(jí)總是高于 keydown
End SubPrivate Sub CommandButton1_KeyUp(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
CommandButton1.Caption = "KEY松開(kāi)了"
Debug.Print Shift
'實(shí)測(cè)是任何鍵按下了以后,再松開(kāi)都算
End Sub
?
3.2.4? click類?
- click 和 doubleClick
- click 只是一個(gè)點(diǎn)擊結(jié)果,點(diǎn)擊的發(fā)起者可能是 mouse 或 任意key
- 現(xiàn)在還不知道為啥? commandbutton.value 一直是false?(默認(rèn)返回值?)
- commandbutton 按鈕好像沒(méi)有狀態(tài),就是點(diǎn)擊,沒(méi)有什么 按下,起來(lái)?---應(yīng)該有那種點(diǎn)1下下去,再點(diǎn)1下起來(lái)的把?
- click 只是一個(gè)行為,只計(jì)次
Private Sub CommandButton1_Click()
CommandButton1.Caption = "點(diǎn)擊" & a & "次"
a = a + 1
'按下和起來(lái)是2個(gè)狀態(tài),要么是按下,要么是起來(lái)Debug.Print CommandButton1.Value '沒(méi)有值默認(rèn)為false?還是有切換?End Sub
?
3.2.5 button的代碼,如果每種都寫(xiě),寫(xiě)一起很多會(huì)發(fā)生事件響應(yīng)沖突
- 本身同一類的事件內(nèi)部就有些沖突
- 比如mouse類的 mouseup?和 mousemove 就有點(diǎn)重合
- 比如key類的,keydown 和 keypress 有點(diǎn)重復(fù)
- 比如click,只可能是鍵盤/鼠標(biāo)點(diǎn)擊,就和 keydown/keypress 以及 mousedown 可能有點(diǎn)重合
Private Sub CommandButton1_BeforeDragOver(ByVal Cancel As MSForms.ReturnBoolean, ByVal Data As MSForms.DataObject, ByVal X As Single, ByVal Y As Single, ByVal DragState As MSForms.fmDragState, ByVal Effect As MSForms.ReturnEffect, ByVal Shift As Integer)
CommandButton1.Caption = "BeforeDragOver"
Debug.Print "BeforeDragOver"
'???
End SubPrivate Sub CommandButton1_BeforeDropOrPaste(ByVal Cancel As MSForms.ReturnBoolean, ByVal Action As MSForms.fmAction, ByVal Data As MSForms.DataObject, ByVal X As Single, ByVal Y As Single, ByVal Effect As MSForms.ReturnEffect, ByVal Shift As Integer)
CommandButton1.Caption = "BeforeDropOrPaste"
Debug.Print "BeforeDropOrPaste"
'???
End SubPrivate Sub CommandButton1_Click()
CommandButton1.Caption = "已經(jīng)按下"
'按下和起來(lái)是2個(gè)狀態(tài),要么是按下,要么是起來(lái)
End SubPrivate Sub CommandButton1_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
CommandButton1.Caption = "你想弄啥"
'操作很明確
End SubPrivate Sub CommandButton1_Error(ByVal Number As Integer, ByVal Description As MSForms.ReturnString, ByVal SCode As Long, ByVal Source As String, ByVal HelpFile As String, ByVal HelpContext As Long, ByVal CancelDisplay As MSForms.ReturnBoolean)
Debug.Print "按鈕還會(huì)報(bào)錯(cuò)?"
'???
End SubPrivate Sub CommandButton1_GotFocus()
CommandButton1.Caption = "來(lái)點(diǎn)我啊"
'測(cè)試了下,獲得焦點(diǎn)的時(shí)觸發(fā)get,點(diǎn)了其他單元格或?qū)ο蟛艜?huì)觸發(fā)lostfocus
'和鼠標(biāo)軌跡無(wú)關(guān),移到上面并不觸發(fā),移走也不觸發(fā)lostfocus
End SubPrivate Sub CommandButton1_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
CommandButton1.Caption = "按下了"
End SubPrivate Sub CommandButton1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
CommandButton1.Caption = "點(diǎn)擊了"
'點(diǎn)擊鍵盤按鍵才觸發(fā)
End SubPrivate Sub CommandButton1_KeyUp(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
'???
End SubPrivate Sub CommandButton1_LostFocus()
CommandButton1.Caption = "離開(kāi)了"
'測(cè)試需要真正的焦點(diǎn)變化到其他地方才算,鼠標(biāo)挪開(kāi)不算
End SubPrivate Sub CommandButton1_MouseDown(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
CommandButton1.Caption = "鼠標(biāo)按下"
'測(cè)試了,只有鼠標(biāo)按下不動(dòng)的那個(gè)持續(xù)的狀態(tài)才算,一松開(kāi)就不算了
End SubPrivate Sub CommandButton1_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
CommandButton1.Caption = "鼠標(biāo)滑過(guò)"
End SubPrivate Sub CommandButton1_MouseUp(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
CommandButton1.Caption = "鼠標(biāo)松開(kāi)"
'沒(méi)試驗(yàn)出來(lái)?
'???
End Sub
?
3.2.6 CommandButton的各種屬性和方法?
- ?commandbutton.value=false ?
?
4? 第3類:option ,checkbox ,分組框
4.1 option 和分組框
- 好像放在附近的多個(gè)option,會(huì)自動(dòng)識(shí)別為一組?不寫(xiě)代碼也行?
- 暫時(shí)只知道寫(xiě)這樣簡(jiǎn)單的代碼
- 實(shí)現(xiàn)互斥效果
- 實(shí)現(xiàn)取得option按鈕的返回值的
Private Sub OptionButton1_Click()
OptionButton2.Value = False
a = OptionButton1.Value
Debug.Print aEnd SubPrivate Sub OptionButton2_Click()
OptionButton1.Value = False
a = OptionButton2.Value
Debug.Print aEnd Sub
?
4.2? CheckBox
- checkbox 感覺(jué)每個(gè)都是獨(dú)立的,代碼只需要取的每個(gè) checkbox的返回值就可以?
- 可以設(shè)置其顏色?CheckBox2.BackColor = RGB(255, 0, 0)
Private Sub CheckBox1_Click()
a = CheckBox1.Value '作用是不是就是這個(gè)checkbox返回其勾選值?
Debug.Print a
End SubPrivate Sub CheckBox2_Click()
a = CheckBox2.Value
Debug.Print aCheckBox2.BackColor = RGB(255, 0, 0)
'CheckBox2.Border = normal
Debug.Print CheckBox2.Name
'Debug.Print CheckBox2.ParentEnd Sub
?
4.3 分組框
?
?
5 第4類:顯示框+操作框:? listbox , combobox (additem)
?使用additem 更像字典
5.1 listbox
- ListBox1.Value = arr1
- 好像只能additem 不能數(shù)組賦值,感覺(jué)像字典?
- 暫時(shí)沒(méi)看到 listbox1.value 和? listbox1.text 有什么區(qū)別
Private Sub ListBox1_Click()'arr1 = Array(11, 22, 33, 44, 55)
'ListBox1.Value = arr1
'好像只能additem 不能數(shù)組賦值,感覺(jué)像字典?ListBox1.AddItem 1
ListBox1.AddItem 2
ListBox1.AddItem 3
ListBox1.AddItem 4
ListBox1.AddItem 5Debug.Print ListBox1.Value
Debug.Print ListBox1.TextEnd Sub
?
5.2 combobox
- 兩種combobox不一樣
- combobox的 activeX控件也支持用戶手動(dòng)輸入
- 表單控件的combobox只支持下拉框
- 添加內(nèi)容也是additem 10
- 寫(xiě)代碼后運(yùn)行下就可以生效
Private Sub ComboBox1_Change()ComboBox1.AddItem 10
ComboBox1.AddItem 20
ComboBox1.AddItem 30
ComboBox1.AddItem 40
ComboBox1.AddItem 50Debug.Print ComboBox1.Value
ComboBox1.BackColor = RGB(255, 0, 0)End Sub
?
6 第5類? spinbutton , scrollbar (設(shè)置min max 范圍)
使用 min max 更像一個(gè)值域范圍
6.1 spinbutton
- 每個(gè)控件都有個(gè)index,這個(gè)估計(jì)和TAB設(shè)置次序相關(guān)?
- 可以用 spinbutton1.min 和? spinbutton1.max 設(shè)置范圍
- 很多屬性可以在VBE中,寫(xiě)? spinbutton1. 自動(dòng)出來(lái)后查看
Private Sub SpinButton1_Change()Debug.Print SpinButton1.Value;
Debug.Print SpinButton1.Index '看起來(lái)這個(gè)像是控件的序號(hào)ID
'測(cè)試了下,如果不設(shè)置值域范圍,好像最小是0,最大好像正整數(shù)都可以?'設(shè)置值域范圍,但是沒(méi)有外部反應(yīng)
SpinButton1.Min = 1
SpinButton1.Max = 15
SpinButton1.SmallChange = 3End Sub
?
6.2 scrollbar
6.2.1 scrollbar的基本用法
- scrollbar1 不設(shè)置值域范圍的時(shí)候,好像默認(rèn)是0和正整數(shù)
- scollbar1.smallchange 是針對(duì)箭頭的調(diào)整幅度,比如每次調(diào)整10
- scrolbar1的滑動(dòng)塊,好像不能控制,可以精確到1
?
6.2.2 怎么設(shè)置其數(shù)據(jù)源?? listbox,spinbutton 都應(yīng)該需要設(shè)置把
- 設(shè)置有問(wèn)題的屬性
- ScrollBar1.SourceName = Range("o25:o35") ? 這樣會(huì)報(bào)錯(cuò),但是需要怎么設(shè)置呢?
- ScrollBar1.ShapeRange
1
?
6.2.2 表單初始化的時(shí)候,給控件賦初值
- ?在表單初始化的時(shí)候可以給scrollbar賦初值,而且不會(huì)觸發(fā) scrollbar_change() 事件
Private Sub UserForm_Initialize()ScrollBar1.Value = 1
End Sub
?
總結(jié)
以上是生活随笔為你收集整理的VBA的表单控件初接触(2):ActiveX控件的基础功能和基础代码的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Python百度文库爬虫终极版
- 下一篇: 黑客初级知识(三)