6 Replies - 1204 Views - Last Post: 07 July 2016 - 01:08 AM Rate Topic: ***-- 2 Votes

#1 TrustsomeOne   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 22-June 16

Deleting everything in a form and then call it again

Posted 06 July 2016 - 06:26 AM

Hi, does any1 know how to clear everything in a form? My problem is that, when i start my program, it gives me a "calendar" just like in the picture, but i want, when i click that litle button, to delete (not clear) all the textbox, and then change the year to 2015
what im doing is using a method to generate all of those textboxes, and when the form loads, it shows up the calendar of the current year, and when i click the button i want to
year=year-1


After that, i want to empty out my form so i can generate the last year calendar
what im doing is

year=year-1
reload() 'this is the method i use to generate the calendar



sorry if this is confusing but my english ain't that great

forgot the pic -_-

Attached image(s)

  • Attached Image


Is This A Good Question/Topic? 0
  • +

Replies To: Deleting everything in a form and then call it again

#2 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 16479
  • View blog
  • Posts: 65,313
  • Joined: 12-June 08

Re: Deleting everything in a form and then call it again

Posted 06 July 2016 - 06:35 AM

Quote

to delete (not clear) all the textbox

Why would you want to remove the controls when you should just be clearing/filling their values?
Was This Post Helpful? 0
  • +
  • -

#3 TrustsomeOne   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 22-June 16

Re: Deleting everything in a form and then call it again

Posted 06 July 2016 - 06:41 AM

This is why

  Public Function myfunc(ByVal nummes As Integer, ByVal ano As Integer, ByVal pridiames As DateTime, ByVal ultdiames As Integer) As Object
        Dim z As Integer
        Select Case pridiames.DayOfWeek
            Case Is = DayOfWeek.Sunday
                z = 111
            Case Is = DayOfWeek.Monday
                z = 138
            Case Is = DayOfWeek.Tuesday
                z = 166
            Case Is = DayOfWeek.Wednesday
                z = 194
            Case Is = DayOfWeek.Thursday
                z = 222
            Case Is = DayOfWeek.Friday
                z = 250
            Case Is = DayOfWeek.Saturday
                z = 278
        End Select

        For i = 1 To ultdiames
            Dim txt As New TextBox
            txt.Visible = True
            Dim nomed As New DateTime(ano, nummes + 1, i)
            If nomed.DayOfWeek = DayOfWeek.Saturday Or nomed.DayOfWeek = DayOfWeek.Sunday Then
                txt.ForeColor = Color.Red
            Else
                txt.ForeColor = Color.Black
            End If
            txt.Font = New Font(txt.Font.FontFamily, 10, FontStyle.Bold)
            txt.Size = New System.Drawing.Size(22, 18)
            txt.Location = New Drawing.Point(z, 50 * nummes + 35)
            Controls.Add(txt)
            txt.Text = i
            z = z + 28
        Next i
    End Function


Was This Post Helpful? 0
  • +
  • -

#4 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 16479
  • View blog
  • Posts: 65,313
  • Joined: 12-June 08

Re: Deleting everything in a form and then call it again

Posted 06 July 2016 - 06:44 AM

Then that's a seriously flawed way of doing things. Why would you spin the wheels creating controls at runtime when you have that whole designer you can lay down the controls and be done with them?
Was This Post Helpful? 1
  • +
  • -

#5 TrustsomeOne   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 22-June 16

Re: Deleting everything in a form and then call it again

Posted 06 July 2016 - 06:51 AM

Ask my teacher <.< xD

He wants everything generated by code

Okay i got it, had to go an arabic website '-'
Controls.clear()


Easier then i tought
Was This Post Helpful? 0
  • +
  • -

#6 IronRazer   User is offline

  • Custom Control Freak
  • member icon

Reputation: 1545
  • View blog
  • Posts: 3,871
  • Joined: 01-February 13

Re: Deleting everything in a form and then call it again

Posted 06 July 2016 - 09:50 AM

Actually, calling Controls.Clear will only removing the controls from the Form`s Control Collection causing you to think they are gone because you don`t see them anymore. However, they do not get disposed and cleared from the memory. This leaves all those controls floating around in the memory without a way to dispose them. That is called a memory leak which you do not want.

