Results 1 to 13 of 13

Thread: [RESOLVED] [2005] Closing previous form

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2006
    Posts
    274

    Resolved [RESOLVED] [2005] Closing previous form

    I need to close a previous form, and open a new form2. This code helps in opening new form but fails to close the old form. Besides, is there a way to use function in form1 at form2? for example, i need to display what hav been selected at the combobox(form1) at form 2.

    VB Code:
    1. Private Sub Button9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button9.Click
    2.      
    3.  
    4.         Form2.ShowDialog()
    5.         Me.Close()
    6. End Sub

  2. #2
    Just Married shakti5385's Avatar
    Join Date
    Mar 2006
    Location
    Udaipur,Rajasthan(INDIA)
    Posts
    3,747

    Re: [2005] Closing previous form


    check this code


    VB Code:
    1. Private Declare Function FindWindow Lib "user32.dll" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Integer
    2. Private Declare Function PostMessage Lib "user32.dll" Alias "PostMessageA" (ByVal hwnd As Integer, ByVal wMsg As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As Integer
    3.  
    4.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    5.         Dim objType As Type() = Reflection.Assembly.GetExecutingAssembly.GetTypes()
    6.         Dim x As Integer, i As Integer
    7.         Try
    8.             For x = LBound(objType) To UBound(objType)
    9.                 If Not objType(x).Name = Me.Name Then '/// make sure you dont unload this form yet.
    10.                     i = FindWindow(vbNullString, objType(x).Name)
    11.                     If Not i = 0 Then
    12.                         PostMessage(i, CInt(&H10), vbNullString, vbNullString)
    13.                     End If
    14.                 End If
    15.             Next
    16.         Catch ex As Exception
    17.             MessageBox.Show("Oops, the following error occured:" & ex.Message)
    18.         Finally
    19.             MyBase.Close() '/// now that all the other forms are closed , unload this one.
    20.         End Try
    21.     End Sub

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2006
    Posts
    274

    Re: [2005] Closing previous form

    The name is where i put the name of current form, or form to be opened?

  4. #4
    PowerPoster sparrow1's Avatar
    Join Date
    May 2005
    Location
    Globetrotter
    Posts
    2,820

    Re: [2005] Closing previous form

    Quote Originally Posted by wence
    I need to close a previous form, and open a new form2. This code helps in opening new form but fails to close the old form. Besides, is there a way to use function in form1 at form2? for example, i need to display what hav been selected at the combobox(form1) at form 2.

    VB Code:
    1. Private Sub Button9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button9.Click
    2.      
    3.  
    4.         Form2.ShowDialog()
    5.         Me.Close()
    6. End Sub
    Hi,

    You can try this:

    VB Code:
    1. Private Sub Button9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button9.Click
    2.      
    3.  
    4.         Form2.ShowDialog()
    5.         Form1.Hide()
    6. End Sub

    However If you close form1 (startup form) then it will close the hole application, but when you want to close another form, for example Form 3 then you can use Form3.Close.

    Hope it helps,

    sparrow1
    Wkr,
    sparrow1

    If I helped you, don't forget to Rate my post. Thank you

    I'm using Visual Studio.Net 2003 and
    2005
    How to learn VB.Net Create setup with VB 2005 Drawing for beginners VB.Net Tutorials GDI+ Tutorials
    Video's for beginners

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2006
    Posts
    274

    Re: [2005] Closing previous form

    Sparrow1, i type form1.hide, it got arrow, when i change it to me.hide, form 1 doesnt seem disappear. Whats the reason?

  6. #6
    PowerPoster sparrow1's Avatar
    Join Date
    May 2005
    Location
    Globetrotter
    Posts
    2,820

    Re: [2005] Closing previous form

    Quote Originally Posted by wence
    Sparrow1, i type form1.hide, it got arrow, when i change it to me.hide, form 1 doesnt seem disappear. Whats the reason?
    Hi,

    I suppose that button9 is a control in Form1.
    Then Try this:

    VB Code:
    1. Private Sub Button9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button9.Click
    2.      
    3.  
    4.         Form2.Show()
    5.         Me.Hide()
    6. End Sub

    Wkr,

    sparrow1
    Wkr,
    sparrow1

    If I helped you, don't forget to Rate my post. Thank you

    I'm using Visual Studio.Net 2003 and
    2005
    How to learn VB.Net Create setup with VB 2005 Drawing for beginners VB.Net Tutorials GDI+ Tutorials
    Video's for beginners

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2006
    Posts
    274

    Re: [2005] Closing previous form

    It still doesn't work. Form 1 still stays there when i click the button

  8. #8
    Hyperactive Member
    Join Date
    Mar 2004
    Location
    Prato - Tuscany - Italy
    Posts
    461

    Re: [2005] Closing previous form

    You can try this:


    visual basic code:--------------------------------------------------------------------------------Private Sub Button9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button9.Click


    Form2.ShowDialog()
    Form1.Hide()
    End Sub--------------------------------------------------------------------------------
    I suggest to use the second sparrow's suggestion, or simply change the order of the lines:

    You can try this:


    visual basic code:--------------------------------------------------------------------------------Private Sub Button9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button9.Click

    Form1.Hide()
    Form2.ShowDialog()

    End Sub--------------------------------------------------------------------------------

    Anyway you can act on Form1 also from within Form2's code. You only need to have a reference to Form1.
    Live long and prosper (Mr. Spock)

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2006
    Posts
    274

    Re: [2005] Closing previous form

    Oh ya, just change the sequence, me.hide() comes first before form2.showdialog() and it works.
    By the way, can i display the textbox's text in form1 at form2?

  10. #10
    Frenzied Member HanneSThEGreaT's Avatar
    Join Date
    Nov 2003
    Location
    Vereeniging, South Africa
    Posts
    1,492

    Re: [2005] Closing previous form

    Yes
    Just use dot notation..

    VB Code:
    1. Button9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button9.Click
    2.  
    3. Form1.Hide()
    4. Form2.TextBox1.Text = Text1.Text 'this will insert text1's value (from form1) into form2
    5. Form2.ShowDialog()
    6.  
    7. End Sub
    VB.NET MVP 2008 - Present

  11. #11

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2006
    Posts
    274

    Re: [RESOLVED] [2005] Closing previous form

    Thanks Hannesthegreat, fantastic, it works.

  12. #12

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2006
    Posts
    274

    Re: [RESOLVED] [2005] Closing previous form

    The problem now is that it can only close form1 and enter form2 once. Once i do the same thing all over again, means i go back to form1 and try to close it in order to enter form2 once again, an error occurs saying that ""Form that is already displayed modally cannot be displayed as a modal dialog box. Close the form before calling showDialog". How should this be fixed?

    When i tried to close application for form1 then only form2.showdialog but it just closes everything .

  13. #13
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: [RESOLVED] [2005] Closing previous form

    Suppose Form1 is your startup Form then
    VB Code:
    1. 'Create a new instance of Form2
    2.         Dim frm2 As New Form2
    3.         'Hide the main form
    4.         Me.Visible = False
    5.         'Show the new instance of Form2 as a dialog. At this point
    6.         'the only active form on your screen should be frm2... And it'll stay there
    7.         'until the user close it (i.e. via a button click)
    8.         frm2.ShowDialog()
    9.         'When the user close frm2, display the main form again
    10.         Me.Visible = True

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