HI,
I have a generic function as shown below. It can be used to show a form by calling
showForm(ch);
IT works for the second function (new without parameter) ,But if I want to show form but with parameter in the constructor as in the third function (new with parameter) ,then I could not do it .Any one has an idea how to do it?
void showForm<T>(T frm) where T :Form, new()
{
if (frm == null)
{
frm = new T();
}
frm.MdiParent = this;
frm.Show();
}
//Works for this
public frmChild2()
{
InitializeComponent();
ChildToolStrip = toolStrip1;
// toolStrip1.Visible = false;
}
//Does not Work for this
public frmChild2(string title)
{
InitializeComponent();
ChildToolStrip = toolStrip1;
Text = title;
// toolStrip1.Visible = false;
}
using
Where T : new()tells the compiler thatThas apublicno-arg constructor.The second form does not have such a constructor.
From what you show, there is no real need to set the title in the constructor (how would the
showFormmethod even know what to set?).Since
Tis also constrained to be aFormyou can setfrm.Text =after instantiating theForm.