473,425 Members | 1,785 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,425 software developers and data experts.

Who's your daddy? (control.parent null in constructor?)

I'm trying to write my first windows control, subclassed from Panel.

In the constructor I try to use the parent property but it is undefined
(null). Is there any way to get to the parent in the constructor? I want
to know the dimensions of the parent container. Thanks

Jim Margarit
Nov 17 '05 #1
4 7995
Unless your control takes the parent as a parameter to the constructor,
there would be no way to know the parent.
When you create an instance of the control, it is not associated with
any other controls until you specifically add it to them.
You might want to create a method like this on your control:

public void AssignToParent(Control parent){
this.Parent = parent;
// do stuff that references the parents dimensions
}

Then you can do something like:

Panel mainPanel = new Panel();
....
MyControl x = new MyControl();
x.AssignToParent(mainPanel);


Jim Margarit wrote:
I'm trying to write my first windows control, subclassed from Panel.

In the constructor I try to use the parent property but it is undefined
(null). Is there any way to get to the parent in the constructor? I want
to know the dimensions of the parent container. Thanks

Jim Margarit

Nov 17 '05 #2
If you need the get the parent dimensions, and simply using the Dock and
Anchor properties of the subclass will not suffice, then you can override
the OnParentChanged method. When the control is first being created
(constructor) it does not yet have a parent.

--
Tim Wilson
..Net Compact Framework MVP

"Jim Margarit" <ma**@drmargaritDOT.com> wrote in message
news:et*************@TK2MSFTNGP12.phx.gbl...
I'm trying to write my first windows control, subclassed from Panel.

In the constructor I try to use the parent property but it is undefined
(null). Is there any way to get to the parent in the constructor? I want
to know the dimensions of the parent container. Thanks

Jim Margarit

Nov 17 '05 #3
Just to add to what the others have said, if you look in the
InitializeComponent method of the parent form, the form must first create an
instance of your control before it can add it to its Control collection,
hence no parent until the forms constructor has finished running.

HTH
Dan

"Jim Margarit" wrote:
I'm trying to write my first windows control, subclassed from Panel.

In the constructor I try to use the parent property but it is undefined
(null). Is there any way to get to the parent in the constructor? I want
to know the dimensions of the parent container. Thanks

Jim Margarit

Nov 17 '05 #4
Thanks to all. OnParentChange should do the trick. So many similarities
to Delphi, so many differences...

Jim
Just to add to what the others have said, if you look in the
InitializeComponent method of the parent form, the form must first create an
instance of your control before it can add it to its Control collection,
hence no parent until the forms constructor has finished running.

HTH
Dan

"Jim Margarit" wrote:

I'm trying to write my first windows control, subclassed from Panel.

In the constructor I try to use the parent property but it is undefined
(null). Is there any way to get to the parent in the constructor? I want
to know the dimensions of the parent container. Thanks

Jim Margarit

Nov 17 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Christian Dieterich | last post by:
Hi, I need to create many instances of a class D that inherits from a class B. Since the constructor of B is expensive I'd like to execute it only if it's really unavoidable. Below is an example...
3
by: Matt Graham | last post by:
Here's a subset of a class I'm trying to initialize: class uiForm { public: uiForm(); uiForm( unsigned short ); unsigned short m_method; unsigned short m_form_id; FormType *m_frmP; char...
1
by: Derrick | last post by:
I have created a custom control, and for one of its properties it is possible that the value may be null. However, at design time when a null value is assigned, I get the error "The object...
3
by: Antuane | last post by:
hi guys, its me again, with a few simple (probably silly) quesetion... hi guys, i've managed to create a custom textbox class. & i'd like to access the parent control (in this case this would be...
1
by: K B | last post by:
Hi, I have a calendar control inside an EditItemTemplate. All works great except when the date field in the db is Null. I get the 'cannot cast string to datetime type' error. I haven't...
1
by: Nathan Sokalski | last post by:
I have created a custom control for ASP.NET using VB.NET. My control inherits from the System.Web.UI.WebControls.CompositeControl class, and is working fine. However, the Visual Studio .NET designer...
1
by: Chris | last post by:
I have some drop downs in a formview. I am binding them to a datasource with the bind syntax e.g. SelectedValue='<%# Bind("DirectorID") %>' . This works fine when there is a value to be bound to,...
0
by: hott5hotj | last post by:
in Actionscript 3.0 Ive created a datagrid that populated with data. I want to create a pop up window to confirm whether the removal of an entry. When I call from the child object the following, I...
1
by: bob | last post by:
Hi, How can you bind a Calendar control in a template field when the data is null? i.e. The bound data is a null date. I would like to use a calendar control in the edit template to update the...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.