C# 如何使用C将窗体作为参数传递给函数#
我需要编写一个函数来防止自己一遍又一遍地编写相同的代码 下面是我想把它放到函数中的代码C# 如何使用C将窗体作为参数传递给函数#,c#,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
//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”}
传递给这个函数,因为这是代码中更改的两个值
这是怎么做到的
到目前为止,我已经尝试过这个函数,但在使用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?)
及
关键字“this”应该引用主父窗体(即
main.cs
)如果使用静态方法,则必须这样做
- 这在静态方法中不起作用,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(role.hasAccess(sectionName,keys)) { var newMDIChild=myForm; //设置子窗口的父窗体。 newMDIChild.mdipalent=;//而不是此。 //显示新表单。 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() );
谢谢,我纠正了这个问题,没有使这个方法成为静态的,也把它放在了我想使用的同一个类中。但问题不在于何时创建表单的新实例。(即,
var newMDIChild=new myform();
)这样更好。现在的问题是这一行var newMDIChild=new myform();
OpenMyForm("department", new string[] { "add" }, new DepartmentsAdd() );