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:
01397262
Views:
33
I guess I was hoping for a generic reference to the control:
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.



>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.
Everything makes sense in someone's mind
public class SystemCrasher :ICrashable
In addition, an integer field is not for irrational people
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform