views:

47

answers:

2

I have a mdi where i have several line of code like the given below. Only chage is Form object which is being opened. So I want to all this work using only single function defination. When I tries to capture sender it gives me ToolStipMenuItem here. But I want its sender to be form name so that I can open its corresponding form.

private void purchaseInvoiceToolStripMenuItem_Click(object sender, EventArgs e)
{
    Forms.PurchaseInvoice purinv = new DigitalInvy.Forms.PurchaseInvoice();
    purinv.Show();
}

private void lederGroupsToolStripMenuItem_Click(object sender, EventArgs e)
{
    Forms.LedgerGroup lgrp = new DigitalInvy.Forms.LedgerGroup();
    lgrp.Show();
}

private void voucherEntryToolStripMenuItem_Click(object sender, EventArgs e)
{
    Forms.VoucherEntry ventry = new DigitalInvy.Forms.VoucherEntry();
    ventry.Show();
}

private void currencyToolStripMenuItem_Click(object sender, EventArgs e)
{
    Forms.CurrencyMaster currency = new DigitalInvy.Forms.CurrencyMaster();
    currency.Show();
}

private void countryToolStripMenuItem_Click(object sender, EventArgs e)
{

    Forms.CountryMaster country = new DigitalInvy.Forms.CountryMaster();
    country.Show();
}

I Want to do it something like this

private void cMenuItem_Click(object sender, EventArgs e)
{
if(sender.GetType()==Form)
  {
    Forms.CountryMaster country = new DigitalInvy.Forms.CountryMaster();
    country.Show();
  }
}
+2  A: 

How about this? If some of your forms need constructor arguments it'll make life slightly trickier, but you could have a non-generic overload taking a Func<Form> to say how to build the form to show.

private static void AssociateBuilder<T>(ToolStripMenuItem item)
    where T : Form, new()
{
    item.Click += () => new T().Show();
}

AssociateBuilder<PurchaseInvoice>(purchaseInvoiceToolsStripMenuItem);
AssociateBuilder<Ledger>(ledgerGroupsToolStripMenuItem);
// etc
Jon Skeet
@Jon: Please put some torch on using "where T : Form, new() " I have no idea about all that you wrote
Shantanu Gupta
@Shantanu Trygoogling for "c# generics".
Damian Powell
@Damian: I am new to generics although, I just want to know is "Form, new()" syntax a part of generics or it has some significance. I know little bit concept about generics
Shantanu Gupta
@Shantanu: Those are *type constraints* on the generic type parameter T. It's a bit much to explain in a comment though - I suggest you look it up in a C# book.
Jon Skeet
@Jon: Thx a lot, I will have a look at it now
Shantanu Gupta
+1  A: 

You are getting all these small Click event handlers because you used the designer to create the event handlers. It is easy to get rid of them if you write the event assignment yourself. For example:

  public partial class Form1 : Form {
    public Form1() {
      InitializeComponent();
      purchaseInvoiceToolStripMenuItem.Click += (o,e) => createChild(new frmPurchaseInvoice());
      ledgerGroupToolStripMenuItem.Click += (o, e) => createChild(new frmLedgerGroup());
      // etc..
    }
    private void createChild(Form frm) {
      frm.MdiParent = this;
      frm.Show();
    }
  }

If you're still on C# 2.0 then you can use an anonymous method:

  purchaseInvoiceToolStripMenuItem.Click += delegate { createChild(new frmPurchaseInvoice()); };
Hans Passant
@Hans: (o,e) => createChild(new frmPurchaseInvoice()); What does this line mean. Please elaborate
Shantanu Gupta
It is a lambda expression. If you're still on C# 2.0 then you can use an anonymous method. Post updated with an example.
Hans Passant