如何将表单作为参数传递给方法? (C#)

发布于 2024-11-24 02:56:44 字数 1161 浏览 1 评论 0原文

我确信我的标题完全错误(请随意修改它),但示例代码会消除混乱。

我有这样的事情要做:

    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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

黎歌 2024-12-01 02:56:44

有一种方法可以做到这一点,但它变得更难,因为看起来您正在使用 C# v2(来自问题的标签)。

更改您的 ShowForms 方法以接受 Func 实例,该实例可以使用提供的参数创建 Form 实例:

private void ShowForms(Func<string, Form> formCreator)
{
    if (abc == "cat")
    {
        return;
    }

    Form form = formCreator(abc);
    form.ShowDialog();
}

然后您可以调用它传入将用于创建每个单独表单实例的 Func 实例:

private void a_Click(object sender, EventArgs e)
{
    ShowForms(p => new Form1(p));
}

private void b_Click(object sender, EventArgs e)
{
    ShowForms(p => new Form2(p));
}

因为您使用的是 C# v2,所以您还需要声明 Func 定义:

public delegate TResult Func<TParameter, TResult>(TParameter parameter);

如果您可以使用更高版本的 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 a Func that can create an instance of a Form with a supplied parameter:

private void ShowForms(Func<string, Form> formCreator)
{
    if (abc == "cat")
    {
        return;
    }

    Form form = formCreator(abc);
    form.ShowDialog();
}

You can then call it passing in an instance of a Func that will be used to create each individual form instance:

private void a_Click(object sender, EventArgs e)
{
    ShowForms(p => new Form1(p));
}

private void b_Click(object sender, EventArgs e)
{
    ShowForms(p => new Form2(p));
}

Because you are using C# v2, you will also need to declare the Func definition:

public delegate TResult Func<TParameter, TResult>(TParameter parameter);

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.

谷夏 2024-12-01 02:56:44

您可以使用泛型和接口来完成此任务,而无需使用 vs2010 和 .net 2.0。

接口会是这样的

public interface IAbcForm { public string Abc {get;set;} }

,因为您需要一个属性或 setter 方法来设置参数。

ShowForm 方法如下所示:

private void ShowForm<T>(string parameter) where T:IAbcForm, new
{
    if(parameter == "cat") return;

    var form = new T();
    form.Abc = parameter;
    form.ShowDialog();
}

用法如下:

ShowForm<Form1>("abc");
ShowForm<Form2>("abc");
ShowForm<Form3>("abc");

您的表单必须实现该接口:

public class Form1 : Form, IAbcForm
{
    // use backing field when .net 2.0 does not support auto properties
    public string Abc { get;set; }

    public Form1() {}

    // I think your current constructor looks something like this:
    public Form1(string abc) { Abc = abc; }
}

}

You could use generics and an interface to accomplish this without vs2010 and .net 2.0.

The interface would be something like

public interface IAbcForm { public string Abc {get;set;} }

because you will need a property or setter method to set your parameter.

The ShowForm method will look like:

private void ShowForm<T>(string parameter) where T:IAbcForm, new
{
    if(parameter == "cat") return;

    var form = new T();
    form.Abc = parameter;
    form.ShowDialog();
}

Usage would be:

ShowForm<Form1>("abc");
ShowForm<Form2>("abc");
ShowForm<Form3>("abc");

Your forms will have to implement the interface:

public class Form1 : Form, IAbcForm
{
    // use backing field when .net 2.0 does not support auto properties
    public string Abc { get;set; }

    public Form1() {}

    // I think your current constructor looks something like this:
    public Form1(string abc) { Abc = abc; }
}

}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文