• 🏆 Texturing Contest #33 is OPEN! Contestants must re-texture a SD unit model found in-game (Warcraft 3 Classic), recreating the unit into a peaceful NPC version. 🔗Click here to enter!
  • It's time for the first HD Modeling Contest of 2024. Join the theme discussion for Hive's HD Modeling Contest #6! Click here to post your idea!

VB6 - Unload the Duplicated Form

Status
Not open for further replies.

NEL

NEL

Level 6
Joined
Mar 6, 2017
Messages
113
Duplicating the specified form in run-time is very useful in my system. The problem is when I created it, I can't unload it.

I have 3 forms (frmMain, frmSub, frmIDK):
Code:
' This code is for frmIDK
' frmIDK duplicates the frmMain when he pressed the Duplicate Button
Private Sub Duplicate_Click()
    Dim FRM As New frmMain
    FRM.Show
End

Code:
' This code is for frmMain
' frmMain duplicates the frmSub when he pressed the Duplicate Button
Private Sub Duplicate_Click()
    Dim FRM As New frmSub
    FRM.Show
End Sub

Code:
' This code is for frmSub
' frmSub unloads a frmMain when he pressed the Unload Button
Private Sub Unload_frmMain_Click()
    ' Unload ???
End Sub

User can spam whatever he want. The main problem here is how to save/store a duplicated frmMain?
 
Last edited:
Level 5
Joined
Jul 27, 2017
Messages
73
i think saving it in an instance variable should be enough to save it.
Using a List saving form-objects can be used to have acces to those forms and to use them at a remove function when you want to but you need to know which forms you want to remove when calling the Unload function.
 

NEL

NEL

Level 6
Joined
Mar 6, 2017
Messages
113
i think saving it in an instance variable should be enough to save it.
Using a List saving form-objects can be used to have acces to those forms and to use them at a remove function when you want to but you need to know which forms you want to remove when calling the Unload function.

I did, but you can't access the instance variable in other form because you can't declare the Public with an infinite array. Infinite Array is only works in Dim.

Code:
Dim frm() as New frmMain
 
Level 5
Joined
Jul 27, 2017
Messages
73
The following code worked for me (i hope that it is displayed correctly)
This code creates a new Form of type Fomr1 when the first button is pressed and disposes the highest form saved within the list Forms.
This will not dispose child forms of this one. (To do so you would have to call a function within the form you want to kill)
You can also save the "parent" form within a "child" form as instance variable if you wish to comminucate between them.

Code:
Public Class Form1

    Public Forms As List(Of Form1)

    Public Sub New()
        InitializeComponent()
        Forms = New List(Of Form1)
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim FRM As New Form1
        FRM.Show()
        Forms.Add(FRM)
    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        If (Forms IsNot Nothing AndAlso Forms.Count() > 0) Then
            Forms.ElementAt(Forms.Count() - 1).Dispose()
        End If
    End Sub

End Class
 

NEL

NEL

Level 6
Joined
Mar 6, 2017
Messages
113
The following code worked for me (i hope that it is displayed correctly)
This code creates a new Form of type Fomr1 when the first button is pressed and disposes the highest form saved within the list Forms.
This will not dispose child forms of this one. (To do so you would have to call a function within the form you want to kill)
You can also save the "parent" form within a "child" form as instance variable if you wish to comminucate between them.

Code:
Public Class Form1

    Public Forms As List(Of Form1)

    Public Sub New()
        InitializeComponent()
        Forms = New List(Of Form1)
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim FRM As New Form1
        FRM.Show()
        Forms.Add(FRM)
    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        If (Forms IsNot Nothing AndAlso Forms.Count() > 0) Then
            Forms.ElementAt(Forms.Count() - 1).Dispose()
        End If
    End Sub

End Class

I like your code btw, but you're using VB.net. :/
 
Status
Not open for further replies.
Top