You need to call the Dispose Method of all those controls. To do that you need to iterate backwards through the Controls using a For Next Loop and call the Dispose Method of each one of them. For Example,
        'iterate backwards from the last control`s index to the first control`s index
        For indx As Integer = Me.Controls.Count - 1 To 0 Step -1

            'make sure the control at this index is a TextBox type before Disposing it. If you dont want to dispose all the controls.
            If TypeOf Controls(indx) Is TextBox Then

                Controls(indx).Dispose() 'call the Dispose method of the TextBox control.
            End If
        Next



However, if you have TextBoxes on the Form that you don`t want disposed then that is not going to work so well for you. The Better way would be to Add the TextBoxes that you create to a class scoped List(Of TextBox). Then you can iterate through the List in reverse and Dispose them. That would assure you only Dispose just those TextBoxes. You also would not need to check if they are TextBox types in the loop either.
Was This Post Helpful? 0
  • +
  • -

#7 maceysoftware   User is offline

  • Member Title
  • member icon

Reputation: 396
  • View blog
  • Posts: 1,672
  • Joined: 07-September 13

Re: Deleting everything in a form and then call it again

Posted 07 July 2016 - 01:08 AM

IronRazors code is the way i would do it however just for completion i thought i would mention something i noticed.

Currently your "solution" of me.controls.clear is doing something that IronRazors is not, it shouldn't matter in your case but if you used IronRazors solution is another project where the circumstances are slightly different you may find the code doesn't quite work.

The Code me.Controls.Clear - well clears all the controls, as IronRazor has already mention you don't want this for x, y and z, whereby IronRazors code will only remove and dispose textboxes which is directly in the forms collection.

So if your have 3 textboxes directly placed on a form and then put a panel on the form with another 3 textboxes and run IronRazor's code then only the textboxes that are directly placed on the form will be disposed and the other three textboxes remain.

What you would need to do is while cycling through the controls also check to see if the current control holds any controls itself

Something like:

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        ' Ok so i am doing this on Form Load which you won't want to do however it is just an example

        ' if you only want to remove the type of control from the parent control directly.
        ' in this case the parent control is the form
        RemoveTextboxes(Me, False)

        ' If you want to resursive down and remove all textboxes 
        RemoveTextboxes(Me, True)

    End Sub

    Private Sub RemoveTextboxes(parentControl As Control, recursive As Boolean)

        'iterate backwards from the last control`s index to the first control`s index
        For index As Integer = parentControl.Controls.Count - 1 To 0 Step -1
            ' first see if we want to actually do the recursive
            If recursive Then
                ' If the current control has controls of its own
                If parentControl.Controls(index).Controls.Count > 0 Then
                    ' Recall this routine but passing in the container control
                    RemoveTextboxes(parentControl.Controls(index), recursive)
                End If
            End If

            'make sure the control at this index is a TextBox type before Disposing it. If you dont want to dispose all the controls.
            If TypeOf parentControl.Controls(index) Is TextBox Then
                parentControl.Controls(index).Dispose() 'call the Dispose method of the TextBox control.
            End If

        Next

    End Sub





I mention this for 2 reasons first you may find some textboxes don't disappear and wonder why, also this could be another way to do what IronRazor was talking about here:

However, if you have TextBoxes on the Form that you don`t want disposed then that is not going to work so well for you. The Better way would be to Add the TextBoxes that you create to a class scoped List(Of TextBox). Then you can iterate through the List in reverse and Dispose them. That would assure you only Dispose just those TextBoxes. You also would not need to check if they are TextBox types in the loop either.

Depending on how the textboxes are placed which you want/don't want to dispose you could place them into a separate panel, Then either remove from the form or the panel depending on which ones you want to keep.

What i would say is don't start putting separate textboxes in separate panels that will just be horrible but if for example it was just the day number textboxes you want to remove because you know the months don't change year to year. then this would be a excellent place where you can stick them into a panel.

This will remove the need to keep reference to of the text boxes in a list, however i personally would most likely go down the list route if i was doing it.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1