VBA的表單控件初接觸(2):ActiveX控件的基礎功能和基礎代碼

控件的這幾個摸索的學習日誌估計有錯誤,不刪了當成歷史放在這吧

 

第1類:文本類: label , textbox

  • 我認爲label  textbox 都屬於文本類控件
  • 其中label,重點是輸入, output ,可以說類似於一個常駐的msgbox這種
  • 而textbox,重點是輸出,input,重點是接收用戶輸入

 

1.1 label

  • 感覺好像沒啥好設置的,只有一些通用的控件事件
  • label不讓用戶修改,定位是 展示,相當於output,相當於1個常駐的msgbox
  • label可以讀取其他控件,或內容的改變而改變顯示
Private Sub Label1_Click()
Label1.BackColor = &O555555
End Sub

 

1.2  textbox

  • textbox比label 多的一個功能是,用戶可以直接在文本框內輸入內容
  • textbox的定位,是接收輸入,input
  • textbox 事件上只有一個特殊的,就是  textbox_change()
Private Sub TextBox1_Change()

Debug.Print TextBox1.Value
Debug.Print TextBox1

TextBox1.BackColor = RGB(0, 255, 0)

End Sub

 

第2類:button 和 togglebutton

2.1 togglebutton  切換按鈕/ 互斥按鈕

2.1.1 commandbutton 和 togglebutton 的區別

  • 一組togglebutton中會始終有一個是按下去的狀態
  • 如果是一組commandbutton一般是獨立的,互相不影響

 

2.1.2 基本用法

  • 首先togglebutton應該有多個,並且這多個邏輯上是一組的
  • 基本功能就是,多個togglebutton只會有一個亮起
  • 如果想把幾個 togglebutton關聯,需要在每個 tongglebutton裏,關聯其他toggblebutton的值
  • 比如一般是在togglebutton1裏,設置togglebutton2.value=false,這樣設置互斥

Private Sub ToggleButton1_Click()

ToggleButton1.Caption = "男"
ToggleButton2.Value = False
'ToggleButton1.TripleState = False
'ToggleButton2.TripleState = False

If ToggleButton1.Value = True Then
   Debug.Print "toggle1按下了"
   Range("M39").Value = "toggle1按下了"
Else
   Debug.Print "toggle1彈起了"
   Range("M39").Value = "toggle1彈起了"
End If

Debug.Print "toggle1被click執行1次"
End Sub



Private Sub ToggleButton2_Click()
ToggleButton2.Caption = "女"
ToggleButton1 = False    'ToggleButton1.value = False
'ToggleButton2.TripleState = False
'ToggleButton1.TripleState = False

If ToggleButton2.Value = True Then
   Debug.Print "toggle2按下了"
   Range("M43").Value = "toggle2按下了"
Else
   Debug.Print "toggle2彈起了"
   Range("M43").Value = "toggle2彈起了"
End If

Debug.Print "toggle2被click執行1次"
End Sub

 

2.1.3 執行次數問題?

  • 如果togglebutton2按下了
  • 這時候點擊togglebutton1按下的時候,只會把togglebutton2彈起,而tongglebutton1自己並不會按下,而且自己被執行2次。。。
  • 參考網上文章,設置了ToggleButton1.TripleState = False 也不管用
  • http://club.excelhome.net/thread-1211638-1-1.html

 

2.1.4 如果讓toggle4來控制togglebutton3

  • togglebutton可以控制 各種內容,其實就當成一個if來看就行
  • 可以用一個 tongglebutton來控制另外一個
Private Sub ToggleButton3_Click()

If ToggleButton3.Value = True Then
   Range("d50").Value = 1
   Worksheets(2).Select
Else
   Range("d50").Value = 0
   Worksheets(3).Select
End If

End Sub

Private Sub ToggleButton4_Click()
ToggleButton3.Value = ToggleButton4.Value
End Sub

 

3  commandButton 和 很多通用事件

  • 因爲commandButton用的最多,所以拿這個來試驗各種通用事件

3.2.1 按鈕的各種觸發事件總結

  • 點擊類
  1. _click  :點擊不同於按下  可能是mouse也可能是KEY造成的點擊(包括keyPress + mouseDown?)
  2. _doubleClick
  • 焦點類
  1. _gotFoucs : 無論是鼠標還是鍵盤點擊了控件,都算 gotfoucs, 必須是確實點擊了纔算,鼠標移動過來不算
  2. _lostFocus : 必須是焦點真的切走了,鼠標移走沒選擇其他地方不算
  • 鍵盤操作
  1. _keyDown: key的按下是一個持續的狀態,鬆開即結束。
  2. _keyUp:     key按下後放開,馬上就是keyup狀態
  3. _keyPress   : 必須是key點擊 , 任意key都可以,應該可以識別是哪個key把?
  • 鼠標操作
  1. _mouseDown:好像有參數可以識別是鼠標的那個鍵按下,或同時按下
  2. _mouseUp
  3. _mouseMove   :
  • 還不清晰的 怎麼用的一些
  1. _error ?
  2. _beforeDragOver
  3. _beforeDropOrPaste

 

