.net 如何以编程方式获取toolstrip控件内部控件的列表

.net 如何以编程方式获取toolstrip控件内部控件的列表,.net,toolstrip,toolstripitem,.net,Toolstrip,Toolstripitem,我只是注意到hasChildren方法没有返回toolstrip中的各种项目,只是因为我猜它不是一个容器 有一个答案,但在我看来它太复杂了 是否有一种简单的方法来迭代toolstrip控件的控件?回答: 我用一个非常简单的递归调用回家! 不需要繁琐、极其复杂的3页c#代码,以下是我编写的代码片段,它可以工作: 为每个循环创建一个循环,以迭代所有表单的控件,并在循环中调用: Private Shared Sub recurseTranslateControls(ByVal lang As Stri

我只是注意到hasChildren方法没有返回toolstrip中的各种项目,只是因为我猜它不是一个容器

有一个答案,但在我看来它太复杂了

是否有一种简单的方法来迭代toolstrip控件的控件?

回答:

我用一个非常简单的递归调用回家! 不需要繁琐、极其复杂的3页c#代码,以下是我编写的代码片段,它可以工作:

为每个循环创建一个循环,以迭代所有表单的控件,并在循环中调用:

Private Shared Sub recurseTranslateControls(ByVal lang As String, ByVal c As Control)

    Dim newtxt as string = getLangItem(c.name, lang)     ' This function performs string translation
                                                         ' Nothing to do with the current post / answer
    ' This will work for "normal" controls
    If newtxt <> "" Then
        c.Text = newtxt                ' Apply the translated text to the control
    End If

    If c.HasChildren Then
        For Each co In c.Controls
                ' This will work for Toolstrip. You should do same for Menustrip etc.
                If "toolstrip".Contains(co.GetType.Name.ToLower) Then
                Dim ts As ToolStrip = co             ' Toolstrip doesn't have child controls, but it DOES have ITEMS!
                For Each itm As ToolStripItem In ts.Items
                    ' No need for recursivity: toolstrip items doesn't have children
                    Call TranslateToolstrip(lang, itm)           ' Apply the translated text to the toolstrip item
                Next
            Else
                Call recurseTranslateControls(lang, co)
            End If
        Next
    End If

End Sub

Private Shared Sub TranslateToolstrip(ByVal lang As String, ByVal t As ToolStripItem)

    Dim newtxt = getLangItem(t.name, lang)

    If newtxt <> "" Then
        t.Text = newtxt
    End If

End Sub
私有共享子递归TranslateControls(ByVal lang作为字符串,ByVal c作为控件)
Dim newText as string=getLangItem(c.name,lang)'此函数执行字符串转换
'与当前帖子/答案无关
'这将适用于“正常”控制
如果newtext“”则
c、 Text=newText'将翻译后的文本应用于控件
如果结束
如果c.有孩子,那么
对于c.控制中的每个co
'这将适用于Toolstrip。你应该为Menustrip等做同样的事情。
如果“toolstrip.包含(co.GetType.Name.ToLower),则
作为ToolStrip=co的Dim ts ToolStrip没有子控件,但它确实有项!
对于ts.项目中的每个itm作为ToolStripItem
'不需要递归性:toolstrip项没有子项
调用TranslateToolstrip(lang,itm)'将翻译后的文本应用于toolstrip项
下一个
其他的
调用recurseTranslateControls(lang,co)
如果结束
下一个
如果结束
端接头
私有共享子TranslateToolstrip(ByVal lang作为字符串,ByVal t作为ToolStripItem)
Dim newtxt=getLangItem(t.name,lang)
如果newtext“”则
t、 Text=newtext
如果结束
端接头
重要提示:我选择VB而不是c的原因之一是c会导致代码变得模糊、复杂、难以重读,最重要的是,c“所谓的”大师(不是你心目中真正的大师)非常乐意编写无人能理解的代码

每次我发现一个问题的复杂c#解决方案,我都不接受,而且我总是找到一些更简单的方法来完成这项工作。
是的,总是,总是

这个问题不正确。Toolstrip的项继承自ToolStripItem,ToolStripItem本身又派生自组件。它们不是控件,这就是ToolStrip.hasChildren始终返回false的原因,这也是它们不能被视为控件的原因。我有同样的任务,很明显ToolStripItem、MenuItems等应该在递归方法中分离。不方便,但没有其他方法

我最近不得不做类似的事情,并发现了这个问题。下面的代码段启用或禁用toolstrip中的项,具体取决于项名称是否包含sType变量

    Friend Sub ModifyEnabledControls(ByVal ts As ToolStrip, ByVal sType As String)
    For Each c As ToolStripItem In ts.Items
        If c.Name.Contains(sType) Then
            c.Enabled = True
        Else
            c.Enabled = False
        End If
    Next
End Sub
使用ModifyEnabledControl(ToolStrip1,“Customers”)调用该函数-这将禁用名称不包含“Customers”的任何toolstrip项