Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
User Controls Again
Message
General information
Forum:
ASP.NET
Category:
Class design
Miscellaneous
Thread ID:
01397235
Message ID:
01397251
Views:
38
I don't think you need to use an interface. Why not just add a Setup method that a UserControl derived class?
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:
>
>I want to create a series of user controls that will be added to a form at runtime. They will obviously need to have certain similar properties and methods such as Setup() and SaveChanges();
>
>So I first tried this:
>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:
>
>I first created an interface:
>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.
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform