如何使用C#将表单作为参数传递给函数

时间:2015-01-11 01:18:54

标签: c#

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

这是我想把它放入函数

的代码
    //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

关键字"此"应该引用主要的父表格(即Main.cs

3 个答案:

答案 0 :(得分:1)

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

  • 这在静态方法中不起作用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();
        }
    

答案 1 :(得分:1)

替换此行:

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

用这个:

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

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

答案 2 :(得分:0)

我终于想出了如何让它发挥作用

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