简体   繁体   中英

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:

First declare second form:

SecondForm second = new SecondForm();

Then foreach all controls:

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

And you can use .GetType() to get type of control:

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() :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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