Home All Groups Group Topic Archive Search About

how to remove a programmatically created DateTimePicker

Author
14 May 2009 10:12 AM
Co
Hi All,

I created a DateTimePicker to use on my form when I doubleclick a
textbox.
The problem is that I can't remove it when I used it.

Dim WithEvents m_picker As DateTimePicker
    Private Sub CreateDTP()

        m_picker = New DateTimePicker
        m_picker.Format = DateTimePickerFormat.Short
        m_picker.Location = tbVerloopt.Location
        m_picker.Size = tbVerloopt.Size
        m_picker.CalendarMonthBackground = Color.Beige
        tbVerloopt.Parent.Controls.Add(m_picker)

    End Sub
    Private Sub OnValueChanged(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles m_picker.ValueChanged

        Try
            tbVerloopt.Text = m_picker.Value.ToShortDateString()
        Catch oException As Exception
            MessageBox.Show(oException.Message)

        End Try
    End Sub
    Private Sub DropDownDTP(ByVal sender As System.Object, _
            ByVal e As System.EventArgs) Handles m_picker.DropDown

    End Sub
    Private Sub CloseUpDTP(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles m_picker.CloseUp

        Me.Controls.Remove(m_picker)
        m_picker = Nothing

    End Sub

The ClosUpDTP isn't working and the DTP stays visible.

Marco

Author
14 May 2009 10:44 AM
Andrew Morton
Co wrote:
> I created a DateTimePicker to use on my form when I doubleclick a
> textbox.
> The problem is that I can't remove it when I used it.
>
>    Private Sub CreateDTP()
>...
>        tbVerloopt.Parent.Controls.Add(m_picker)
>...
>    Private Sub CloseUpDTP(ByVal sender As System.Object, _
>        ByVal e As System.EventArgs) Handles m_picker.CloseUp
>
>        Me.Controls.Remove(m_picker)

Shouldn't you be removing it from the place you added it to, i.e.
tbVerloopt.Parent.Controls?

Andrew
Author
14 May 2009 10:48 AM
Co
Show quote Hide quote
On 14 mei, 12:44, "Andrew Morton" <a***@in-press.co.uk.invalid> wrote:
> Co wrote:
> > I created a DateTimePicker to use on my form when I doubleclick a
> > textbox.
> > The problem is that I can't remove it when I used it.
>
> >    Private Sub CreateDTP()
> >...
> >        tbVerloopt.Parent.Controls.Add(m_picker)
> >...
> >    Private Sub CloseUpDTP(ByVal sender As System.Object, _
> >        ByVal e As System.EventArgs) Handles m_picker.CloseUp
>
> >        Me.Controls.Remove(m_picker)
>
> Shouldn't you be removing it from the place you added it to, i.e.
> tbVerloopt.Parent.Controls?
>
> Andrew

Yes you are right but how?

Marco
Author
14 May 2009 4:56 PM
Cor Ligthert[MVP]
Cor

tbVerloopt.Parent.Controls.Remove(m_picker)

Be aware that setting the reference to nothing makes not sense.

Cor


Show quoteHide quote
"Co" <vonclausow***@gmail.com> wrote in message
news:312235e2-846c-4db9-a578-e068114469eb@s28g2000vbp.googlegroups.com...
On 14 mei, 12:44, "Andrew Morton" <a***@in-press.co.uk.invalid> wrote:
> Co wrote:
> > I created a DateTimePicker to use on my form when I doubleclick a
> > textbox.
> > The problem is that I can't remove it when I used it.
>
> > Private Sub CreateDTP()
> >...
> > tbVerloopt.Parent.Controls.Add(m_picker)
> >...
> > Private Sub CloseUpDTP(ByVal sender As System.Object, _
> > ByVal e As System.EventArgs) Handles m_picker.CloseUp
>
> > Me.Controls.Remove(m_picker)
>
> Shouldn't you be removing it from the place you added it to, i.e.
> tbVerloopt.Parent.Controls?
>
> Andrew

Yes you are right but how?

Marco
Author
14 May 2009 5:15 PM
Branco
Cor Ligthert[MVP] wrote:

> Be aware that setting the reference to nothing makes not sense.

Correct me if I'm wrong, but it seems to me that if the reference
being set to nothing is at form level then it may "free" the object to
be garbage-collected (if all other references were also freed), even
more if the top level variable is declared WithEvents (as is the case
with the OP's object).

And since it's a control, I guess it should be disposed alltogether.

Regards,

Branco.
Author
14 May 2009 5:41 PM
Co
On 14 mei, 19:15, Branco <branco.medei***@gmail.com> wrote:
Show quoteHide quote
> Cor Ligthert[MVP] wrote:
> > Be aware that setting the reference to nothing makes not sense.
>
> Correct me if I'm wrong, but it seems to me that if the reference
> being set to nothing is at form level then it may "free" the object to
> be garbage-collected (if all other references were also freed), even
> more if the top level variable is declared WithEvents (as is the case
> with the OP's object).
>
> And since it's a control, I guess it should be disposed alltogether.
>
> Regards,
>
> Branco.

Cheers folks.

Marco
Author
15 May 2009 4:38 AM
Cor Ligthert[MVP]
Branco,

No it does not, the reference has not any more meanings than being a
reference.

(The garbage collector runs by a time, by instance as there is low memory,
it is not a kind of processor occupier).

It should be disposed, but be aware that the method dispose means dispose
unmanaged resources, that has not much to do with disposing an object,
disposing of an object is done by the GC.

You be right, that as the class which is used is not made by using by
instance a component or a form template, then the implementation of Idispose
should be done as well. (Don't get the idea that solely using the dispose
(unmanaged resources) method is implementing that as often is written in
forums or newsgroups.)

Cor

Show quoteHide quote
"Branco" <branco.medei***@gmail.com> wrote in message
news:824a015b-43b0-4887-b859-9871432ff5f4@v4g2000vba.googlegroups.com...
> Cor Ligthert[MVP] wrote:
>
>> Be aware that setting the reference to nothing makes not sense.
>
> Correct me if I'm wrong, but it seems to me that if the reference
> being set to nothing is at form level then it may "free" the object to
> be garbage-collected (if all other references were also freed), even
> more if the top level variable is declared WithEvents (as is the case
> with the OP's object).
>
> And since it's a control, I guess it should be disposed alltogether.
>
> Regards,
>
> Branco.
Author
15 May 2009 2:39 PM
Branco
Cor Ligthert[MVP] wrote:
> Branco,
>
> No it does not, the reference has not any more meanings than being a
> reference.

Exactly what "does not"? What are you answering to? Please, learn how
to quote when responding to a newsgroup message, this will make
everyone's life easier.

> (The garbage collector runs by a time, by instance as there is low memory,
> it is not a kind of processor occupier).

I'll do my best trying to translate this phrase, but don't blame me if
the meaning comes out wrong (next time, please try simpler phrases if
English is difficult for you):
"The garbage collector runs in time intervals, for instance when there
Show quoteHide quote
>
> It should be disposed, but be aware that the method dispose means dispose
> unmanaged resources, that has not much to do with disposing an object,
> disposing of an object is done by the GC.
>
> You be right, that as the class which is used is not made by using by
> instance a component or a form template, then the implementation of Idispose
> should be done as well. (Don't get the idea that solely using the dispose
> (unmanaged resources) method is implementing that as often is written in
> forums or newsgroups.)
>
> Cor
>
> "Branco" <branco.medei***@gmail.com> wrote in message
>
> news:824a015b-43b0-4887-b859-9871432ff5f4@v4g2000vba.googlegroups.com...
>
>
>
> > Cor Ligthert[MVP] wrote:
>
> >> Be aware that setting the reference to nothing makes not sense.
>
> > Correct me if I'm wrong, but it seems to me that if the reference
> > being set to nothing is at form level then it may "free" the object to
> > be garbage-collected (if all other references were also freed), even
> > more if the top level variable is declared WithEvents (as is the case
> > with the OP's object).
>
> > And since it's a control, I guess it should be disposed alltogether.
>
> > Regards,
>
> > Branco.- Hide quoted text -
>
> - Show quoted text -
Author
15 May 2009 2:47 PM
Branco
Branco wrote:
> Cor Ligthert[MVP] wrote:

OOps, sorry for the unnedited message. Apparently my laptop got a life
of its own and sent the message while it was still being edited. Myff
appfollopgieffs toff efryonneff (takes off foot from mouth).

Show quoteHide quote
> > Branco,
>
> > No it does not, the reference has not any more meanings than being a
> > reference.
>
> Exactly what "does not"? What are you answering to? Please, learn how
> to quote when responding to a newsgroup message, this will make
> everyone's life easier.
>
> > (The garbage collector runs by a time, by instance as there is low memory,
> > it is not a kind of processor occupier).
>
> I'll do my best trying to translate this phrase, but don't blame me if
> the meaning comes out wrong (next time, please try simpler phrases if
> English is difficult for you):
> "The garbage collector runs in time intervals, for instance when there

(I'll continue as if nothing had happened) =))

"The garbage collector runs in time intervals, for instance when there
is low memory. It's not a kid of" (what? sorry, can't get the meaning
of that).


> > It should be disposed, but be aware that the method dispose means dispose
> > unmanaged resources, that has not much to do with disposing an object,
> > disposing of an object is done by the GC.

I'm perfectly aware of what dispose does: it allows an object to free
resources -- memory handles, file handles, etc) which it may be
holding whithout the need to wait for the garbage collector to kick in
for those resources to be freed. Since the OP is holding reference at
form scope to a control (which means, a window handle, possibly a DC
handle, depending on how the control works, and who know what other
handles), then it does make sense to me that 1) the object myst be
disposed. Itthe variable must be assinged to nothing (that will allow
the GC to collect
Show quoteHide quote
>
> > You be right, that as the class which is used is not made by using by
> > instance a component or a form template, then the implementation of Idispose
> > should be done as well. (Don't get the idea that solely using the dispose
> > (unmanaged resources) method is implementing that as often is written in
> > forums or newsgroups.)
>
> > Cor
>
> > "Branco" <branco.medei***@gmail.com> wrote in message
>
> >news:824a015b-43b0-4887-b859-9871432ff5f4@v4g2000vba.googlegroups.com...
>
> > > Cor Ligthert[MVP] wrote:
>
> > >> Be aware that setting the reference to nothing makes not sense.
>
> > > Correct me if I'm wrong, but it seems to me that if the reference
> > > being set to nothing is at form level then it may "free" the object to
> > > be garbage-collected (if all other references were also freed), even
> > > more if the top level variable is declared WithEvents (as is the case
> > > with the OP's object).
>
> > > And since it's a control, I guess it should be disposed alltogether.
>
> > > Regards,
>
> > > Branco.- Hide quoted text -
>
> > - Show quoted text -- Hide quoted text -
>
> - Show quoted text -
Author
15 May 2009 2:53 PM
Branco
Branco wrote:

Ouch! Maybe those warning to don't edit the message in the reader
itself are true after all. Sorry again for the message being sent
before it's finished. Turning to Notepad, now.

Show quoteHide quote
> Branco wrote:
> > Cor Ligthert[MVP] wrote:
>
> OOps, sorry for the unnedited message. Apparently my laptop got a life
> of its own and sent the message while it was still being edited. Myff
> appfollopgieffs toff efryonneff (takes off foot from mouth).
>
>
>
>
>
> > > Branco,
>
> > > No it does not, the reference has not any more meanings than being a
> > > reference.
>
> > Exactly what "does not"? What are you answering to? Please, learn how
> > to quote when responding to a newsgroup message, this will make
> > everyone's life easier.
>
> > > (The garbage collector runs by a time, by instance as there is low memory,
> > > it is not a kind of processor occupier).
>
> > I'll do my best trying to translate this phrase, but don't blame me if
> > the meaning comes out wrong (next time, please try simpler phrases if
> > English is difficult for you):
> > "The garbage collector runs in time intervals, for instance when there
>
> (I'll continue as if nothing had happened) =))
>
> "The garbage collector runs in time intervals, for instance when there
> is low memory. It's not a kid of" (what? sorry, can't get the meaning
> of that).
>
> > > It should be disposed, but be aware that the method dispose means dispose
> > > unmanaged resources, that has not much to do with disposing an object,
> > > disposing of an object is done by the GC.
>
> I'm perfectly aware of what dispose does: it allows an object to free
> resources -- memory handles, file handles, etc) which it may be
> holding whithout the need to wait for the garbage collector to kick in
> for those resources to be freed. Since the OP is holding reference at
> form scope to a control (which means, a window handle, possibly a DC
> handle, depending on how the control works, and who know what other
> handles), then it does make sense to me that 1) the object myst be
> disposed. Itthe variable must be assinged to nothing (that will allow
> the GC to collect
>
<snip>
Author
15 May 2009 2:59 PM
Co
On 15 mei, 16:53, Branco <branco.medei***@gmail.com> wrote:
Show quoteHide quote
> Branco wrote:
>
> Ouch! Maybe those warning to don't edit the message in the reader
> itself are true after all. Sorry again for the message being sent
> before it's finished. Turning to Notepad, now.
>
>
>
> > Branco wrote:
> > > Cor Ligthert[MVP] wrote:
>
> > OOps, sorry for the unnedited message. Apparently my laptop got a life
> > of its own and sent the message while it was still being edited. Myff
> > appfollopgieffs toff efryonneff (takes off foot from mouth).
>
> > > > Branco,
>
> > > > No it does not, the reference has not any more meanings than being a
> > > > reference.
>
> > > Exactly what "does not"? What are you answering to? Please, learn how
> > > to quote when responding to a newsgroup message, this will make
> > > everyone's life easier.
>
> > > > (The garbage collector runs by a time, by instance as there is low memory,
> > > > it is not a kind of processor occupier).
>
> > > I'll do my best trying to translate this phrase, but don't blame me if
> > > the meaning comes out wrong (next time, please try simpler phrases if
> > > English is difficult for you):
> > > "The garbage collector runs in time intervals, for instance when there
>
> > (I'll continue as if nothing had happened) =))
>
> > "The garbage collector runs in time intervals, for instance when there
> > is low memory. It's not a kid of" (what? sorry, can't get the meaning
> > of that).
>
> > > > It should be disposed, but be aware that the method dispose means dispose
> > > > unmanaged resources, that has not much to do with disposing an object,
> > > > disposing of an object is done by the GC.
>
> > I'm perfectly aware of what dispose does: it allows an object to free
> > resources -- memory handles, file handles, etc) which it may be
> > holding whithout the need to wait for the garbage collector to kick in
> > for those resources to be freed. Since the OP is holding reference at
> > form scope to a control (which means, a window handle, possibly a DC
> > handle, depending on how the control works, and who know what other
> > handles), then it does make sense to me that 1) the object myst be
> > disposed. Itthe variable must be assinged to nothing (that will allow
> > the GC to collect
>
> <snip>

Folks,

I put in a MonthCalendar and in the DataChanged event I check if there
was a date clicked
or another part of the Calendar. When it is a date then the
MonthCalendar will be hidden and disposed.

Private Sub m_picker_DateChanged(ByVal sender As Object, _
    ByVal e As DateRangeEventArgs) Handles m_picker.DateChanged

        Dim hti As MonthCalendar.HitTestInfo = m_picker.HitTest
(m_picker.PointToClient(MousePosition))

        'Display the Start and End property values of
        'the SelectionRange object in the text boxes.
        Me.tbVerloopt.Text =
m_picker.SelectionRange.Start.Date.ToShortDateString()
        Me.tbVerloopt.Text =
m_picker.SelectionRange.End.Date.ToShortDateString()

        'if we clicked on a date then hide the Calendar
        If hti.HitArea = MonthCalendar.HitArea.Date Then
            m_picker.Hide()
            tbVerloopt.Parent.Controls.Remove(m_picker)
            m_picker = Nothing
            bCalOpened = False
        End If
    End Sub

Marco
Author
15 May 2009 4:09 PM
BillE
I think Cor expresses himself well, compared to some!

Show quoteHide quote
"Branco" <branco.medei***@gmail.com> wrote in message
news:ec2691f5-5ad4-4ad0-91e1-9e4351adb550@21g2000vbk.googlegroups.com...
> Branco wrote:
>
> Ouch! Maybe those warning to don't edit the message in the reader
> itself are true after all. Sorry again for the message being sent
> before it's finished. Turning to Notepad, now.
>
>> Branco wrote:
>> > Cor Ligthert[MVP] wrote:
>>
>> OOps, sorry for the unnedited message. Apparently my laptop got a life
>> of its own and sent the message while it was still being edited. Myff
>> appfollopgieffs toff efryonneff (takes off foot from mouth).
>>
>>
>>
>>
>>
>> > > Branco,
>>
>> > > No it does not, the reference has not any more meanings than being a
>> > > reference.
>>
>> > Exactly what "does not"? What are you answering to? Please, learn how
>> > to quote when responding to a newsgroup message, this will make
>> > everyone's life easier.
>>
>> > > (The garbage collector runs by a time, by instance as there is low
>> > > memory,
>> > > it is not a kind of processor occupier).
>>
>> > I'll do my best trying to translate this phrase, but don't blame me if
>> > the meaning comes out wrong (next time, please try simpler phrases if
>> > English is difficult for you):
>> > "The garbage collector runs in time intervals, for instance when there
>>
>> (I'll continue as if nothing had happened) =))
>>
>> "The garbage collector runs in time intervals, for instance when there
>> is low memory. It's not a kid of" (what? sorry, can't get the meaning
>> of that).
>>
>> > > It should be disposed, but be aware that the method dispose means
>> > > dispose
>> > > unmanaged resources, that has not much to do with disposing an
>> > > object,
>> > > disposing of an object is done by the GC.
>>
>> I'm perfectly aware of what dispose does: it allows an object to free
>> resources -- memory handles, file handles, etc) which it may be
>> holding whithout the need to wait for the garbage collector to kick in
>> for those resources to be freed. Since the OP is holding reference at
>> form scope to a control (which means, a window handle, possibly a DC
>> handle, depending on how the control works, and who know what other
>> handles), then it does make sense to me that 1) the object myst be
>> disposed. Itthe variable must be assinged to nothing (that will allow
>> the GC to collect
>>
> <snip>
Author
15 May 2009 3:38 PM
Branco
Cor Ligthert[MVP] wrote:
> Branco,
>
> No it does not, the reference has not any more meanings than being a
> reference.
>
> (The garbage collector runs by a time, by instance as there is low memory,
> it is not a kind of processor occupier).
>
> It should be disposed, but be aware that the method dispose means dispose
> unmanaged resources, that has not much to do with disposing an object,
> disposing of an object is done by the GC.

Exactly: dispose is a way for the programmer to tell the object: "ok,
I'm done with you, release your resources now and be ready to die when
the garbage collector thinks it's time to get rid of you".

Since the object in point (a control) does implement IDispose (lemme
check... yes, it does [1]) and *may* be holding to an unknown number
of resources, it's fair to call Dispose on the object when the OP is
done with it (which is implied to when the object is set to nothing in
the OP's code).

NOw, setting the object to Nothing has other implications as well:
since the object is declared at class level (where it is declared
"WithEvents"), then, not seting the object to Nothing means that --
even though it has been disposed -- a live reference to it is still
active at class level, but the object being referenced is half dead
and in an unpredictable state. Not only that, it won't be collected by
the GC if the need arises, because there will be a class level
reference to it still active and the GC won't touch that. Therefore
the correct sequence of actions should be, in my opinion (quoting the
original message):

[Co wrote:]
> Private Sub CloseUpDTP(ByVal sender As System.Object, _
> ByVal e As System.EventArgs) Handles m_picker.CloseUp
>   Me.Controls.Remove(m_picker)
    'remove references of m_picker from wherever
    'else it was added to. and then:

    m_Picker.Dispose

>   m_picker = Nothing
>   End Sub

Note, however, that in the original code the OP is not calling
Dispose, but is (correctly) assigning Nothing to the variable. Even
though Dispose wasn't called, at least the references for the object
were freed, which means that the garbage collector will address that
object if more memory is needed (and dispose will be called then,
automatically). So, assigning Nothing to the variable *has* a meaning,
after all, despite the fact that Dispose wasn't called (even though it
should).

[Cor Lightert]
> You be right, that as the class which is used is not made by using by
> instance a component or a form template, then the implementation of Idispose
> should be done as well.


HOnestly, I can't get exactly what you are implying. What are you
trying to say? That I'd be right if the object the OP is using was a
component?  (well, it is, so I'm right);  but then, again, Who is
talking about implementing IDIsposable? The component the OP is using
implements that, so Dispose should be called on it. And since the
variable is at class level, Nothing *must* be assigned to it after
dispose was called.

>(Don't get the idea that solely using the dispose
> (unmanaged resources) method is implementing that as often is written in
> forums or newsgroups.)
<snip>

Sorry I couldn't understand what you're trying to say. Have you
considered using a translator? Maybe you get better results at
expressing what you are trying to.

Regards,

Branco.

[1] http://msdn.microsoft.com/en-us/library/system.windows.forms.datetimepicker_members.aspx
Author
15 May 2009 2:53 AM
Jack Jackson
I believe setting the Parent property to Nothing will also work.

In this case I'm sure the property holding the reference to the object
is a form level property, so it does make a lot of sense to set it to
Nothing.  The object should also be Disposed.

On Thu, 14 May 2009 18:56:14 +0200, "Cor Ligthert[MVP]"
<Notmyfirstn***@planet.nl> wrote:

Show quoteHide quote
>Cor
>
> tbVerloopt.Parent.Controls.Remove(m_picker)
>
>Be aware that setting the reference to nothing makes not sense.
>
>Cor
>
>
>"Co" <vonclausow***@gmail.com> wrote in message
>news:312235e2-846c-4db9-a578-e068114469eb@s28g2000vbp.googlegroups.com...
>On 14 mei, 12:44, "Andrew Morton" <a***@in-press.co.uk.invalid> wrote:
>> Co wrote:
>> > I created a DateTimePicker to use on my form when I doubleclick a
>> > textbox.
>> > The problem is that I can't remove it when I used it.
>>
>> > Private Sub CreateDTP()
>> >...
>> > tbVerloopt.Parent.Controls.Add(m_picker)
>> >...
>> > Private Sub CloseUpDTP(ByVal sender As System.Object, _
>> > ByVal e As System.EventArgs) Handles m_picker.CloseUp
>>
>> > Me.Controls.Remove(m_picker)
>>
>> Shouldn't you be removing it from the place you added it to, i.e.
>> tbVerloopt.Parent.Controls?
>>
>> Andrew
>
>Yes you are right but how?
>
>Marco
Author
15 May 2009 4:45 AM
Cor Ligthert[MVP]
Jack,

First look at the code from the OP before you reply.

The DateTimePicker is created programmatically into a method. It goes out of
scope as soon as the method ends.
It will however not been garbaged as long the DateTimePicker is referenced
by the controls of the tabpage.

The GC disposes the object.

Cor


Show quoteHide quote
"Jack Jackson" <jjackson-***@cinnovations.net> wrote in message
news:i6mp0592c5gnfg78k9nejgqubkssfd0k7g@4ax.com...
>I believe setting the Parent property to Nothing will also work.
>
> In this case I'm sure the property holding the reference to the object
> is a form level property, so it does make a lot of sense to set it to
> Nothing.  The object should also be Disposed.
>
> On Thu, 14 May 2009 18:56:14 +0200, "Cor Ligthert[MVP]"
> <Notmyfirstn***@planet.nl> wrote:
>
>>Cor
>>
>> tbVerloopt.Parent.Controls.Remove(m_picker)
>>
>>Be aware that setting the reference to nothing makes not sense.
>>
>>Cor
>>
>>
>>"Co" <vonclausow***@gmail.com> wrote in message
>>news:312235e2-846c-4db9-a578-e068114469eb@s28g2000vbp.googlegroups.com...
>>On 14 mei, 12:44, "Andrew Morton" <a***@in-press.co.uk.invalid> wrote:
>>> Co wrote:
>>> > I created a DateTimePicker to use on my form when I doubleclick a
>>> > textbox.
>>> > The problem is that I can't remove it when I used it.
>>>
>>> > Private Sub CreateDTP()
>>> >...
>>> > tbVerloopt.Parent.Controls.Add(m_picker)
>>> >...
>>> > Private Sub CloseUpDTP(ByVal sender As System.Object, _
>>> > ByVal e As System.EventArgs) Handles m_picker.CloseUp
>>>
>>> > Me.Controls.Remove(m_picker)
>>>
>>> Shouldn't you be removing it from the place you added it to, i.e.
>>> tbVerloopt.Parent.Controls?
>>>
>>> Andrew
>>
>>Yes you are right but how?
>>
>>Marco
Author
15 May 2009 6:31 AM
Jack Jackson
Cor,

Why don't YOU look at the code from the OP:

Dim WithEvents m_picker As DateTimePicker
    Private Sub CreateDTP()

m_picker is defined outside all of the methods.  That makes it a class
level field.  It does not go out of scope when the method exits.

Yes the GC will eventually dispose of it if there are no references,
but if an object implements IDispose, you should call its Dispose()
when you are finished with it.  Waiting for the garbage collector to
get rid of it causes its resources to be kept for far longer than
necessary.

On Fri, 15 May 2009 06:45:02 +0200, "Cor Ligthert[MVP]"
<Notmyfirstn***@planet.nl> wrote:

Show quoteHide quote
>Jack,
>
>First look at the code from the OP before you reply.
>
>The DateTimePicker is created programmatically into a method. It goes out of
>scope as soon as the method ends.
>It will however not been garbaged as long the DateTimePicker is referenced
>by the controls of the tabpage.
>
>The GC disposes the object.
>
>Cor
>
>
>"Jack Jackson" <jjackson-***@cinnovations.net> wrote in message
>news:i6mp0592c5gnfg78k9nejgqubkssfd0k7g@4ax.com...
>>I believe setting the Parent property to Nothing will also work.
>>
>> In this case I'm sure the property holding the reference to the object
>> is a form level property, so it does make a lot of sense to set it to
>> Nothing.  The object should also be Disposed.
>>
>> On Thu, 14 May 2009 18:56:14 +0200, "Cor Ligthert[MVP]"
>> <Notmyfirstn***@planet.nl> wrote:
>>
>>>Cor
>>>
>>> tbVerloopt.Parent.Controls.Remove(m_picker)
>>>
>>>Be aware that setting the reference to nothing makes not sense.
>>>
>>>Cor
>>>
>>>
>>>"Co" <vonclausow***@gmail.com> wrote in message
>>>news:312235e2-846c-4db9-a578-e068114469eb@s28g2000vbp.googlegroups.com...
>>>On 14 mei, 12:44, "Andrew Morton" <a***@in-press.co.uk.invalid> wrote:
>>>> Co wrote:
>>>> > I created a DateTimePicker to use on my form when I doubleclick a
>>>> > textbox.
>>>> > The problem is that I can't remove it when I used it.
>>>>
>>>> > Private Sub CreateDTP()
>>>> >...
>>>> > tbVerloopt.Parent.Controls.Add(m_picker)
>>>> >...
>>>> > Private Sub CloseUpDTP(ByVal sender As System.Object, _
>>>> > ByVal e As System.EventArgs) Handles m_picker.CloseUp
>>>>
>>>> > Me.Controls.Remove(m_picker)
>>>>
>>>> Shouldn't you be removing it from the place you added it to, i.e.
>>>> tbVerloopt.Parent.Controls?
>>>>
>>>> Andrew
>>>
>>>Yes you are right but how?
>>>
>>>Marco
Author
15 May 2009 8:28 AM
Co
On 15 mei, 08:31, Jack Jackson <jjackson-***@cinnovations.net> wrote:
Show quoteHide quote
> Cor,
>
> Why don't YOU look at the code from the OP:
>
> Dim WithEvents m_picker As DateTimePicker
>     Private Sub CreateDTP()
>
> m_picker is defined outside all of the methods.  That makes it a class
> level field.  It does not go out of scope when the method exits.
>
> Yes the GC will eventually dispose of it if there are no references,
> but if an object implements IDispose, you should call its Dispose()
> when you are finished with it.  Waiting for the garbage collector to
> get rid of it causes its resources to be kept for far longer than
> necessary.
>
> On Fri, 15 May 2009 06:45:02 +0200, "Cor Ligthert[MVP]"
>
> <Notmyfirstn***@planet.nl> wrote:
> >Jack,
>
> >First look at the code from the OP before you reply.
>
> >The DateTimePicker is created programmatically into a method. It goes out of
> >scope as soon as the method ends.
> >It will however not been garbaged as long the DateTimePicker is referenced
> >by the controls of the tabpage.
>
> >The GC disposes the object.
>
> >Cor
>
> >"Jack Jackson" <jjackson-***@cinnovations.net> wrote in message
> >news:i6mp0592c5gnfg78k9nejgqubkssfd0k7g@4ax.com...
> >>I believe setting the Parent property to Nothing will also work.
>
> >> In this case I'm sure the property holding the reference to the object
> >> is a form level property, so it does make a lot of sense to set it to
> >> Nothing.  The object should also be Disposed.
>
> >> On Thu, 14 May 2009 18:56:14 +0200, "Cor Ligthert[MVP]"
> >> <Notmyfirstn***@planet.nl> wrote:
>
> >>>Cor
>
> >>> tbVerloopt.Parent.Controls.Remove(m_picker)
>
> >>>Be aware that setting the reference to nothing makes not sense.
>
> >>>Cor
>
> >>>"Co" <vonclausow***@gmail.com> wrote in message
> >>>news:312235e2-846c-4db9-a578-e068114469eb@s28g2000vbp.googlegroups.com....
> >>>On 14 mei, 12:44, "Andrew Morton" <a***@in-press.co.uk.invalid> wrote:
> >>>> Co wrote:
> >>>> > I created a DateTimePicker to use on my form when I doubleclick a
> >>>> > textbox.
> >>>> > The problem is that I can't remove it when I used it.
>
> >>>> > Private Sub CreateDTP()
> >>>> >...
> >>>> > tbVerloopt.Parent.Controls.Add(m_picker)
> >>>> >...
> >>>> > Private Sub CloseUpDTP(ByVal sender As System.Object, _
> >>>> > ByVal e As System.EventArgs) Handles m_picker.CloseUp
>
> >>>> > Me.Controls.Remove(m_picker)
>
> >>>> Shouldn't you be removing it from the place you added it to, i.e.
> >>>> tbVerloopt.Parent.Controls?
>
> >>>> Andrew
>
> >>>Yes you are right but how?
>
> >>>Marco

Guys,

Is there a way to simulate a dropdown of the DTP?
I have the code which creates a DTP on form but I want it to show the
calendar when it is created, not the textbox.

        m_picker = New DateTimePicker
        m_picker.Format = DateTimePickerFormat.Short
        m_picker.Location = TableLayoutPanel1.Location
        m_picker.Size = tbGewijzigd.Size
        m_picker.CalendarMonthBackground = Color.Beige
        tbVerloopt.Parent.Controls.Add(m_picker)
        m_picker.Select()
        Dim tempPos As Point
        Dim R As Long = GetCursorPos(tempPos)
        mouse_event(MOUSEEVENTF_LEFTDOWN, tempPos.X, tempPos.Y, 0, 0)

My code doesn't work however.

Marco
Author
15 May 2009 9:38 AM
Cor Ligthert[MVP]
Jack,

> Waiting for the garbage collector to
> get rid of it causes its resources to be kept for far longer than
> necessary.

You are right the reference to the DateTimePicker sits on class level.

Be aware that it is not a DateTimePicker, it is not programming from 40
years ago, it is a reference.

Cor
Author
15 May 2009 11:14 AM
Co
Show quote Hide quote
On 15 mei, 11:38, "Cor Ligthert[MVP]" <Notmyfirstn***@planet.nl>
wrote:
> Jack,
>
> > Waiting for the garbage collector to
> > get rid of it causes its resources to be kept for far longer than
> > necessary.
>
> You are right the reference to the DateTimePicker sits on class level.
>
> Be aware that it is not a DateTimePicker, it is not programming from 40
> years ago, it is a reference.
>
> Cor

Cor,

mea culpa, i'm new in the .net theatre.
But how could this be done?

MArco
Author
15 May 2009 12:55 PM
Cor Ligthert[MVP]
Co,

Do you mean you want to use the monthcalendar instead of the datetimepicker,
it is  almost the same but is not a dropdown

http://msdn.microsoft.com/en-us/library/system.windows.forms.monthcalendar.aspx

Be aware that newsgroups are as well discussion groups, it is not only
helping.
In your question are the most replies not to help you direct with your
question however more about what is there around.
(Not that this is a problem, that is were newsgroup are for)

Andrew gave you a correct answer, I replied on that with a piece of code in
the context of Andrew his message and wanted to explain something more
because things that were true in VB6 (often because of wrong behaviour) and
called best practice, are not true anymore for VB for Net.

Don't forget that VB for Net uses the same CLI as C# and C++ for Net. This
means that there have to be less flaws which are done with creating
solutions around them and then call those best practice.

Cor

Show quoteHide quote
"Co" <vonclausow***@gmail.com> wrote in message
news:e50d8369-2e24-44ab-9331-2acf19e1efe3@v17g2000vbb.googlegroups.com...
> On 15 mei, 11:38, "Cor Ligthert[MVP]" <Notmyfirstn***@planet.nl>
> wrote:
>> Jack,
>>
>> > Waiting for the garbage collector to
>> > get rid of it causes its resources to be kept for far longer than
>> > necessary.
>>
>> You are right the reference to the DateTimePicker sits on class level.
>>
>> Be aware that it is not a DateTimePicker, it is not programming from 40
>> years ago, it is a reference.
>>
>> Cor
>
> Cor,
>
> mea culpa, i'm new in the .net theatre.
> But how could this be done?
>
> MArco