Help please with a function

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • David Smith

    #1

    Help please with a function

    Im trying the following code: (pardon the indentations... weird newsreader)

    The obvious problem here is that the editPanel object is defined within the
    blocks, and out of scope outside of the switch statement where I want to set
    some basic parameters before diplaying it (Its a usercontrol).

    Anyway, Im trying to avoid creating a "base" object to inherit from in order
    to use this code, and I cant cast the object since the later code has no
    idea what type it is. Any suggestions?

    Thanks

    David

    private void ShowEditPanel(s tring editPanelName)

    {

    switch (editPanelName)

    {

    case "General":

    {

    Vortex.UC_Accou ntGeneral editPanel;

    editPanel = new UC_AccountGener al();

    break;

    }

    case "Numbers":

    {

    Vortex.UC_Accou ntNumbers editPanel;

    editPanel = new UC_AccountNumbe rs();

    break;

    }

    case "Address":

    {

    Vortex.UC_Accou ntAddress editPanel;

    editPanel = new UC_AccountAddre ss();

    break;

    }

    }



    // Set location of panel

    editPanel.Accou nt = this.Account;

    editPanel.Locat ion = new System.Drawing. Point(128, 64);

    editPanel.Width = this.Width - panel2.Width - 35;

    editPanel.Heigh t = this.Height - 110;

    editPanel.Ancho r =

    (((

    System.Windows. Forms.AnchorSty les.Top |

    System.Windows. Forms.AnchorSty les.Bottom)|

    System.Windows. Forms.AnchorSty les.Left) |

    System.Windows. Forms.AnchorSty les.Right);

    Controls.Add(ed itPanel);

    editPanel.Show( );

    editPanel.Bring ToFront();

    }



  • codewriter

    #2
    Re: Help please with a function

    It does not seem to be a very good approach. The problem that you mentioned
    can be dealt with though. You will need to iterate through all the controls
    on your form and try to cast all three types, if you do not hit the error,
    then it is the one that you need. But this is very cumbersome and will slow
    down your code. You also can use Tag property or Name property of this
    custom control to catch it. But again, you will have to iterate through
    controls on your form.
    I think that better way would be to declare all three of them using
    different names and make them available in your form. Then you can create a
    function that will check wich one of them is not null, and use it
    accordingly.

    "David Smith" <nospam@here.ne t> wrote in message
    news:3f356972$1 _4@news.athenan ews.com...[color=blue]
    > Im trying the following code: (pardon the indentations... weird[/color]
    newsreader)[color=blue]
    >
    > The obvious problem here is that the editPanel object is defined within[/color]
    the[color=blue]
    > blocks, and out of scope outside of the switch statement where I want to[/color]
    set[color=blue]
    > some basic parameters before diplaying it (Its a usercontrol).
    >
    > Anyway, Im trying to avoid creating a "base" object to inherit from in[/color]
    order[color=blue]
    > to use this code, and I cant cast the object since the later code has no
    > idea what type it is. Any suggestions?
    >
    > Thanks
    >
    > David
    >
    > private void ShowEditPanel(s tring editPanelName)
    >
    > {
    >
    > switch (editPanelName)
    >
    > {
    >
    > case "General":
    >
    > {
    >
    > Vortex.UC_Accou ntGeneral editPanel;
    >
    > editPanel = new UC_AccountGener al();
    >
    > break;
    >
    > }
    >
    > case "Numbers":
    >
    > {
    >
    > Vortex.UC_Accou ntNumbers editPanel;
    >
    > editPanel = new UC_AccountNumbe rs();
    >
    > break;
    >
    > }
    >
    > case "Address":
    >
    > {
    >
    > Vortex.UC_Accou ntAddress editPanel;
    >
    > editPanel = new UC_AccountAddre ss();
    >
    > break;
    >
    > }
    >
    > }
    >
    >
    >
    > // Set location of panel
    >
    > editPanel.Accou nt = this.Account;
    >
    > editPanel.Locat ion = new System.Drawing. Point(128, 64);
    >
    > editPanel.Width = this.Width - panel2.Width - 35;
    >
    > editPanel.Heigh t = this.Height - 110;
    >
    > editPanel.Ancho r =
    >
    > (((
    >
    > System.Windows. Forms.AnchorSty les.Top |
    >
    > System.Windows. Forms.AnchorSty les.Bottom)|
    >
    > System.Windows. Forms.AnchorSty les.Left) |
    >
    > System.Windows. Forms.AnchorSty les.Right);
    >
    > Controls.Add(ed itPanel);
    >
    > editPanel.Show( );
    >
    > editPanel.Bring ToFront();
    >
    > }
    >
    >
    >[/color]


    Comment

    Working...