我正在嘗試構建一個簡單的用戶表單,其中包含一個帶有滾動條的文本框,用于提供資訊訊息。
不幸的是,我收到運行時錯誤 438,物件不正確支持此錯誤或方法錯誤。
我的測驗代碼很簡單“三種不同的嘗試”
用戶表單包括不同的嘗試
Dim inString As String
Sub txtBoxVal(passedStr As String)
inString = passedStr
MyMessageBox = inString
End Sub
public Property get txtBoxVal2(passedStr As String) as string
inString = passedStr
MyMessageBox = inString
End Sub
Public Sub update()
MyMessageBox.Text = publicStrVarDeclaredInFunction
End Sub
Private Sub UserForm_Initialize()
End Sub
Sheet1 代碼
Public publicVar As String
Private Sub userformTest()
Dim myForm As UserForm
Dim testString As String
testString = "This is a test string"
Set myForm = New MyUserForm
With myForm
' My 3 different way attempts
Call .txtBoxVal(testString)
.update publicVar
Call .txtBoxVal2(testString)
.Show
End With
End Sub
任何人都對我犯錯的地方有任何見解?
uj5u.com熱心網友回復:
假設您的用戶表單的名稱是MyUserForm并且MyMessageBox是TextBox控制元件:
你應該
Dim myForm As MyUserForm而不是Dim myForm As UserForm.您的
updatesub 沒有引數,但您傳入publicVar了.update publicVar,您需要在您的updatesub 中宣告引數,如下所示:
Public Sub update(argNewText As String)
MyMessageBox.Text = argNewText
End Sub
Call .txtBoxVal2(testString)不會作業有兩個原因:a)
txtBoxVal2是 aProperty,不是 aSub所以你不要使用Call. 您txtBoxVal2通過以下方式為財產分配價值myForm.txtBoxVal2 = "new value"b)
txtBoxVal2是一個Get屬性,因此您只能檢索txtBoxVal2值,為了允許txtBoxVal2分配值,您必須更改為Let:
Public Property Let txtBoxVal2(passedStr As String)
inString = passedStr
MyMessageBox = inString
End Property
======================
Let在這種情況下,我個人更喜歡使用屬性方法,因此您的代碼將如下所示:
表 1
Private Sub userformTest()
Dim testString As String
testString = "This is a test string"
Dim myForm As MyUserForm
Set myForm = New MyUserForm
myForm.MessageText = testString
End Sub
我的用戶表單
Private inString As String
Public Property Let MessageText(passedStr As String)
inString = passedStr
MyMessageBox.Text = inString
End Property
uj5u.com熱心網友回復:
試試這個:
制作一個名為“MyMsgBox”的簡單用戶表單,其中包含一個名為“TextBox1”的文本框和一個名為“CmdOK”的命令按鈕。
將此代碼放入用戶表單中:
Option Explicit
''' On form show:
Private Sub UserForm_Initialize()
''' Put text into textbox
TextBox1 = pstMessage
'''BEEP (optional)
Beep
End Sub
''' Close form on OK
Private Sub CmdOK_Click(): Unload Me
End Sub
將此代碼放在任何標準模塊中:
Option Explicit
Public pstMessage$
Sub ShowMsg()
''' Put the message into pstMessage
pstMessage = "This is a test string"
''' Run MyMsgBox
MyMsgBox.Show
End Sub
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/348748.html
上一篇:來自SVG的ImageBitmap-某些字體大小的鋸齒狀文本
下一篇:將多行合并在一張紙中
