返回列表 上一主題 發帖

[發問] 動態新增控制後怎麼觸發事件

可以省略theTextBox
直接Set AA(I).C_TextBox = Me.Controls.Add("Forms.TextBox.1", "TextBox" & I, True ...
linyancheng 發表於 2016-4-11 13:30

此處絕對不可以偷懶將 theTextBox 省略,
如果予以省略成:
  1.         '  Set theTextBox = Me.Controls.Add("Forms.TextBox.1", "TextBox" & I, True)
  2.         '  Set AA(I).C_TextBox = theTextBox
  3.         Set AA(I).C_TextBox = Me.Controls.Add("Forms.TextBox.1", "TextBox" & I, True)
複製代碼
接下來的執行將會出現
  1. 執行階段錯誤 91:
  2. 沒有設定物件變數或 With 區塊變數
複製代碼
所以 GBKEE 大大原本的設定與考量是正確無誤誤的:
  1.         Set theTextBox = Me.Controls.Add("Forms.TextBox.1", "TextBox" & I, True)
  2.         Set AA(I).C_TextBox = theTextBox

  3.         With theTextBox
  4.                 .
  5.                .
  6.         End With
複製代碼

TOP

回復 4# linyancheng
除非你不使用 theTextBox 宣告之變數,而直接將它寫成:
  1.     For I = 1 To 3
  2.         '  Set theTextBox  = Me.Controls.Add("Forms.TextBox.1", "TextBox" & I, True)
  3.         '  Set AA(I).C_TextBox =theTextBox     '  可以省略 theTextBox
  4.         Set AA(I).C_TextBox = Me.Controls.Add("Forms.TextBox.1", "TextBox" & I, True)
  5.         
  6.         '  With theTextBox
  7.         With AA(I).C_TextBox
  8.                 .Left = 50
  9.                 .Top = K
  10.                 K = .Top + .Height + 18
  11.         End With
  12.     Next I
複製代碼
方可。

TOP

        靜思自在 : 愛不是要求對方,而是要由自身的付出。
返回列表 上一主題