开发者

How to pass a Form as a parameter to a method? (C#)

I'm sure I got the title terribly wron开发者_高级运维g (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).


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.


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; }
}

}

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