简体   繁体   English

如何通过名称获取控件名称?

[英]How to get a name of control by name?

I have a simple function where there's a combo box. 我有一个简单的功能,其中有一个组合框。 If combo box's value is equal to "Disable", I'll disable textbox B. There are many combo boxes with their corresponding textbox B, arranged in rows and named by hand. 如果组合框的值等于“禁用”,我将禁用文本框B.有许多组合框及其对应的文本框B,按行排列并手动命名。 If combo box A is named Product1 , textbox B will be named Product1_status 如果组合框A名为Product1 ,则文本框B将命名为Product1_status

I was thinking something like: 我想的是:

If value_of_a = "disable" Then 
 Dim name_of_b as String
 name_of_b = Me.Combo.Name + "_status"
 get_object_by_name(name_of_b).Enabled = False
End If

How do I do this? 我该怎么做呢?

I'm not sure how you are calling this, but here's a self-contained procedure that should help: 我不确定你是怎么称呼这个,但这是一个自给自足的程序,应该有所帮助:

Sub test()

Dim ws As Excel.Worksheet
Dim ProductCombo As OLEObject
Dim ProductText As OLEObject

Set ws = ThisWorkbook.Sheets(1)
With ws
    Set ProductCombo = .OLEObjects("Product1")
    Set ProductText = .OLEObjects(ProductCombo.Name & "_status")
    ProductText.Enabled = ProductCombo.Object.Text <> "Disabled"
End With
End Sub

EDIT: I really hate worksheet controls - I start from scratch every time I program them! 编辑:我真的讨厌工作表控件 - 每次编程时我都会从头开始! Nonetheless, I thought I'd add this subroutine which resets every textbox whose name fits the patter Product#_status, according to its paired combobox. 尽管如此,根据配对的组合框,我想我会添加这个子程序来重置每个文本框的名称都适合模式Product#_status。 The logic does assume the names start with Product1, Product2, etc., without a gap in the numbering: 逻辑确实假设名称以Product1,Product2等开头,编号没有间隙:

Sub test2()

Dim ws As Excel.Worksheet
Dim ctl As OLEObject
Dim i As Long
Dim ProductComboboxesCount
Dim ProductCombo As OLEObject
Dim ProductText As OLEObject
Const ControlPrefix As String = "Product"

Set ws = ThisWorkbook.Sheets(1)
With ws
    For Each ctl In .OLEObjects
        If TypeOf ctl.Object Is MSForms.ComboBox And Left(ctl.Name, Len(ControlPrefix)) = ControlPrefix Then
            ProductComboboxesCount = ProductComboboxesCount + 1
        End If
    Next ctl
    For i = 1 To ProductComboboxesCount
        Set ProductCombo = .OLEObjects(ControlPrefix & i)
        Set ProductText = .OLEObjects(ControlPrefix & i & "_status")
        ProductText.Enabled = ProductCombo.Object.Text <> "Disabled"
    Next i
End With
End Sub

VBA VBA

Edit: (Change for an actual VBA macro) 编辑:(更改实际的VBA宏)

Sub Macro1()
'
' GetControl By Name
'
    If value_of_a = "disable" Then
        GetControl(ComboBox1.Name + "_status").Enabled = False
    End If

End Sub

Function GetControl(nameOfControl As String) As OLEObject
    Dim ctrl As OLEObject

    For Each ctrl In ActiveSheet.OLEObjects
        If ctrl.Name = nameOfControl Then
            Set GetControl = ctrl
        End If
    Next ctrl
End Function

VB.Net VB.Net

Code for VB.Net if anyone wants it for that reason: VB.Net的代码,如果有人想要它的原因:

Sub Main()
    If value_of_a = "disable" Then 
        GetControl(ComboBox_1.Name + "_status").Enabled = False
    End If
End Sub

Function GetControl(nameOfControl As String) As Control
    For Each ctrl In Me.Controls
        If ctrl.Name = nameOfControl Then
            Return ctrl
        End If
    Next ctrl

    Return Nothing
End Function

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM