Event wanted

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Martin

    #1

    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


  • Larry Lard

    #2
    Re: Event wanted


    Martin wrote:[color=blue]
    > 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.[/color]

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

    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.OnParent Changed), rather than Handles Me.ParentChange d.

    --
    Larry Lard
    Replies to group please

    Comment

    • Martin

      #3
      Re: Event wanted

      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.EventArg s)
      MyBase.OnParent Changed(e)
      CreateCaption()
      End Sub

      Private Sub CreateCaption()
      If pvtCaption <> "" Then
      If pvtLabelObject Is Nothing Then
      pvtLabelObject = New EntryCaption
      pvtLabelObject. Anchor = AnchorStyles.No ne
      pvtLabelObject. AutoSize = False
      pvtLabelObject. BackColor = Color.Transpare nt
      pvtLabelObject. BorderStyle = Windows.Forms.B orderStyle.None
      pvtLabelObject. ForeColor = Color.Black
      pvtLabelObject. Text = pvtCaption
      pvtLabelObject. TextAlign = ContentAlignmen t.TopRight
      pvtLabelObject. BoxObject = Me
      pvtLabelObject. Visible = True
      'RelocateCaptio n()
      Me.Parent.Contr ols.Add(pvtLabe lObject)
      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.No ne

      However, the label seems to be anchored anyway.

      It get's even weirder when I execute the ''RelocateCapti on' 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" <larrylard@hotm ail.com> wrote in message
      news:1141229592 .891663.293380@ i39g2000cwa.goo glegroups.com.. .[color=blue]
      >
      > Martin wrote:[color=green]
      >> 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.[/color]
      >
      > No, not Finalize. That is, as you have guessed, what happens when the
      > object is being destroyed by the garbage collector.
      >[color=green]
      >> What would be a good event?[/color]
      >
      > 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.OnParent Changed), rather than Handles Me.ParentChange d.
      >
      > --
      > Larry Lard
      > Replies to group please
      >[/color]



      Comment

      • Martin

        #4
        Re: Event wanted

        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.EventArg s)
        MyBase.OnParent Changed(e)
        CreateCaption()
        End Sub

        Private Sub CreateCaption()
        If pvtCaption <> "" Then
        If pvtLabelObject Is Nothing Then
        pvtLabelObject = New EntryCaption
        pvtLabelObject. Anchor = AnchorStyles.No ne
        pvtLabelObject. AutoSize = False
        pvtLabelObject. BackColor = Color.Transpare nt
        pvtLabelObject. BorderStyle = Windows.Forms.B orderStyle.None
        pvtLabelObject. ForeColor = Color.Black
        pvtLabelObject. Text = pvtCaption
        pvtLabelObject. TextAlign = ContentAlignmen t.TopRight
        pvtLabelObject. BoxObject = Me
        pvtLabelObject. Visible = True
        'RelocateCaptio n()
        Me.Parent.Contr ols.Add(pvtLabe lObject)
        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.No ne

        However, the label seems to be anchored anyway.

        It get's even weirder when I execute the ''RelocateCapti on' 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" <larrylard@hotm ail.com> wrote in message
        news:1141229592 .891663.293380@ i39g2000cwa.goo glegroups.com.. .[color=blue]
        >
        > Martin wrote:[color=green]
        >> 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.[/color]
        >
        > No, not Finalize. That is, as you have guessed, what happens when the
        > object is being destroyed by the garbage collector.
        >[color=green]
        >> What would be a good event?[/color]
        >
        > 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.OnParent Changed), rather than Handles Me.ParentChange d.
        >
        > --
        > Larry Lard
        > Replies to group please
        >[/color]


        Comment

        • Larry Lard

          #5
          Re: Event wanted


          Martin wrote:[color=blue]
          > 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.EventArg s)
          > MyBase.OnParent Changed(e)
          > CreateCaption()
          > End Sub
          >
          > Private Sub CreateCaption()
          > If pvtCaption <> "" Then
          > If pvtLabelObject Is Nothing Then
          > pvtLabelObject = New EntryCaption
          > pvtLabelObject. Anchor = AnchorStyles.No ne[/color]

          This may well be what's causing your problems. AnchorStyle.Non e 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,
          [color=blue][color=green]
          >>[/color][/color]
          If a control has its Anchor property set to AnchorStyles.No ne, 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.No ne 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.[color=blue][color=green]
          >>[/color][/color]

          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:
          [color=blue][color=green]
          >>[/color][/color]
          Inherit from the UserControl class if:

          You want to combine the functionality of several Windows Forms controls
          into a single reusable unit.[color=blue][color=green]
          >>[/color][/color]

          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

          Comment

          • Martin

            #6
            Re: Event wanted

            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" <larrylard@hotm ail.com> wrote in message
            news:1141308987 .272271.12290@t 39g2000cwt.goog legroups.com...[color=blue]
            >
            > Martin wrote:[color=green]
            >> 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.EventArg s)
            >> MyBase.OnParent Changed(e)
            >> CreateCaption()
            >> End Sub
            >>
            >> Private Sub CreateCaption()
            >> If pvtCaption <> "" Then
            >> If pvtLabelObject Is Nothing Then
            >> pvtLabelObject = New EntryCaption
            >> pvtLabelObject. Anchor = AnchorStyles.No ne[/color]
            >
            > This may well be what's causing your problems. AnchorStyle.Non e 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,
            >[color=green][color=darkred]
            >>>[/color][/color]
            > If a control has its Anchor property set to AnchorStyles.No ne, 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.No ne 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.[color=green][color=darkred]
            >>>[/color][/color]
            >
            > 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:
            >[color=green][color=darkred]
            >>>[/color][/color]
            > Inherit from the UserControl class if:
            >
            > You want to combine the functionality of several Windows Forms controls
            > into a single reusable unit.[color=green][color=darkred]
            >>>[/color][/color]
            >
            > 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
            >[/color]


            Comment

            Working...