功能中对象更改的名称 [英] Name of object changes in function
本文介绍了功能中对象更改的名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个获取文本框数量的函数.我需要在对象名称中使用此数字.
I have a function that gets the number of Textbox. I need to use this number in object's name.
Private Function func(ByVal number As Integer)
If Val(TextBox[number].Text) > 300 Then
TextBox[number].Text = 300
End If
End Function
例如如果数字为5,则应为: TextBox5.Text = 300
E.g. if number is 5 it should be: TextBox5.Text = 300
感谢帮助!
推荐答案
使用此函数的名称即可获得控件:
This function gets you a control given its name:
Private Function GetControlByName(name As String) As Object
Dim obj As Object
For Each obj In Me.Controls
If obj.Name = name Then
Set GetControlByName = obj
Exit Function
End If
Next
End Function
然后,该子项完成您想要的操作,但如果您没有真正名为TextBoxN的文本框,则它将失败,其中N是您提供的数字
Then this sub does what you want, but it will fail it you dont really have some textbox called TextBoxN, where N is the number you provide
Private Sub func(ByVal N As Integer)
Dim t As Object
Set t = GetControlByName("TextBox" & N)
If val(t.Text) > 300 Then t.Text = "300"
End Sub
这篇关于功能中对象更改的名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文