3.2.2 mouse類,鼠標相關

  • mousedown: 按下鼠標的那個狀態,一般除非刻意,mousedown的時間很短
  • mouseup:    這個和mousedown 互爲互斥狀態,也就是一對開關狀態,一個button要麼處於mousedown,要麼mouseup
  • mousemove: 鼠標在 控件上方滑過即可,感覺優先級比mouseup要高
Private Sub CommandButton1_MouseDown(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
CommandButton1.Caption = "鼠標按下"
Debug.Print "鼠標按下"
Debug.Print Button
Debug.Print Shift   '可以傳遞參數,判斷是否按下了SHIFT
Debug.Print X
Debug.Print Y

'只有鼠標按下不動的那個持續的狀態纔算,一鬆開就不算了
End Sub

Private Sub CommandButton1_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
CommandButton1.Caption = "鼠標滑過"
'感覺優先級高於 mouseup
Debug.Print "鼠標滑過"
End Sub

Private Sub CommandButton1_MouseUp(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
CommandButton1.Caption = "鼠標鬆開"
'基本是個常態,鼠標一鬆馬上顯示 鼠標鬆開,一直處於 鼠標鬆開狀態
Debug.Print "鼠標鬆開"

End Sub

 

3.2.3 key類,鍵盤操作

  • _keyDown:任意鍵按下都可以
  • _keyUp  :    任意鍵按下後鬆開都算,和keydown 屬於一對互斥狀態,
  • _keyPress  : 優先級高於keydown,但感覺keydown==keypress機會一樣
Private Sub CommandButton1_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
CommandButton1.Caption = "KEY按下了"
Debug.Print Shift
'實測是任何鍵按下了都算

End Sub

Private Sub CommandButton1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
CommandButton1.Caption = "KEY點擊了"
Debug.Print Shift
'點擊鍵盤按鍵才觸發
'有可能keydown 但不觸發 keyPress嗎?好像不能,keydown 幾乎等於keypress吧
'優先級總是高於 keydown
End Sub

Private Sub CommandButton1_KeyUp(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
CommandButton1.Caption = "KEY鬆開了"
Debug.Print Shift
'實測是任何鍵按下了以後,再鬆開都算
End Sub

 

3.2.4  click類 

  • click 和 doubleClick
  • click 只是一個點擊結果,點擊的發起者可能是 mouse 或 任意key
  • 現在還不知道爲啥  commandbutton.value 一直是false?(默認返回值?)
  • commandbutton 按鈕好像沒有狀態,就是點擊,沒有什麼 按下,起來?---應該有那種點1下下去,再點1下起來的把?
  • click 只是一個行爲,只計次
Private Sub CommandButton1_Click()
CommandButton1.Caption = "點擊" & a & "次"
a = a + 1
'按下和起來是2個狀態,要麼是按下,要麼是起來

Debug.Print CommandButton1.Value   '沒有值默認爲false?還是有切換?

End Sub

 

3.2.5 button的代碼,如果每種都寫,寫一起很多會發生事件響應衝突

  • 本身同一類的事件內部就有些衝突
  • 比如mouse類的 mouseup 和 mousemove 就有點重合
  • 比如key類的,keydown 和 keypress 有點重複
  • 比如click,只可能是鍵盤/鼠標點擊,就和 keydown/keypress 以及 mousedown 可能有點重合
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 Sub

Private 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 Sub

Private Sub CommandButton1_Click()
CommandButton1.Caption = "已經按下"
'按下和起來是2個狀態,要麼是按下,要麼是起來
End Sub


Private Sub CommandButton1_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
CommandButton1.Caption = "你想弄啥"
'操作很明確
End Sub

Private 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 "按鈕還會報錯?"
'???
End Sub

Private Sub CommandButton1_GotFocus()
CommandButton1.Caption = "來點我啊"
'測試了下,獲得焦點的時觸發get,點了其他單元格或對象纔會觸發lostfocus
'和鼠標軌跡無關,移到上面並不觸發,移走也不觸發lostfocus
End Sub

Private Sub CommandButton1_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
CommandButton1.Caption = "按下了"
End Sub

Private Sub CommandButton1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
CommandButton1.Caption = "點擊了"
'點擊鍵盤按鍵才觸發
End Sub

Private Sub CommandButton1_KeyUp(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
'???
End Sub

Private Sub CommandButton1_LostFocus()
CommandButton1.Caption = "離開了"
'測試需要真正的焦點變化到其他地方纔算,鼠標挪開不算
End Sub

Private Sub CommandButton1_MouseDown(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
CommandButton1.Caption = "鼠標按下"
'測試了,只有鼠標按下不動的那個持續的狀態纔算,一鬆開就不算了
End Sub

Private Sub CommandButton1_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
CommandButton1.Caption = "鼠標滑過"
End Sub

Private Sub CommandButton1_MouseUp(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
CommandButton1.Caption = "鼠標鬆開"
'沒試驗出來?
'???
End Sub

 

3.2.6 CommandButton的各種屬性和方法?

  •  commandbutton.value=false ?

 

4  第3類:option ,checkbox ,分組框

4.1 option 和分組框

  • 好像放在附近的多個option,會自動識別爲一組?不寫代碼也行?
  • 暫時只知道寫這樣簡單的代碼
  • 實現互斥效果
  • 實現取得option按鈕的返回值的
Private Sub OptionButton1_Click()
OptionButton2.Value = False
a = OptionButton1.Value
Debug.Print a

End Sub

Private Sub OptionButton2_Click()
OptionButton1.Value = False
a = OptionButton2.Value
Debug.Print a

End Sub

 

4.2  CheckBox

  • checkbox 感覺每個都是獨立的,代碼只需要取的每個 checkbox的返回值就可以?
  • 可以設置其顏色 CheckBox2.BackColor = RGB(255, 0, 0)
Private Sub CheckBox1_Click()
a = CheckBox1.Value   '作用是不是就是這個checkbox返回其勾選值?
Debug.Print a
End Sub

Private Sub CheckBox2_Click()
a = CheckBox2.Value
Debug.Print a

CheckBox2.BackColor = RGB(255, 0, 0)
'CheckBox2.Border = normal
Debug.Print CheckBox2.Name
'Debug.Print CheckBox2.Parent

End Sub

 

4.3 分組框

 

 

5 第4類:顯示框+操作框:  listbox , combobox (additem)

 使用additem 更像字典

5.1 listbox

  • ListBox1.Value = arr1
  • 好像只能additem 不能數組賦值,感覺像字典?
  • 暫時沒看到 listbox1.value 和  listbox1.text 有什麼區別
Private Sub ListBox1_Click()

'arr1 = Array(11, 22, 33, 44, 55)
'ListBox1.Value = arr1
'好像只能additem 不能數組賦值,感覺像字典?


ListBox1.AddItem 1
ListBox1.AddItem 2
ListBox1.AddItem 3
ListBox1.AddItem 4
ListBox1.AddItem 5

Debug.Print ListBox1.Value
Debug.Print ListBox1.Text

End Sub

 

5.2 combobox

  • 兩種combobox不一樣
  • combobox的 activeX控件也支持用戶手動輸入
  • 表單控件的combobox只支持下拉框
  • 添加內容也是additem 10
  • 寫代碼後運行下就可以生效
Private Sub ComboBox1_Change()

ComboBox1.AddItem 10
ComboBox1.AddItem 20
ComboBox1.AddItem 30
ComboBox1.AddItem 40
ComboBox1.AddItem 50

Debug.Print ComboBox1.Value
ComboBox1.BackColor = RGB(255, 0, 0)

End Sub

 

6 第5類  spinbutton , scrollbar (設置min max 範圍)

使用 min max 更像一個值域範圍

6.1 spinbutton

  • 每個控件都有個index,這個估計和TAB設置次序相關?
  • 可以用 spinbutton1.min 和  spinbutton1.max 設置範圍
  • 很多屬性可以在VBE中,寫  spinbutton1. 自動出來後查看
Private Sub SpinButton1_Change()

Debug.Print SpinButton1.Value;
Debug.Print SpinButton1.Index    '看起來這個像是控件的序號ID
'測試了下,如果不設置值域範圍,好像最小是0,最大好像正整數都可以?

'設置值域範圍,但是沒有外部反應
SpinButton1.Min = 1
SpinButton1.Max = 15
SpinButton1.SmallChange = 3

End Sub

 

6.2 scrollbar

6.2.1 scrollbar的基本用法

  • scrollbar1 不設置值域範圍的時候,好像默認是0和正整數
  • scollbar1.smallchange 是針對箭頭的調整幅度,比如每次調整10
  • scrolbar1的滑動塊,好像不能控制,可以精確到1

 

6.2.2 怎麼設置其數據源?  listbox,spinbutton 都應該需要設置把

  • 設置有問題的屬性
  • ScrollBar1.SourceName = Range("o25:o35") ? 這樣會報錯,但是需要怎麼設置呢?
  • ScrollBar1.ShapeRange
1

 

6.2.2 表單初始化的時候,給控件賦初值

  •  在表單初始化的時候可以給scrollbar賦初值,而且不會觸發 scrollbar_change() 事件
Private Sub UserForm_Initialize()
    ScrollBar1.Value = 1
End Sub

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章