how to remove a programmatically created DateTimePicker

C

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
 
A

Andrew Morton

Co said:
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
 
A

Andrew Morton

Co said:
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
 
C

Cor Ligthert[MVP]

Cor

tbVerloopt.Parent.Controls.Remove(m_picker)

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

Cor


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
 
C

Cor Ligthert[MVP]

Cor

tbVerloopt.Parent.Controls.Remove(m_picker)

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

Cor


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
 
B

Branco

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

Branco

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

Co

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
 
C

Co

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
 
J

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

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

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
 
C

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
 
C

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
 
C

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
 
J

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

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

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