Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

C# problem - wrecking my buzz...

Options
  • 08-05-2007 12:43pm
    #1
    Registered Users Posts: 11,035 ✭✭✭✭


    Hey, using C# Express Edition - I have a form that has a custom panel added to it. The Form acts as an event handler for a button contained in the panel. Runs perfectly, no bother like. However there is 2 warnings that i am getting. They are :


    1. Type CustomPanel does not have a constructor with parameters of types Form.
    2. The variable 'customPanel1' is either undeclared or was never assigned.

    When i open the designer window to change the layout of the Form i get the following:
    One or more errors encountered while loading the designer. The errors are listed below. Some errors can be fixed by rebuilding your project, while others may require code changes.
    Type 'CustomPanel' does not have a constructor with parameters of types Form.

    Any ideas? The CustomPanel's constructor is:

    public Form1(Form1 theForm)
    
    {
    
         this.theForm = theForm;
    
         InitializeComponent();
    
    }
    




    As i said, the code works perfectly, just doesn't seem to let me access the designer... Any ideas/pointers? I am doing something obviously wrong or something?


Comments

  • Moderators, Science, Health & Environment Moderators Posts: 10,079 Mod ✭✭✭✭marco_polo


    dulpit wrote:
    Hey, using C# Express Edition - I have a form that has a custom panel added to it. The Form acts as an event handler for a button contained in the panel. Runs perfectly, no bother like. However there is 2 warnings that i am getting. They are :


    1. Type CustomPanel does not have a constructor with parameters of types Form.
    2. The variable 'customPanel1' is either undeclared or was never assigned.

    When i open the designer window to change the layout of the Form i get the following:



    Any ideas? The CustomPanel's constructor is:

    public Form1(Form1 theForm)
    
    {
    
         this.theForm = theForm;
    
         InitializeComponent();
    
    }
    




    As i said, the code works perfectly, just doesn't seem to let me access the designer... Any ideas/pointers? I am doing something obviously wrong or something?

    Not terribly familiar with C# more of a java man but I assume the following hold for both languages. The Constructor ought to have the same name as the class:
    public CustomPanel(Form1 theForm)
    
    {
    
         this.theForm = theForm;
    
         InitializeComponent();
    
    }
    

    The Form1(Form1 theForm){} code is considered a method not a constructor by the compiller as it does not have the same name as the class. And as you have not provided a constructor a default constructor with no parameters is generated by the compiller.


  • Registered Users Posts: 11,035 ✭✭✭✭dulpit


    Sorry, that was me not being specific enough... The constructor method for the custom panel is:
    public CustomPanel(Form1 theForm)
    {
         myForm1 = theForm;
         InitializeComponent();
    }
    

    and in the main Form1 class i call the custom panel like this:
         CustomPanel myPanel;
         myPanel = new CustomPanel(this);
    

    As i said, works perfectly, but won't let me get at the designer view, cos of some dodgy warnings...


  • Registered Users Posts: 981 ✭✭✭fasty


    So, is this code:
    CustomPanel myPanel;
         myPanel = new CustomPanel(this);
    

    being called from a class derived System.Windows.Form? Is it called Form1? The error your getting implies that the "this" in the above code is called from an instance of a Form class not Form1.

    That's only a guess though, it's hard to tell from small pieces of code sometimes.


  • Registered Users Posts: 11,035 ✭✭✭✭dulpit


    fasty wrote:
    So, is this code:
    CustomPanel myPanel;
         myPanel = new CustomPanel(this);
    

    being called from a class derived System.Windows.Form? Is it called Form1? The error your getting implies that the "this" in the above code is called from an instance of a Form class not Form1.

    That's only a guess though, it's hard to tell from small pieces of code sometimes.

    Ok, sorry for the small bits of code...

    Anyway, I have a class - Form1 is it's name and it inherits directly from System.Windows.Form. In there i create the CustomPanel, [myPanel = new CustomPanel(this)] I can't see why it wouldn't work, I used to do similar stuff in Java no hassle... (i'm new enough to C#)


Advertisement