I am making a program in which you can press a button and it makes a new form but I have a little bit of a problem.
Because I have Form1 and Form2. When I press the button on Form1 it shows Form2 and it should just close Form1.
Private Sub PictureBox1_Click(sender As Object, e As EventArgs) Handles PictureBox1.Click
Form2.Show()
Me.Close()
What actually happens is that it closes Form1 and Form2 even if I said Me.Close().
Is there a fix to it or did I just do it wrong somehow?
I bet Form1 is your startup form. If not sure sure check that in Project properties.
The moment you close Form1, your application terminates altogether. So in simple words you can't close Form1 and show another form, at least not with 2 lines of code.
What you can do is hide Form1 and Show Form2.
Form2.Show()
Me.Hide()
Now when you close Form2, make sure you either unhide Form1 (so that usercan manually close it) or automatically close Form1 from Form2's FormClosing event, else your process will be alive in the background, a ghost :)
So in your Form2, add the FormClosing event handler and then inside that close Form1
Private Sub Form2_FormClosing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing
Form1.Close()
End Sub
Related
The user enters data in Form1 for example his name and phone number, he clicks on the "next" button that opens Form2 and Hide Form1, if he clicks on the "back" button I want the program to show Form1 with the data he entered before
The code for the "Next" Button in Form1:
Private Sub NextButton_Click(sender As Object, e As EventArgs) Handles NextButton.Click
Dim MyForm As New Form2
MyForm.Show()
Me.Hide()
End Sub
What should I do in order to keep the data the user entered if he comes back to Form1?
The code for the "Previous" button in Form2:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Form1.Show()
Me.Close()
End Sub
One approach you could use is to only the use the DEFAULT INSTANCES of your Forms. These are accessed by using only the name of the Form, WITHOUT ever using the "New" keyword.
You're doing exactly that in Form2 when you show Form1 with:
Form1.Show()
This works because Form1 is your startup object and VB.Net used the default instance to start the application.
You could do the same thing in Form1, to show Form2:
' ... code in Form1 ...
Private Sub NextButton_Click(sender As Object, e As EventArgs) Handles NextButton.Click
Form2.Show() ' show the default instance of Form2 (do NOT use the "new" keyword)
Me.Hide()
End Sub
Just make sure in all places you Hide() the current form instead of closing it.
You can access the properties/fields of the default instances using the same syntax to show them, using only their name. For example, here is a made up value being accessed on Form2:
Dim userName As String = Form2.txtAddress1.Text
This assumes that Form2 was previously displayed and populated by the user. You could access the Forms from anywhere in the code using this type of syntax.
If you want to implement your own "default instance" mechanism, you could use Shared members in a class:
Public Class WizardForms
Public Shared F1 As New Form1
Public Shared F2 As New Form2
Public Shared F3 As New Form3
End Class
Then you could use code like this to display one:
WizardForms.F2.Show()
This would work as long as you are only HIDING the forms. If you allow the user to close the Forms (or close them via code), then you'd need extra code to make sure they get recreated as needed.
I have two forms in my WindowsFormApplication named as Form1 and Form2. The idea is when the Program is being closed, it shows up a Dialog Box to confirm it.
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Form2.Show()
Me.Close()
End Sub
Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
MessageBox.Show("You are About to cancel the Setup.","Cancel Setup?",
MessageBoxButtons.OK,
MessageBoxIcon.Exclamation,
MessageBoxDefaultButton.Button1)
End Sub
End Class
Until here, my code worked fine but the problem is when I click Button1, the Message Box appears to confirm the closure of Form1.
I don't want this to happen so then I tried changing Me.Close() to Me.Hide. I was successful for preventing the message box to appear but then I got another Problem. As the Form hides, it stays active in the background and I also don't want this to happen.
Another thing I added in Form1_FormClosing is Me. Close and Form2.Close. This enables to close both the forms once the program's active Form is being closed. But Again, there's a problem. As soon as I click the close button, the Message Boxes fill up the screen and not listening to my Command. Anyone got a solution for this?
So..
If the user clicks close in Form1, then the program should terminate with no messagebox.
If the user clocks close in Form2, Form3 or Form4, the program would Terminate when the user clicks Yes in the MessageBox or else nothing would get affected (Especially the Data).
The way this works is when user clicks Close in Form1, the program terminates with no MessageBox and if the user clicks Close in other Forms, A MessageBox would appear asking whether you are sure to close the Program. If the user clicks Yes, the program will Terminate because the DialogResult is Yes and Form1.Closing Cancel goes to False and Closes Form1(As Closure type was set as First Form Closes in Program Properties, this will terminate the program) Else the Form1.Closing Cancel goes to True which will prevent the current Form from closing and losing any Data.
This code goes to Form1:
Imports System.ComponentModel
Public Class Form1
Friend closeProgramAlreadyRequested As Boolean = False
Private Sub Form1_Closing(sender As Object, e As CancelEventArgs) Handles Me.Closing
closeProgramAlreadyRequested = False
End Sub
End Class
This code goes to Form2 & applies to other Forms as well but except for Form1:
Imports System.ComponentModel
Public Class Form2
Private Sub Form2_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
If Form1.closeProgramAlreadyRequested = False Then
Dim result As DialogResult = MessageBox.Show("You are About to cancel the Setup.", "Cancel Setup?", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1)
If result = DialogResult.No Then
e.Cancel = True
Else
e.Cancel = False
Form1.Close()
End If
End If
End Sub
End Class
how do i close my form after opening the next form (VB.net Windows form) like the Next button
I tried Form2.show() it shows form2 but does not close the form1 and if i type me.close(), the entire project stops
If you just want Form2 to be Visible you can hide Form1 when you show Form2, then show it Form1 again when you close Form2. What is happening is that once you close Form1 your program will exit see below edit.
Something like this.
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Dim frm2 As New Form2
AddHandler frm2.FormClosed, AddressOf Form2Closing
frm2.Show()
Me.Hide()
End Sub
Private Sub Form2Closing(sender As Object, e As FormClosedEventArgs)
Me.Show()
RemoveHandler DirectCast(sender, Form2).FormClosed, AddressOf Form2Closing
End Sub
If you just are wanting to Close Form1 and not go back to it once Form2 is open, you can change your project settings from When startup form closes(which is the default) to When last form closes then you can close your first form without closing your application.
You have to specify what to close:
Form1.Close()
You better not close your form after opening another one from it, unless you want that other one also closed. Otherwise it will cause ownership and visibility side effects, which you really don't want to deal with in the long run.
This is exactly why me.close() on your main form stops your project. You just have consider paradigms Microsoft put into a Winforms application. If you don't, you are guaranteed to get in trouble.
Instead, a wizard control is what you are probably looking for.
Consider i am having two forms, form1 and form2
How can i click,mouse over(any events) a specific button on another form using coding in vb.net?
I'm assuming that Form1 launches Form2, since there's not a whole lot of information in the description.
When Form1 launches, there are two buttons: "button1" and "Launch Form 2" (forgot to change text on button1, sorry. :(
When you click "Launch Form 2", Form2 pops up:
Clicking the "button1" on Form1, a message box originating from Form1 pops up saying:
Clicking the "button1" on Form2, a message box ALSO originating from Form1 pops up saying:
Here's the code:
Form1
Public Class Form1
Private WithEvents frm2 As New Form2
Private Sub Form1Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Form1Button.Click
RunSomeCode("Called from form 1!")
End Sub
Public Sub RunSomeCode(ByVal message As String)
MessageBox.Show(message)
End Sub
Private Sub Form1LaunchForm2Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Form1LaunchForm2Button.Click
frm2.Activate()
frm2.Show()
End Sub
Private Sub frm2_SimulateForm1ButtonClick() Handles frm2.SimulateForm1ButtonClick
RunSomeCode("Called from form 2!")
End Sub
End Class
Form2
Public Class Form2
Public Event SimulateForm1ButtonClick()
Private Sub Form2Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Form2Button.Click
RaiseEvent SimulateForm1ButtonClick()
End Sub
End Class
How it works
Form 2 has a public event called "SimulateForm1ButtonClick". That event can be raised whenever you want, from any code block. I just decided to raise it when I click the button on the form.
Form 1 has an instance of Form2 WithEvents. It's very important that you use the WithEvents keyword, or that public event in Form2 won't show up. :(
Form 1 has a sub that handles the "SimulateForm1ButtonClick" that is raised when Form2 clicks its button.
Now, here's another important detail: The code executed when button1 is clicked on Form1 is actually in a private sub called RunSomeCode(). This is important, because it makes the code accessible from any other part of Form1, namely the part that handles Form2's event.
I hope that helps you out a little bit. I'm not sure exactly what you were asking. :/
Code: http://darin.hoover.fm/code/dl/FormsSandbox.zip
If you are trying to fire the event, just use Form2.Button1.PerformClick() assuming that the button on form 2 is called 'button1'.
I have 2 forms. Form1 and Form2.
When Form2 is closing, how do I make Form2 to inform Form1 that Form2 is closed.
Do I need to use Delegate, if yes, how?
thanks.
You will need to first reference Form2 from Form1, then you can use one of the close events on the other form. Either the Form.OnClosed or Form.OnClosing events.
' On form1
Private Sub Form2_Closing(ByVal sender As Object, _
ByVal e As EventArgs) Handles Form2.OnClosing
' Form2 informed us that it is closing. Do stuff!
End Sub
You can just create a custom event in Form2 and then make Form1 subscribe to it. Here's a nice and easy tutorial article for this:
http://www.codeproject.com/KB/vb/StepByStepEventsInVBNET.aspx
Edit: If you just need the event when it's closing, I'd go with Oded's solution since that's easier.