Results 1 to 20 of 20

Thread: Unloading a Form

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Aug 2000
    Location
    UK
    Posts
    78

    Unloading a Form

    I'm looking for the equivalent of the unload.form2 command in VB.NET and what event that needs to be placed in.

    In VB6:
    Click button1 on form 1 event: form2.show:form1.hide
    Click button1 on form 2 event: form1.show:form2.hide

    and in my form1.unload I would have Unload.Form2:Unload.Me


    Now I've got the showing/hiding of the forms OK with VB.NET, I use (in a module):
    Public MainForm As New Form1()
    Public ProgressForm As New Form2()

    I can show/hide with mainform.hide/progressform.show e.t.c. all works fine but I know the progressform (form2) is not unloading when I quite the app from form1.

    I've looked at the 'closing' and 'closed' events of form1 and added a progressform.dispose but no good as when I quit the app the VB.NET IDE has not stopped.

    Anyone?

  2. #2

    Thread Starter
    Lively Member
    Join Date
    Aug 2000
    Location
    UK
    Posts
    78
    No one?

    Is this VB.NET going from bad to worse?

    All I want is the equivelent of: unload form2

  3. #3
    PowerPoster Lethal's Avatar
    Join Date
    Oct 2000
    Location
    Ohio
    Posts
    2,496
    I haven't gotten to reading up on the Windows Forms yet, but maybe try:

    Code:
        FormName.Dispose()

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Aug 2000
    Location
    UK
    Posts
    78
    Hi, thanks for the reply.

    I've tried both formname.dispose and formname.close, neither worked .... or maybe I'm just putting them in the incorrect event???, I don't know. I'm putting them in the main forms closing, closed and leave, still nothing. :-(

  5. #5
    Addicted Member ZanM's Avatar
    Join Date
    Oct 1999
    Location
    The here and now.
    Posts
    191

    don't know but

    try

    Unload FormName

    or

    MyBase.Dispose

    and if the fails use the DestroyWindow API
    Magiaus
    Visual Basic 6.0 SP5
    Visual C++ 6.0 SP5


    The only sovereign you can allow to rule you is reason.

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Aug 2000
    Location
    UK
    Posts
    78
    No good

  7. #7
    New Member
    Join Date
    Mar 2002
    Location
    New Jersey
    Posts
    3
    Use Me.Dispose (True)

    It works for me


    EDIT:

    And if you know the name of the form then use Form1.ActiveForm.Dispose()

    Assuming the name of the form is Form1
    Last edited by Axyun; Mar 24th, 2002 at 10:46 PM.

  8. #8
    Addicted Member ZanM's Avatar
    Join Date
    Oct 1999
    Location
    The here and now.
    Posts
    191

    Tested & Works

    -_-

    Code:
         frm.Dispose() 'Works
    i used here in a note pade i am working on

    look at:
    Private Sub mnuFileOpen_Clicked
    Private Sub mnuFileSave_Clicked
    Code:
    Option Explicit On
    Option Strict On
    Option Compare Binary
    
    Imports System
    Imports System.ComponentModel
    Imports System.Drawing
    Imports System.Drawing.Text
    Imports System.Windows.Forms
    Imports Microsoft.VisualBasic
    
    Namespace VBNotepad
    	Public Class frmPad
    		Inherits System.Windows.Forms.Form
    
    		Private frm As New System.Windows.Forms.Form
    
    		Private WithEvents CodePane As System.Windows.Forms.TextBox
    
    		Private FileName as String = "New Source.vb"
    		Private FilePath as String = "C:\"
    		Private isChanged as Boolean = False
    		Private AlreadyExists As Boolean = False
    		
    		<STAThread()> Shared Sub Main()
    			System.Windows.Forms.Application.Run(New frmPad())
    		End Sub
    
    		Public Sub New()
    			'
    			MyBase.New()
    			
    			Call AddMenus()
    			Call AddCodePane()
    			
    			Me.Text = "VB.Net Simple Code Pad"
    			Me.TopMost = True
    		End Sub
    
    		Overrides Protected Sub OnResize(ByVal e As EventArgs )
    			CodePane.Size = New Size(Me.ClientSize.Width - 1, Me.ClientSize.Height - 1)
    		End Sub
    
    
    		'Menu Creation and Event Handlers
    		'***********************************************************************************************		
    		Private Sub AddMenus()
    			Dim mnuMain As New MainMenu
    			Dim mnuFile As MenuItem = mnuMain.MenuItems.Add("&File")
    			
    			'File Menu
    			mnuFile.MenuItems.Add("&New", New EventHandler(AddressOf Me.mnuFileNew_Clicked))
    			mnuFile.MenuItems.Add("&Open", New EventHandler(AddressOf Me.mnuFileOpen_Clicked))
    			mnuFile.MenuItems.Add("&Save", New EventHandler(AddressOf Me.mnuFileSave_Clicked))
    			mnuFile.MenuItems.Add("Save Copy",New EventHandler(AddressOf Me.mnuFileSaveCopy_Clicked))
    			mnuFile.MenuItems.Add("-")	'Seperator
    			mnuFile.MenuItems.Add("Build", New EventHandler(AddressOf Me.mnuFileBuild_Clicked))
    			mnuFile.MenuItems.Add("-")
    			mnuFile.MenuItems.Add("E&xit", New EventHandler(AddressOf Me.mnuFileExit_Clicked))
    			
    			Me.Menu = mnuMain
    		End Sub
    
    		Private Sub mnuFileNew_Clicked(ByVal sender as Object, ByVal e As System.EventArgs)
    			If isChanged = True Then
    				Call SaveFile()
    			End If
    			CodePane.Text = "" 
    		End Sub
    
    		Private Sub mnuFileOpen_Clicked(ByVal sender as Object, ByVal e As System.EventArgs)
    			frm.Show
    		End Sub
    
    		Private Sub mnuFileSave_Clicked(ByVal sender as Object, ByVal e As System.EventArgs)
    			frm.Dispose
    		End Sub
    		
    		Private Sub mnuFileSaveCopy_Clicked(ByVal sender as Object, ByVal e As System.EventArgs)
    			'
    		End Sub
    		
    		Private Sub mnuFileBuild_Clicked(ByVal sender as Object, ByVal e As System.EventArgs)
    			'
    		End Sub
    		
    		Private Sub mnuFileExit_Clicked(ByVal sender as Object, ByVal e As System.EventArgs)
    			'
    		End Sub
    				
    		'***********************************************************************************************
    		
    		'CodePane Creation and Event Handlers
    		'***********************************************************************************************
    		Private Sub AddCodePane()
    			'
    			Me.CodePane = New System.Windows.Forms.TextBox
    			
    			CodePane.Location = New Point(1, 1)
    			CodePane.TabIndex = 2
    			CodePane.Size = New Size(Me.ClientSize.Width - 1, Me.ClientSize.Height - 1)
    			CodePane.Multiline = True
    			CodePane.AcceptsReturn = True
    			CodePane.AcceptsTab = True
    			CodePane.WordWrap = False
    			CodePane.Scrollbars = ScrollBars.Both
    			
    			AddHandler CodePane.TextChanged, AddressOf Me.CodePane_TextChanged
    				
    			Me.Controls.Add(CodePane)
    		End Sub
    		
    		Private Sub CodePane_TextChanged(sender As Object, e As EventArgs)
    			'
    			isChanged = True
    		End Sub
    		'************************************************************************************************
    
    		'General Methods
    		'************************************************************************************************
    		Private Sub SaveFile()
    
    		End Sub
    		'*************************************************************************************************
    	End Class
    End Namespace
    to compile save as vbNote.vb and put
    Code:
    vbc.exe vbNote.vb /r:System.dll /r:System.Drawing.dll  /r:System.Windows.Forms.dll /target:winexe /out:vbNote /bugreport:vbNoteError.log
    in a batch file @ the same path then dblClick
    Last edited by ZanM; Mar 25th, 2002 at 12:35 AM.
    Magiaus
    Visual Basic 6.0 SP5
    Visual C++ 6.0 SP5


    The only sovereign you can allow to rule you is reason.

  9. #9
    Lively Member MedevH's Avatar
    Join Date
    Aug 2001
    Location
    Where I walk, I walk alone. Where I code, I code alone.
    Posts
    91

    There you go again...

    There you go again with the complex chit again
    Hey Zan I'm telling you all these folks are Newbies

    Hey WideAwake if you want to tried and true easy method:

    Code:
    Option Explicit On
    Option Strict On
    Option Compare Binary
    
    Imports System
    Imports System.ComponentModel
    Imports System.Drawing
    Imports System.Windows.Forms
    Imports Microsoft.VisualBasic
    
    Namespace TestDispose
    
    #Region
    	Public Class frmDispose
    		Inherits System.Windows.Forms.Form
    
    		Private frm As New System.Windows.Forms.Form
    		Private isLoaded As Boolean = False
    		
    		<STAThread()> Shared Sub Main()
    			System.Windows.Forms.Application.Run(New frmDispose())
    		End Sub
    
    		Public Sub New()
    			MyBase.New()
    			Call AddMenus()
    			Me.Text = "VB.Net Dispose Test"
    			Me.TopMost = True
    		End Sub
    End Region
    
    		'Menu Creation and Event Handlers
    		'***********************************************************************************************		
    		Private Sub AddMenus()
    			Dim mnuMain As New MainMenu
    			Dim mnuFile As MenuItem = mnuMain.MenuItems.Add("&File")
    
    			mnuFile.MenuItems.Add("&Exit",New EventHandler(AddressOf Me.FormDispoer))	
    	
    			Me.Menu = mnuMain
    		End Sub
    
    		Private Sub FormDispoer(sender As Object, e As System.EventArgs)
    			Me.Dispose
    		End Sub
    	
    		'***********************************************************************************************
    	End Class
    End Namespace

    This one works to

    Copy this into your batch file
    Code:
    vbc.exe frmDipose.vb /r:System.dll /r:System.Drawing.dll  /r:System.Windows.Forms.dll /target:winexe /out:"Unload Form.exe" /bugreport:err.log
    There is no good or evil only CODE! ~MedevH~

  10. #10

    Thread Starter
    Lively Member
    Join Date
    Aug 2000
    Location
    UK
    Posts
    78
    But when you try to show the form for the 2nd time you get an error saying 'Cannot access disposed form'.

  11. #11
    Lively Member MedevH's Avatar
    Join Date
    Aug 2001
    Location
    Where I walk, I walk alone. Where I code, I code alone.
    Posts
    91

    HERE!!!!

    I fixed that problem for you
    but your gonna have to weed it out yourself
    that way you'll learn from it

    by the way you said dispose didn't work
    not that Zan's program didn't have a bug in

    I recommend downloading the .NET SDK
    and if you have then USE IT!!!!


    Code:
    Option Explicit On
    Option Strict On
    Option Compare Binary
    
    Imports System
    Imports System.ComponentModel
    Imports System.Drawing
    Imports System.Drawing.Text
    Imports System.Windows.Forms
    Imports Microsoft.VisualBasic
    
    Namespace VBNotepad
    	Public Class frmPad
    		Inherits System.Windows.Forms.Form
    
    		Private frm As System.Windows.Forms.Form
    
    		Private WithEvents CodePane As System.Windows.Forms.TextBox
    
    		Private FileName as String = "New Source.vb"
    		Private FilePath as String = "C:\"
    		Private isChanged as Boolean = False
    		Private AlreadyExists As Boolean = False
    		
    		<STAThread()> Shared Sub Main()
    			System.Windows.Forms.Application.Run(New frmPad())
    		End Sub
    
    		Public Sub New()
    			'
    			MyBase.New()
    			
    			Call AddMenus()
    			Call AddCodePane()
    			
    			Me.Text = "VB.Net Simple Code Pad"
    			Me.TopMost = True
    		End Sub
    
    		Overrides Protected Sub OnResize(ByVal e As EventArgs )
    			CodePane.Size = New Size(Me.ClientSize.Width - 1, Me.ClientSize.Height - 1)
    		End Sub
    
    
    		'Menu Creation and Event Handlers
    		'***********************************************************************************************		
    		Private Sub AddMenus()
    			Dim mnuMain As New MainMenu
    			Dim mnuFile As MenuItem = mnuMain.MenuItems.Add("&File")
    			
    			'File Menu
    			mnuFile.MenuItems.Add("&New", New EventHandler(AddressOf Me.mnuFileNew_Clicked))
    			mnuFile.MenuItems.Add("&Open", New EventHandler(AddressOf Me.mnuFileOpen_Clicked))
    			mnuFile.MenuItems.Add("&Save", New EventHandler(AddressOf Me.mnuFileSave_Clicked))
    			mnuFile.MenuItems.Add("Save Copy",New EventHandler(AddressOf Me.mnuFileSaveCopy_Clicked))
    			mnuFile.MenuItems.Add("-")	'Seperator
    			mnuFile.MenuItems.Add("Build", New EventHandler(AddressOf Me.mnuFileBuild_Clicked))
    			mnuFile.MenuItems.Add("-")
    			mnuFile.MenuItems.Add("E&xit", New EventHandler(AddressOf Me.mnuFileExit_Clicked))
    			
    			Me.Menu = mnuMain
    		End Sub
    
    		Private Sub mnuFileNew_Clicked(ByVal sender as Object, ByVal e As System.EventArgs)
    			If isChanged = True Then
    				Call SaveFile()
    			End If
    			CodePane.Text = "" 
    		End Sub
    
    		Private Sub mnuFileOpen_Clicked(ByVal sender as Object, ByVal e As System.EventArgs)
    			Set frm = New System.Windows.Forms.Form
    			frm.Show
    		End Sub
    
    		Private Sub mnuFileSave_Clicked(ByVal sender as Object, ByVal e As System.EventArgs)
    			frm.Dispose
    		End Sub
    		
    		Private Sub mnuFileSaveCopy_Clicked(ByVal sender as Object, ByVal e As System.EventArgs)
    			'
    		End Sub
    		
    		Private Sub mnuFileBuild_Clicked(ByVal sender as Object, ByVal e As System.EventArgs)
    			'
    		End Sub
    		
    		Private Sub mnuFileExit_Clicked(ByVal sender as Object, ByVal e As System.EventArgs)
    			'
    		End Sub
    				
    		'***********************************************************************************************
    		
    		'CodePane Creation and Event Handlers
    		'***********************************************************************************************
    		Private Sub AddCodePane()
    			'
    			Me.CodePane = New System.Windows.Forms.TextBox
    			
    			CodePane.Location = New Point(1, 1)
    			CodePane.TabIndex = 2
    			CodePane.Size = New Size(Me.ClientSize.Width - 1, Me.ClientSize.Height - 1)
    			CodePane.Multiline = True
    			CodePane.AcceptsReturn = True
    			CodePane.AcceptsTab = True
    			CodePane.WordWrap = False
    			CodePane.Scrollbars = ScrollBars.Both
    			
    			AddHandler CodePane.TextChanged, AddressOf Me.CodePane_TextChanged
    				
    			Me.Controls.Add(CodePane)
    		End Sub
    		
    		Private Sub CodePane_TextChanged(sender As Object, e As EventArgs)
    			'
    			isChanged = True
    		End Sub
    		'************************************************************************************************
    
    		'General Methods
    		'************************************************************************************************
    		Private Sub SaveFile()
    
    		End Sub
    		'*************************************************************************************************
    	End Class
    End Namespace
    There is no good or evil only CODE! ~MedevH~

  12. #12
    Addicted Member ZanM's Avatar
    Join Date
    Oct 1999
    Location
    The here and now.
    Posts
    191

    umm....

    would you try to use a form in vb6 after you unloaded it? i mean you caould load a new one and use it but isn't that what you have to do in .net too..? MS is just hiding less of the C backbone from us now personally i like it the power of VB has trippled or more 0_0
    Magiaus
    Visual Basic 6.0 SP5
    Visual C++ 6.0 SP5


    The only sovereign you can allow to rule you is reason.

  13. #13
    Lively Member MedevH's Avatar
    Join Date
    Aug 2001
    Location
    Where I walk, I walk alone. Where I code, I code alone.
    Posts
    91
    You bet your arse Zan
    The whole .NET framework has finally revealed Microsoft has an intelligent person working on thier staff.
    There is no good or evil only CODE! ~MedevH~

  14. #14

    Thread Starter
    Lively Member
    Join Date
    Aug 2000
    Location
    UK
    Posts
    78
    Why is everyone making controls at runtime ?

  15. #15
    Addicted Member ZanM's Avatar
    Join Date
    Oct 1999
    Location
    The here and now.
    Posts
    191

    well

    i happen to be useing notepad and the framework to code in no IDE just me my brain and the compiler
    Magiaus
    Visual Basic 6.0 SP5
    Visual C++ 6.0 SP5


    The only sovereign you can allow to rule you is reason.

  16. #16
    Lively Member MedevH's Avatar
    Join Date
    Aug 2001
    Location
    Where I walk, I walk alone. Where I code, I code alone.
    Posts
    91

    Uhhh

    I am?
    There is no good or evil only CODE! ~MedevH~

  17. #17
    Frenzied Member Shawn N's Avatar
    Join Date
    Dec 2001
    Location
    Houston
    Posts
    1,631
    Originally posted by WideAwake
    Why is everyone making controls at runtime ?
    Better question is why there's so much unrelated coding to such a [seemingly] simple question.
    Please rate my post.

  18. #18
    Lively Member MedevH's Avatar
    Join Date
    Aug 2001
    Location
    Where I walk, I walk alone. Where I code, I code alone.
    Posts
    91
    Only way newbies learn anything is by DOING IT!!!
    all the extra code is to help him learn


    3 ways of doing thing

    right way

    wrong way

    easy way

    I always prefer #1
    There is no good or evil only CODE! ~MedevH~

  19. #19
    Addicted Member ZanM's Avatar
    Join Date
    Oct 1999
    Location
    The here and now.
    Posts
    191

    Arrow well because

    I didn't feel like making a new app in notepad when i had the one i was working on and tested the dispose in already coded. I mean I did tell WideAwake axactly where to look in the cade and incase you can't tell MedevH used my code to make a simpler example. I got to say this thing is pretty sad it's been viewed over 100 times and any newbie should know how to do it. it's simple.
    Code:
    Dim frm as System.Windows.Forms.Form
    
    Sub LoadForm()
           Set frm = New System.Windows.Forms.Form
           frm.Text = "New Form"
           frm.Show
    End Sub
    
    Sub DisposeForm()
           frm.Dispose()
    End Sub
    now let's just suppose you have a file called frmMp3Played.vb with the class frmMP3 that is compiled into you exe just change System.Windows.Forms.Form to be frmMP3 now you have a form that can be loaded and disposed of more than one time now if you want to keep the forms state just hide the form instead of disposeing of it.

    is that better for you guys shessh i think maybe you need to try to make an app in notepad WideAwake then go on to vs.net because my code looks just like the designer makes if you look close or open the .vb file in notepad i just don't have vs.net
    Magiaus
    Visual Basic 6.0 SP5
    Visual C++ 6.0 SP5


    The only sovereign you can allow to rule you is reason.

  20. #20

    Thread Starter
    Lively Member
    Join Date
    Aug 2000
    Location
    UK
    Posts
    78
    'MedevH' - "Only way newbies learn anything is by DOING IT!!!"
    What a stupid statement, I'm new to VB.NET not VB in general. You seem more interested in showing people how to right vb.net code via notepad using the .NET SDK, which has nothing to do with the initial question. The amount of people I see ask questions to have a completely different question answered is quite remarkable.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width