如何将表单作为参数传递给方法? (C#)
我确信我的标题完全错误(请随意修改它),但示例代码会消除混乱。
我有这样的事情要做:
private void a_Click(object sender, EventArgs e)
{
if (abc = "cat")
return;
Form1 f = new Form1(abc);
f.ShowDialog()
}
private void b_Click(object sender, EventArgs e)
{
if (abc = "cat")
return;
Form2 f = new Form2(abc);
f.ShowDialog()
}
private void c_Click(object sender, EventArgs e)
{
if (abc = "cat")
return;
Form3 f = new Form3(abc);
f.ShowDialog()
}
现在我如何编写一个方法来通过传递表单类本身来显示这样的表单。或者我是否破坏了类和对象的概念?像这样:
private void ShowForms(Form F)
{
if (abc = "cat")
return;
F f = new F(abc);
f.Showdialog();
}
然后
private void a_Click(object sender, EventArgs e)
{
ShowForms(Form1); // I cant pass string abc from here..
}
谢谢。没有它我也可以生活,但如果我能拥有它,那将会有很大的帮助。
编辑:我稍微修改了我的示例以使我的要求更清晰,因为第一个答案并没有完全解决我的问题。抱歉。
编辑2:我的问题不是如何让我的程序运行(这太微不足道了),而是如何通过传递表单作为参数来精确地使用第三个通用函数来显示表单(如上所述)。
I'm sure I got the title terribly wrong (feel free to make it proper), but the example code would clear the confusion.
I have something to do like this:
private void a_Click(object sender, EventArgs e)
{
if (abc = "cat")
return;
Form1 f = new Form1(abc);
f.ShowDialog()
}
private void b_Click(object sender, EventArgs e)
{
if (abc = "cat")
return;
Form2 f = new Form2(abc);
f.ShowDialog()
}
private void c_Click(object sender, EventArgs e)
{
if (abc = "cat")
return;
Form3 f = new Form3(abc);
f.ShowDialog()
}
Now how can I write a single method to show up forms like these by passing the form class itself. Or am I spoiling the very concept of classes and objects? Something like this:
private void ShowForms(Form F)
{
if (abc = "cat")
return;
F f = new F(abc);
f.Showdialog();
}
and then
private void a_Click(object sender, EventArgs e)
{
ShowForms(Form1); // I cant pass string abc from here..
}
Thanks. I can live without it, but would be of great help if I can have one.
EDIT: I've slightly modified my example to make my requirement clearer, since the first answer wasnt exactly addressing my issue. Apologies.
EDIT2: My Question is not how to get my program running (that would be too trivial), but how to precisely use a third common function to show up forms by passing form as argument (as described above).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
有一种方法可以做到这一点,但它变得更难,因为看起来您正在使用 C# v2(来自问题的标签)。
更改您的
ShowForms
方法以接受Func
实例,该实例可以使用提供的参数创建Form
实例:然后您可以调用它传入将用于创建每个单独表单实例的
Func
实例:因为您使用的是 C# v2,所以您还需要声明
Func
定义:如果您可以使用更高版本的 C#,您将不需要声明最后一部分。 但是,您需要使用更高版本的编译器来编译它(VS2010 应该没问题) - 它利用了更高版本的编译器理解 lamda 语法的事实。
这样做的好处是,只有在您需要执行某些操作时才会创建表单,而不是仅仅创建表单并将其传入(例如
ShowForms(new Form1(abc));
)与它。There is a way to do this, but it is made harder because it looks like you are using C# v2 (from the tag on the question).
Change your
ShowForms
method to accept an instance of aFunc
that can create an instance of aForm
with a supplied parameter:You can then call it passing in an instance of a
Func
that will be used to create each individual form instance:Because you are using C# v2, you will also need to declare the
Func
definition:If you can use a later version of C#, you will not need to declare this last part. You will however need to compile this using a later version of the compiler (VS2010 should be fine) - it is making use of the fact that the later version of the compiler understands the lamda syntax.
The benefit of doing it this way instead of just creating the form and passing it in (e.g.
ShowForms(new Form1(abc));
) is that the form will only be created if you need to do something with it.您可以使用泛型和接口来完成此任务,而无需使用 vs2010 和 .net 2.0。
接口会是这样的
,因为您需要一个属性或 setter 方法来设置参数。
ShowForm
方法如下所示:用法如下:
您的表单必须实现该接口:
}
You could use generics and an
interface
to accomplish this without vs2010 and .net 2.0.The interface would be something like
because you will need a property or setter method to set your parameter.
The
ShowForm
method will look like:Usage would be:
Your forms will have to implement the interface:
}