我的程序如下:功能是设置三个文本框txtID、txtName、txtSex的对齐方式
(1) 首先把三个文本框对象的名称赋值到数组strTxtItem中去
(2) 然后单击Command1_Click时,通过循环取出文本框的对象名,并设置各文本框的对齐方式为2.
但是运行到“strTmp.Alignment = 2”时,却提示“编译错误:无效限定字符”
我猜可能是文本框对象名默认取值为文本框的值,比如“print Text1”等价于“print Text1.text”
请问大侠有什么办法能取得文本框对象的句柄,而非其值,以避免产生歧义,谢谢!Dim strTxtItem(1 to 3) as string
Public Sub getStrItem()
strTxtItem(1) = "txtID"
strTxtItem(2) = "txtName"
strTxtItem(3) = "txtSex"
End SubPrivate Sub Command1_Click()
Call getStrItem
Dim i, j As Integer
For i = 1 To 3
For j = 1 To Len(strTxtItem(i))
strTxtItem(i).Alignment = 2 '运行到此处有错误,提示“编译错误:无效限定字符”
Next
Next
End Sub
(1) 首先把三个文本框对象的名称赋值到数组strTxtItem中去
(2) 然后单击Command1_Click时,通过循环取出文本框的对象名,并设置各文本框的对齐方式为2.
但是运行到“strTmp.Alignment = 2”时,却提示“编译错误:无效限定字符”
我猜可能是文本框对象名默认取值为文本框的值,比如“print Text1”等价于“print Text1.text”
请问大侠有什么办法能取得文本框对象的句柄,而非其值,以避免产生歧义,谢谢!Dim strTxtItem(1 to 3) as string
Public Sub getStrItem()
strTxtItem(1) = "txtID"
strTxtItem(2) = "txtName"
strTxtItem(3) = "txtSex"
End SubPrivate Sub Command1_Click()
Call getStrItem
Dim i, j As Integer
For i = 1 To 3
For j = 1 To Len(strTxtItem(i))
strTxtItem(i).Alignment = 2 '运行到此处有错误,提示“编译错误:无效限定字符”
Next
Next
End Sub
两种方法:
1、使用Controls(控件名)即可,如Controls(strTxtItem(i)).Alignment = 2就行了
2、使用CallByName函数:
CallByName strTxtItem(i), "Alignment", vbLet, 2_____________________________________Visual Basic for Applications ReferenceCallByName Function
Executes a method of an object, or sets or returns a property of an object.SyntaxCallByName(object, procname, calltype,[args()])The CallByName function syntax has these named arguments:Part Description
object Required; Variant (Object). The name of the object on which the function will be executed.
procname Required; Variant (String). A string expression containing the name of a property or method of the object.
calltype Required; Constant. A constant of type vbCallType representing the type of procedure being called.
args() Optional: Variant (Array).
ResThe CallByName function is used to get or set a property, or invoke a method at run time using a string name.In the following example, the first line uses CallByName to set the MousePointer property of a text box, the second line gets the value of the MousePointer property, and the third line invokes the Move method to move the text box:CallByName Text1, "MousePointer", vbLet, vbCrosshair
Result = CallByName (Text1, "MousePointer", vbGet)
CallByName Text1, "Move", vbMethod, 100, 100