Event wanted

M

Martin

I have created a subclass of a textbox. In this subclass I have a property
called 'Caption'. If this property contains a value then I want to create a
Label object and add it to the controls collection of the parent of the
Textbox. So, basically both objects exist next to eachother on the same
parent.

Originally I tried the object creation inside the Property-Set, but this
seems that this is too early, because the textbox itself isn't added to the
controls collection of the parent yet.
So, I'm looking for an event, which comes after the textbox is completely
created and added to the controls collection of its parent. Judging by the
name I would think 'Finalize' would be a good one, but... The description of
this event in the help documentation sounds very mysterious, more like
something that happens when the object is being destroyed.

What would be a good event?

Tia,
Martin
 
L

Larry Lard

Martin said:
I have created a subclass of a textbox. In this subclass I have a property
called 'Caption'. If this property contains a value then I want to create a
Label object and add it to the controls collection of the parent of the
Textbox. So, basically both objects exist next to eachother on the same
parent.

Originally I tried the object creation inside the Property-Set, but this
seems that this is too early, because the textbox itself isn't added to the
controls collection of the parent yet.
So, I'm looking for an event, which comes after the textbox is completely
created and added to the controls collection of its parent. Judging by the
name I would think 'Finalize' would be a good one, but... The description of
this event in the help documentation sounds very mysterious, more like
something that happens when the object is being destroyed.

No, not Finalize. That is, as you have guessed, what happens when the
object is being destroyed by the garbage collector.
What would be a good event?

ParentChanged. And you might know this, but for anyone else reading,
the recommended thing to do to change event-response behaviour in a
subclassed control is to override OnParentChanged (remembering to call
MyBase.OnParentChanged), rather than Handles Me.ParentChanged.
 
M

Martin

Hi Larry,

Thanks for your reply. This event worked fine for me, however the newly
created control behaves a bit different than I had expected.

This is the sub as well as the event I use for calling the sub:

Protected Overrides Sub OnParentChanged(ByVal e As System.EventArgs)
MyBase.OnParentChanged(e)
CreateCaption()
End Sub

Private Sub CreateCaption()
If pvtCaption <> "" Then
If pvtLabelObject Is Nothing Then
pvtLabelObject = New EntryCaption
pvtLabelObject.Anchor = AnchorStyles.None
pvtLabelObject.AutoSize = False
pvtLabelObject.BackColor = Color.Transparent
pvtLabelObject.BorderStyle = Windows.Forms.BorderStyle.None
pvtLabelObject.ForeColor = Color.Black
pvtLabelObject.Text = pvtCaption
pvtLabelObject.TextAlign = ContentAlignment.TopRight
pvtLabelObject.BoxObject = Me
pvtLabelObject.Visible = True
'RelocateCaption()
Me.Parent.Controls.Add(pvtLabelObject)
Else
pvtLabelObject.Text = pvtCaption
End If
End If
End Sub

The class EntryCaption is merely a Label with an additional property
'BoxObject'
In order to link the label with the Textbox I store the object reference of
the Textbox in the "BoxObject" property of the label, and vice versa.

After creating the EntryCaption (Label) I set a couple of properties, such
as:
pvtLabelObject.Anchor = AnchorStyles.None

However, the label seems to be anchored anyway.

