473,387 Members | 1,569 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,387 software developers and data experts.

Event wanted

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
Mar 1 '06 #1
5 1026

Martin wrote:
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.

--
Larry Lard
Replies to group please

Mar 1 '06 #2
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
"Larry Lard" <la*******@hotmail.com> wrote in message
news:11**********************@i39g2000cwa.googlegr oups.com...

Martin wrote:
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.

--
Larry Lard
Replies to group please


Mar 2 '06 #3
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
"Larry Lard" <la*******@hotmail.com> wrote in message
news:11**********************@i39g2000cwa.googlegr oups.com...

Martin wrote:
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.

--
Larry Lard
Replies to group please

Mar 2 '06 #4

Martin wrote:
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.

--
Larry Lard
Replies to group please

Mar 2 '06 #5
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

"Larry Lard" <la*******@hotmail.com> wrote in message
news:11*********************@t39g2000cwt.googlegro ups.com...

Martin wrote:
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.

--
Larry Lard
Replies to group please

Mar 2 '06 #6

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

Similar topics

3
by: boxboy | last post by:
I am hoping someone from Microsoft could shed some insight into this question. Why did microsoft decide to use a verb based naming convtion for events rather such as Close and Click for rather...
2
by: Bill | last post by:
I'm having what seems to me to be an odd problem. Perhaps there is some explanation, but don't know at this point. Basically I have a form that tracks memberships and donations. The main form...
19
by: Heidi Hundåla | last post by:
Hi ! I have a Wep App in C#. Page_Unload fires after Page_Load, and it seems totally unreasonable when you want to use this event when you _leave_ the page. In my project we wanted to use...
15
by: Amit D.Shinde | last post by:
I am adding a new picturebox control at runtime on the form How can i create click event handler for this control Amit Shinde
8
by: Dave Wurtz | last post by:
All, Is there a way to force a derived class to contain an event. I have done this with methods (i.e. Protected MustOverride Sub Test), but can I do something similar with an event (Protected...
41
by: JohnR | last post by:
In it's simplest form, assume that I have created a usercontrol, WSToolBarButton that contains a button. I would like to eventually create copies of WSToolBarButton dynamically at run time based...
3
by: geskerrett | last post by:
We have been asked to develop and application for a client that is a 'notification" system. We would like to use python, but are struggling to find the right starting point. Any suggestions, tips...
19
by: Daniela Roman | last post by:
Hello, I try to fire an event under a button click event and maybe anybody can give a clue please. I have let's say a WEB grid with PageIndexChanged event: private void...
4
by: portCo | last post by:
Hello there, I am using Visual Basic 2005. I want to store a value when I press enter key on the keyboard. Could anyone help me out? Thanks alot.
4
by: FullBandwidth | last post by:
I have been perusing various blogs and MSDN pages discussing the use of event properties and the EventHandlerList class. I don't believe there's anything special about the EventHandlerList class in...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...

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.