|
-
Apr 20th, 2007, 06:19 AM
#1
Thread Starter
Lively Member
[2005] Dispose a MonthCalendar object
Hi everyone,
I've a textbox in which, when the DoubleClick event is calling, I control if the user has wrote a normal text (e.g. 'BUSINESS') or a date (e.g. 23/01/2007). in this last case I open a monthCalendar object and the user could select the new date.
Which method/event of this object I have to modify (I think DateChanged or DateSelected) in order to have in the textbox the date I've selected (this part is not a problem....I only need the best event where put the code in) and, after this operation, make a dispose of MonthCalendar object???
I tried this (oDt is the MonthCalendar Object and field1 the textbox where I want to put my date):
Code:
Private Sub oDt_DateSelected(ByVal sender As Object, ByVal e As System.Windows.Forms.DateRangeEventArgs) Handles oDt.DateSelected
Me.field1.Text = oDt.SelectionStart.Date.ToShortDateString
oDt.Dispose()
End Sub
but Vb returns me an error which sounds like it's impossibile to call an object which was deleted
Thank you in advance...
-
Apr 20th, 2007, 06:25 AM
#2
Re: [2005] Dispose a MonthCalendar object
If you dispose the calendar then you destroy it. Don't destroy objects if you need to use them again. If you want to show and hide a control then use the Show and Hide methods or set the Visible property. Also, I think you should be using a DateTimePicker, not a MonthCalendar.
-
Apr 20th, 2007, 07:29 AM
#3
Thread Starter
Lively Member
Re: [2005] Dispose a MonthCalendar object
Hi,
I know that dispose an object means destroy it, but is what I want.
The user will re-use the object only if has wrote a date string in the textbox and the date is wrong so, he want to correct it by selecting a new one on the calendar. There aren't any other uses of this object.
So I think MonthCalendar is better than DateTimePicker only because I need the "box" where it's possible to select month/Day and not the DropDownList typical of the DateTimePicker control.
About the error: after selecting a new date, I don't call again Doubleclick event. This is the entire code:
Code:
Private Sub field1_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles field1.DoubleClick
Dim nX, nY As Integer
If Me.field1.Enabled = True And Not Me.field1.ReadOnly Then
If IsDate(Me.field1.Text) Then
nX = Me.Top + Me.field1.Top + Me.field1.Left + 2
nY = Me.Top + Me.field1.Top + Me.field1.Height + 2
nWidthOld = Me.field1.Width
oDt.Left = nX
oDt.Top = nY
Me.Controls.Add(oDt)
oDt.SetDate(Me.field1.Text)
Me.field1.Anchor = AnchorStyles.Left + AnchorStyles.Right + AnchorStyles.Top
Me.Height = Me.Height + oDt.Height
Me.Width = Me.Width + oDt.Width
Me.Height = Me.Height + 50
AddHandler oDt.DateChanged, AddressOf oDt_DateSelected
End If
End If
End Sub
and this is the Dateselected method
Code:
Private Sub oDt_DateSelected(ByVal sender As Object, ByVal e As System.Windows.Forms.DateRangeEventArgs) Handles oDt.DateSelected
Me.field1.Text = oDt.SelectionStart.Date.ToShortDateString
Me.field1.Width = nWidthOld
Me.Height = Me.field1.Height
oDt.Dispose()
End Sub
Thank you.
-
Apr 20th, 2007, 08:54 AM
#4
Re: [2005] Dispose a MonthCalendar object
But you aren't creating any new object. You're trying to access members via the oDt variable, which still refers to the object you destroyed.
Think of it like this. You have a Car property. It refers to the car object that you own. You open your Car.Door property, start your Car.Engine property and call your Car.Drive method. Now, you have a crash and total your car, so it's destroyed. Can you go and open your Car.Door property? No you can't, because the car object that you Car property refers to is destroyed. You have to get a new car, i.e. assign a new car object to your Car property. Then you can access its member again.
The same is true of a MonthCalendar. If the oDt variable refers to a destroyed object you have to create a new MonthCalendar object and assign it to the variable:
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|