|
-
Oct 29th, 2006, 12:58 AM
#1
Thread Starter
Hyperactive Member
[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:
Private Sub Button9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button9.Click
Form2.ShowDialog()
Me.Close()
End Sub
-
Oct 29th, 2006, 01:59 AM
#2
Re: [2005] Closing previous form
check this code
VB Code:
Private Declare Function FindWindow Lib "user32.dll" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Integer
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
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim objType As Type() = Reflection.Assembly.GetExecutingAssembly.GetTypes()
Dim x As Integer, i As Integer
Try
For x = LBound(objType) To UBound(objType)
If Not objType(x).Name = Me.Name Then '/// make sure you dont unload this form yet.
i = FindWindow(vbNullString, objType(x).Name)
If Not i = 0 Then
PostMessage(i, CInt(&H10), vbNullString, vbNullString)
End If
End If
Next
Catch ex As Exception
MessageBox.Show("Oops, the following error occured:" & ex.Message)
Finally
MyBase.Close() '/// now that all the other forms are closed , unload this one.
End Try
End Sub
-
Oct 29th, 2006, 02:33 AM
#3
Thread Starter
Hyperactive Member
Re: [2005] Closing previous form
The name is where i put the name of current form, or form to be opened?
-
Oct 29th, 2006, 03:38 AM
#4
Re: [2005] Closing previous form
 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:
Private Sub Button9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button9.Click
Form2.ShowDialog()
Me.Close()
End Sub
Hi,
You can try this:
VB Code:
Private Sub Button9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button9.Click
Form2.ShowDialog()
Form1.Hide()
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
-
Oct 29th, 2006, 03:48 AM
#5
Thread Starter
Hyperactive Member
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?
-
Oct 29th, 2006, 03:59 AM
#6
Re: [2005] Closing previous form
 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:
Private Sub Button9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button9.Click
Form2.Show()
Me.Hide()
End Sub
Wkr,
sparrow1
-
Oct 29th, 2006, 12:39 PM
#7
Thread Starter
Hyperactive Member
Re: [2005] Closing previous form
It still doesn't work. Form 1 still stays there when i click the button
-
Oct 29th, 2006, 12:45 PM
#8
Hyperactive Member
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)
-
Oct 29th, 2006, 09:54 PM
#9
Thread Starter
Hyperactive Member
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?
-
Oct 30th, 2006, 12:58 AM
#10
Re: [2005] Closing previous form
Yes 
Just use dot notation..
VB Code:
Button9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button9.Click
Form1.Hide()
Form2.TextBox1.Text = Text1.Text 'this will insert text1's value (from form1) into form2
Form2.ShowDialog()
End Sub
VB.NET MVP 2008 - Present
-
Oct 30th, 2006, 01:08 AM
#11
Thread Starter
Hyperactive Member
Re: [RESOLVED] [2005] Closing previous form
Thanks Hannesthegreat, fantastic, it works.
-
Oct 30th, 2006, 11:24 AM
#12
Thread Starter
Hyperactive Member
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 .
-
Oct 30th, 2006, 11:57 AM
#13
Re: [RESOLVED] [2005] Closing previous form
Suppose Form1 is your startup Form then
VB Code:
'Create a new instance of Form2
Dim frm2 As New Form2
'Hide the main form
Me.Visible = False
'Show the new instance of Form2 as a dialog. At this point
'the only active form on your screen should be frm2... And it'll stay there
'until the user close it (i.e. via a button click)
frm2.ShowDialog()
'When the user close frm2, display the main form again
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|