UserControl control; switch (sKey) { case "ship_items": control = new crlShipItems(); break; ... } this.Controls.Add(control); control.Left = 0; control.Top = 0; control.Width = this.Width; control.Height = this.Height; control.Setup();Notice in the example, the only part inside the CASE is the reference to the class I want to instantiate. I don't know how to set this up correctly.
public partial class crlShipItems : UserControl >{ > public void Setup() > { > ... > } >} > >private void _AddPanel(string sKey) >{ > crlShipItems control; > > switch (sKey) > { > case "ship_items": > control = new crlShipItems(); > > this.Controls.Add(control); > control.Left = 0; > control.Top = 0; > control.Width = this.Width; > control.Height = this.Height; > > control.Setup(); > break; > ... > } >}>I'm feeling frustrated because I cannot get this to work:
>private void _AddPanel(string sKey) >>{ >> UserControl control; >> >> switch (sKey) >> { >> case "ship_items": >> control = new crlShipItems(); >> >> this.Controls.Add(control); >> control.Left = 0; >> control.Top = 0; >> control.Width = this.Width; >> control.Height = this.Height; >> >> control.Setup(); >> >> break; >> >> case "sview_inv": >> break; >> >> case "sreports": >> break; >> } >>}>But this doesn't compile because there is not Setup() on a UserControl. So I tried this:
>public interface IPanel >>{ >> void Setup(); >> bool SaveChanages(); >>}>I implemented the interface in the header of the Panel control's definition:
public partial class crlShipItems : UserControl, IPanel>Then I changed the _AddPanel method:
>private void _AddPanel(string sKey) >>{ >> IPanel control; >> >> switch (sKey) >> { >> case "ship_items": >> control = new crlShipItems(); >> >> this.Controls.Add(control); >> control.Left = 0; >> control.Top = 0; >> control.Width = this.Width; >> control.Height = this.Height; >> >> control.Setup(); >> >> break; >> >> case "sview_inv": >> break; >> >> case "sreports": >> break; >> } >>}>Not I get 5 compilations errors - one for each of the Top,Left, Width and Height, and on on the Controls.Add saying "The best overloaded method match for 'System.Windows.Forms.Control.ControlCollection.Add(System.Windows.Forms.Control)' has some invalid arguments", probably because it's no longer a control.