代码之家  ›  专栏  ›  技术社区  ›  DaveG

运行时搜索窗体对象

  •  -1
  • DaveG  · 技术社区  · 6 年前

    我有太多的复选框,按钮,标签。。。。在我的简历里,有没有可能 search go to 其中一个在运行时?据我所知,在visualstudio中,您可以使用 Properties Windows 这是一个 ComboBox

    button1 System.Windows.Forms.Button
    button2 System.Windows.Forms.Button
    button3 System.Windows.Forms.Button
    label1 System.Windows.Forms.Label
    label2 System.Windows.Forms.Label
    label3 System.Windows.Forms.Label
    

    当你选择其中一个的时候,编译器会显示它在哪里,这是很好的,但是我想在运行时这样做,并通过它的属性来搜索它 name text

    1 回复  |  直到 6 年前
        1
  •  0
  •   Mong Zhu Bart de Boer    6 年前

    SelectionIndexChanged Controls 你的财产 Form

    Control toBeHighlighted = Controls.FirstOrDefault(x=> x.Name.Contains(hereSearchName));
    
    // Now do what ever you want with it.
    

    OfType<Type>()

    Button buttonToBeHighlighted = Controls.OfType<Button>()
                                           .FirstOrDefault(x=> x.Name.Contains(hereSearchName));