如何按名称查找按钮 [英] How to find button by name

查看:74
本文介绍了如何按名称查找按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有几个按钮的winform。我需要按名称找到按钮,并在找到按钮后使其可见或隐藏。对解决方案有什么建议吗?



我尝试过:



我尝试了Controls.Find和其他一些选项,但没有一个是成功的...。

I have a winform with several buttons. I need to find button by its name and make it visible or hidden after the button is found. Any suggestions for solutions?

What I have tried:

I tried Controls.Find and some other options but none of them was succesful….

推荐答案

你可以迭代控制集合。



You can iterate through control collection.

foreach (Control control in Controls)
{
      Button button = control as Button;
      if (!ReferenceEqual(null, button))
      {
        button.Visible = someConditionIfYoWant;
      }
}


如果您试图直接在窗口代码中找到按钮,您只需参考按钮按它的名字。 WinForms中的所有控件都有一个名称,所以如果你的按钮被称为 button1 ,那么访问按钮以查看可见性只是 button1.Visible



如果它来自窗外,您可能需要将按钮更改为公开。
If you're trying to find the button directly in the code of your window you can just reference the button by it's name. All controls in WinForms are given a name, so if your button has been called button1 then accessing the button for visibility is just button1.Visible

If it's from outside the window you may need to change your button to be public.


这篇关于如何按名称查找按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