all groups > dotnet windows forms > november 2007 >
You're in the

dotnet windows forms

group:

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


Why should controls inherited from a base form be invisible at des Pat Moran
11/29/2007 8:47:05 AM
dotnet windows forms:
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?
Re: Why should controls inherited from a base form be invisible at des Wil Peck
11/29/2007 10:50:52 AM
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

[quoted text, click to view]
Re: Why should controls inherited from a base form be invisible at des Wil Peck
11/29/2007 1:03:19 PM
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

[quoted text, click to view]
Re: Why should controls inherited from a base form be invisible at des Wil Peck
11/29/2007 1:52:54 PM
Makes sense. Thanks for the reply.

[quoted text, click to view]
Re: Why should controls inherited from a base form be invisible at des Jon Skeet [C# MVP]
11/29/2007 7:11:58 PM
[quoted text, click to view]

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.

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
Re: Why should controls inherited from a base form be invisible at des Jon Skeet [C# MVP]
11/29/2007 9:41:29 PM
[quoted text, click to view]

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

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

and

public Foo()
{
// Stuff
}

[quoted text, click to view]

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.

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
Re: Why should controls inherited from a base form be invisible at Pat Moran
11/30/2007 1:57:00 AM
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


[quoted text, click to view]
AddThis Social Bookmark Button