how to remove a programmatically created DateTimePicker

C

Co

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.

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
 
C

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
 
C

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
 
C

Co

Jack,


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
 
C

Co

Jack,


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
 
C

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
 
C

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
 
B

Branco

Cor said:
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
 
B

Branco

Cor said:
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
 
B

Branco

Branco said:
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).
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.


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).


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
 
B

Branco

Branco said:
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).
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.


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).


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
 
B

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.
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).







(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).

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>
 
B

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.
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).







(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).

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>
 
C

Co

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.








<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
 
C

Co

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.








<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
 
B

Branco

Cor said:
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
 
B

Branco

Cor said:
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
 

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