简体   繁体   English

从登录表单C#控制所有控件

[英]Control all controls from Login form C#

I have Login form and after that Main form with a lot of more mini forms. 我有“登录”表单,之后有“主要”表单以及许多其他小型表单。 I am planning to put in my application some type of mini roles. 我正计划在我的应用程序中放入一些小型角色。 My idea is to take the logged-in user at logging, see his role and do what needs to be done ( disable some controls for example ). 我的想法是让登录的用户登录,查看他的角色并执行需要做的事情(例如,禁用某些控件)。

The problem is that I do not know how to control the controls on other forms from the initial form. 问题是我不知道如何从初始形式控制其他形式的控件。

Any advice? 有什么建议吗?

To edit controls in main form you can use foreach loop: 要以主要形式编辑控件,可以使用foreach循环:

First declare second form: 首先声明第二种形式:

SecondForm second = new SecondForm();

Then foreach all controls: 然后foreach所有控件:

foreach(Control c in second.Controls)
{
    c.Text = "hello world!";
}

And you can use .GetType() to get type of control: 您可以使用.GetType()来获取控件的类型:

foreach(Control c in second.Controls)
{
    if (c.GetType() == typeof(TextBox))
        c.Text = "hello world!";
}

If you want to disable some controls you can do it by name: 如果要禁用某些控件,可以按名称进行操作:

foreach(Control c in second.Controls)
{
    if (c.Name == "button1")
        c.Enabled = false;
}

Or you can disable it by type with .GetType() : 或者您可以使用.GetType()通过类型禁用它:

foreach(Control c in second.Controls)
{
    if (c.GetType() == typeof(Button))
        c.Enabled = false;
}

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

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