Why should controls inherited from a base form be invisible at des

  • Thread starter Thread starter Pat Moran
  • Start date Start date
P

Pat Moran

I have created a C# form with several controls on it each of which have the
protected modifier set.

When I inherit a form from this base clas then none of the inherited
controls are visible at design time though they are visible and operational
in the inherited form at run time.

i can not see what is preventing them from being visible?
 
Make sure you forms constructor calls the base forms constructor as
follows...

public class MyForm : MyBaseForm
{
public MyForm()
:base()
{
InitializeComponent();
....
}
}

This will cause your base forms InitializeComponent() to be executed
initializing the base forms controls at design time.

Hope this helps.
Wil
 
Wil Peck said:
Make sure you forms constructor calls the base forms constructor as
follows...

public class MyForm : MyBaseForm
{
public MyForm()
:base()
{
InitializeComponent();
....
}
}

This will cause your base forms InitializeComponent() to be executed
initializing the base forms controls at design time.

Mind you, that's exactly what will happen if you miss out :base()
completely. The parameterless constructor of the base class is called
by default.
 
I realize how the base constructor is called by default. However, it
seemed like I had this same problem with an inherited form and the design
view. Calling the base forms constructor explicitly seemed to solve the
problem. Unfortunately I don't have a ton of Win Form dev so it could have
been some other problem I was unaware of. :|

Now that I think about it a little more, the problem I had was when my child
form had a constructor other than the default parameter-less constructor.
Whenever I instantiated my form using the non-default constructor the child
controls would not show up, but they wouldn't show up at all. This is
different than the scenario Pat describes.

Pat, I'd just make sure your InitializeComponent is able to be called on
your base form when in design view. If it is being called then the controls
should be displayed in your child form.

Thanks for setting me straight Jon. :D

Thanks,
Wil
 
Wil Peck said:
I realize how the base constructor is called by default. However, it
seemed like I had this same problem with an inherited form and the design
view. Calling the base forms constructor explicitly seemed to solve the
problem. Unfortunately I don't have a ton of Win Form dev so it could have
been some other problem I was unaware of. :|

It certainly wasn't that. There is literally no difference in the
compiled code between:

public Foo() : base()
{
// Stuff
}

and

public Foo()
{
// Stuff
}
Now that I think about it a little more, the problem I had was when my child
form had a constructor other than the default parameter-less constructor.
Whenever I instantiated my form using the non-default constructor the child
controls would not show up, but they wouldn't show up at all. This is
different than the scenario Pat describes.

I think the big problem is if you don't have a parameterless
constructor, the designer doesn't know what to call. At least, that's
one I've run into. It could be that *adding* an explicit parameterless
constructor fixes the issue.
 
Makes sense. Thanks for the reply.

Jon Skeet said:
It certainly wasn't that. There is literally no difference in the
compiled code between:

public Foo() : base()
{
// Stuff
}

and

public Foo()
{
// Stuff
}


I think the big problem is if you don't have a parameterless
constructor, the designer doesn't know what to call. At least, that's
one I've run into. It could be that *adding* an explicit parameterless
constructor fixes the issue.
 
Thanks Will for the suggestion which pointed the way to the actual problem.
The problem was that the base default constructor did not invoke
InitializeComponent. It appears that you dont need to ecplicitly invoke the
base comstructor.

Pat
 
Back
Top