如何使用 C# 将窗体作为参数传递给函数

本文关键字:参数传递 函数 窗体 何使用 | 更新日期: 2023-09-27 18:37:08

我需要编写一个函数来防止自己一遍又一遍地编写相同的代码。

这是我想把它放到函数中的代码

    //make sure there are no other forms of the ame type open
    foreach (Form form in Application.OpenForms)
    {
        if (form.GetType() == typeof(DepartmentsAdd))
        {
            form.Activate();
            return;
        }
    }

    var newSignIn = new Verify();
    // Set the Parent Form of the Child window.
    newSignIn.MdiParent = this;
    newSignIn.FormClosed += delegate
    {
        if (UserInfo.Autherized == true)
        {
            UserInfo.Autherized = false;
            var role = new Roles();
            string[] aa = { "add" };
            if (role.hasAccess("department", aa))
            {
                var newMDIChild = new DepartmentsAdd();
                // Set the Parent Form of the Child window.
                newMDIChild.MdiParent = this;
                // Display the new form.
                newMDIChild.Show();
                newSignIn.Close();
            }
            else
            {
                Common.Alert("You do not have a permissions to perform this action!");
            }
        }
    };
    newSignIn.Show();

我必须将表单DepartmentsAdd为变量,以便我可以将其添加到函数中。 此外,我必须将此数组string[] aa = { "add" }传递给此函数,因为这些是代码中更改的 2 个值。

如何做到这一点是 c#?

到目前为止,我已经尝试过此功能,因为我在使用myform时出现错误

    public static void OpenMyForm(string sectionName, string[] keys, Form myform)
    {
        //make sure there are no other forms of the ame type open
        foreach (Form form in Application.OpenForms)
        {
            if (form.GetType() == typeof(myform))
            {
                form.Activate();
                return;
            }
        }

        var newSignIn = new Verify();
        // Set the Parent Form of the Child window.
        newSignIn.MdiParent = this;
        newSignIn.FormClosed += delegate
        {
            if (UserInfo.Autherized == true)
            {
                UserInfo.Autherized = false;
                var role = new Roles();
                if (role.hasAccess(sectionName, keys))
                {
                    var newMDIChild = new myform();
                    // Set the Parent Form of the Child window.
                    newMDIChild.MdiParent = this;
                    // Display the new form.
                    newMDIChild.Show();
                    newSignIn.Close();
                }
                else
                {
                    Common.Alert("You do not have a permissions to perform this action!");
                }
            }
        };
        newSignIn.Show();
    }
}

这是我收到的错误

The type or namespace name 'myform' could not be found (are you missing a using directive or an assembly reference?)

Error   4   Keyword 'this' is not valid in a static property, static method, or static field initializer

关键字"this"应引用主父窗体(即。 Main.cs

如何使用 C# 将窗体作为参数传递给函数

如果你使用的是静态方法,那么你必须这样做。

  • 这在静态方法中也不起作用,myFrom 是表单本身,所以你
  • 无法以这种方式创建实例。

     if(role.hasAccess(sectionName, keys))
        {
            var newMDIChild = myForm;
            // Set the Parent Form of the Child window.
            newMDIChild.MdiParent = <<your mdi form name>>; // instead of this. 
            // Display the new form.
            newMDIChild.Show();
            newSignIn.Close();
        }
    

替换此行:

if (form.GetType() == typeof(myform))

有了这个:

if (form.GetType() == myform.GetType())

我认为我不需要解释发生了什么,但如果这种变化的原因不清楚,请告诉我。

我终于想出了如何让它工作

public void OpenMyForm(string sectionName, string[] keys, Form myform)
        {
            //make sure there are no other forms of the ame type open
            foreach (Form form in Application.OpenForms)
            {
                if (form.GetType() == myform.GetType())
                {
                    form.Activate();
                    return;
                }
            }
            var newSignIn = new Verify();
            newSignIn.MdiParent = this;
            newSignIn.FormClosed += delegate
            {
                if (UserInfo.Autherized == true)
                {
                    UserInfo.Autherized = false;
                    var role = new Roles();
                    if (role.hasAccess(sectionName, keys))
                    {
                        var newMDIChild = myform;
                        // Set the Parent Form of the Child window.
                        newMDIChild.MdiParent = this;
                        newMDIChild.Dock = DockStyle.Fill;
                        // Display the new form.
                        newMDIChild.Show();
                        newSignIn.Close();
                    }
                    else
                    {
                        Common.Alert("You do not have a permissions to perform this action!");
                    }
                }
            };
            newSignIn.Show();
        }

调用此方法我调用此行

OpenMyForm("department", new string[]  { "add" }, new DepartmentsAdd() );