It get's even weirder when I execute the ''RelocateCaption' sub (as you see
in the code above that call is commented out. When I uncomment it, the whole
label object seems to disappear.
When I ask the visible property in the debugger it tells me 'False' (even
before the relocate is executed).

I'm wondering, about the variable I use to store the object reference in
(pvtLabelObject) is declared as an EntryCaption (subclass of Label). Does
the object reference change after I have added this to the parent's controls
collection? In other words, after the adding, does pvtLabelObject still
contain a proper reference to the newly created object?

Hope you can help me out here, i'm really puzzled

Martin
 
M

Martin

Hi Larry,

Thanks for your reply. This event worked fine for me, however the newly
created control behaves a bit different than I had expected.

This is the sub as well as the event I use for calling the sub:

Protected Overrides Sub OnParentChanged(ByVal e As System.EventArgs)
MyBase.OnParentChanged(e)
CreateCaption()
End Sub

Private Sub CreateCaption()
If pvtCaption <> "" Then
If pvtLabelObject Is Nothing Then
pvtLabelObject = New EntryCaption
pvtLabelObject.Anchor = AnchorStyles.None
pvtLabelObject.AutoSize = False
pvtLabelObject.BackColor = Color.Transparent
pvtLabelObject.BorderStyle = Windows.Forms.BorderStyle.None
pvtLabelObject.ForeColor = Color.Black
pvtLabelObject.Text = pvtCaption
pvtLabelObject.TextAlign = ContentAlignment.TopRight
pvtLabelObject.BoxObject = Me
pvtLabelObject.Visible = True
'RelocateCaption()
Me.Parent.Controls.Add(pvtLabelObject)
Else
pvtLabelObject.Text = pvtCaption
End If
End If
End Sub

The class EntryCaption is merely a Label with an additional property
'BoxObject'
In order to link the label with the Textbox I store the object reference of
the Textbox in the "BoxObject" property of the label, and vice versa.

After creating the EntryCaption (Label) I set a couple of properties, such
as:
pvtLabelObject.Anchor = AnchorStyles.None

However, the label seems to be anchored anyway.

It get's even weirder when I execute the ''RelocateCaption' sub (as you see
in the code above that call is commented out. When I uncomment it, the whole
label object seems to disappear.
When I ask the visible property in the debugger it tells me 'False' (even
before the relocate is executed).

I'm wondering, about the variable I use to store the object reference in
(pvtLabelObject) is declared as an EntryCaption (subclass of Label). Does
the object reference change after I have added this to the parent's controls
collection? In other words, after the adding, does pvtLabelObject still
contain a proper reference to the newly created object?

Hope you can help me out here, i'm really puzzled

Martin
 
L

Larry Lard

Martin said:
Hi Larry,

Thanks for your reply. This event worked fine for me, however the newly
created control behaves a bit different than I had expected.

This is the sub as well as the event I use for calling the sub:

Protected Overrides Sub OnParentChanged(ByVal e As System.EventArgs)
MyBase.OnParentChanged(e)
CreateCaption()
End Sub

Private Sub CreateCaption()
If pvtCaption <> "" Then
If pvtLabelObject Is Nothing Then
pvtLabelObject = New EntryCaption
pvtLabelObject.Anchor = AnchorStyles.None

This may well be what's causing your problems. AnchorStyle.None doesn't
so much mean 'no anchoring', rather 'do weird resize behaviour in an
attempt to keep me in the same place on my parent'. To quote,
If a control has its Anchor property set to AnchorStyles.None, the
control moves half of the distance that the container of the control is
resized. For example, if a Button has its Anchor property set to
AnchorStyles.None and the Form that the control is located on is
resized by 20 pixels in either direction, the button will be moved 10
pixels in both directions.
Probably you should set pvtLabelObject.Anchor to Me.Anchor.

HOWEVER

Re-reading this thread I think I may have been leading you astray
somewhat - it now seems to me that a much better solution for you is to
create a UserControl that *contains* (not derives from) a TextBox, and
a Label. There is a lot of stuff in the docs about creating
UserControl, one of the particular cases being:
Inherit from the UserControl class if:

You want to combine the functionality of several Windows Forms controls
into a single reusable unit.
It's (to my mind) easier than it was under VB6, and there's
walkthroughs to get you started.
 
M

Martin

Hi Larry, thanks again for your reply.

The road you suggested (user control containing two controls) was the way I
originally thought to go, but somehow it didn't seem right. Dynamically
creating (and destroying if required) an additional object seems more
logical (imho) and more lean and clean. And I'm sure in the back of my mind
I remembered what an ordeal the usercontrol approach was in VB6.
The last few years I have been working a lot in another OOP language, and
there I did the same (dynamically creating a label object) without any
problems.

I lost the 'anchoring' line, so that it simply uses the class default (which
is actually ok for this purpose), but I still had the same problem. I
started looking if this event maybe occurs in a different thread or
something (it kinda like felt like that). Until I noticed that it was
something much simpler... A really stupid mistake I made in my code: I
forgot to initialize the variable I use to set the width! In other words,
the controls width was always 0. The fact that this causes the visible
property to change led me somewhat astray. Now it works perfectly!

Thanks for your help, I appreciate it.
Martin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top