I can't close userform- me.close, not working - vb.net

Please, I am having issues and needs help with the following code:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Me.Close() 'Form2
Form3.showDialog()
End Sub
Report:
I have Form2 and Form3. With the above code, I intend to close Form2 and open Form3 (on button click). When I implemented the code, Form3 popped-up as expected but surprisingly few seconds after, the closed Form2 also popped-up. I believe "Me.Close" serves primarily to kill Form2 permanently. However, when I go to Project menu and set the Startup Form to start from Form2, everything works fine. But this would ignore Form1 which is the Welcome Page.
I have attempted using Me.Visible = False, dispose, and Me.Hide in place of “Me.Close” but all to no avail. I have also tried re-writing the code such that Form3.ShowDialog is written before Me.Close. I have also attempted using Form3.Show in place of "Form3.ShowDialog". Meanwhile, I also have set my Application Tab under Project properties to "When last form closes".‎ Still, the closed Form still re-occurring.
The following is the code in Form1, could this be the cause?
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Timer1.Interval = 10000
Timer1.Start()
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
Form2.Show()
Me.Hide()
End Sub
End Class
I am using Visual Studio 2015.

I think, the problem is the method ShowDialog. This will open the new Window in modal mode, which means only the new form is accessible, but when you close Form3 the focus will get back to Form2. Therefore is has to be there. Maybe you could just try:
Form3.Show()

Related

How to Open Form 2 from Form 1 and close Form 1 in vb.net

When I open Order Form from Dashboard Form, Order Form appears, Dashboard Form is hidden. But when I want to close it. Dashboard Form is still running. I need to manually stop it in vb.net. Anyone help!!
Public Class frmDashboard
Private Sub btnDashboard_Click(sender As Object, e As EventArgs) Handles btnDashboard.Click
Me.Show()
End Sub
Private Sub btnOrders_Click(sender As Object, e As EventArgs) Handles btnOrders.Click
frmOrders.Show()
Me.Close()
End Sub
Well, you shouldn't do that... but if it's really what you want...
On Form2, add something like that :
public Parent as Windows.Forms.Form
On Form1, when you open Form2, set Parent = Form1
Private Sub btnOrders_Click(sender As Object, e As EventArgs) Handles btnOrders.Click
Form2.Parent = Me
Form2.Show()
End Sub
So, on Form2, you can close Form1 when Form2 is closing.
Private Sub Form2_FormClosing(ByVal sender As Object, _
ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
Parent.Close()
End Sub
But, as I wrote, I think you shouldn't do that... it's not a common behaviour.
Instead of closing Form1, I suggest you to refresh it / reload / make it visible again (by using the Parent variable)

How to close an open form in Visual Basic using a button?

I am working on a game, and it has a simple menu system. One of the buttons, simply known as 'Button1', should close the menu form and load up the game window. While it opens the game window, it does not close the menu window. Is there a way to make it so that it closes one form and opens another.
Game window name = GameBoard
Menu window name = Form2
Code:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim Form2 As New GameBoard
Form2.ShowDialog()
Me.Close()
End Sub
The reason that the form is not closed is because Form2 is opened with ShowDialog():
Form2.ShowDialog()
' This will not execute until Form2 is closed
Me.Close()
This will pause execution of the code in Button1_Click until after Form2 is closed, as per the docs for ShowDialog():
Shows the form as a modal dialog box.
You could, alternatively, just do Form2.Show(), which would not open it as a dialog and allow the remainder of your code to run:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim Form2 As New GameBoard
Form2.Show()
Me.Close()
End Sub
Alternatively, close the current form before opening Form2:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim Form2 As New GameBoard
Me.Close()
Form2.ShowDialog()
End Sub

VB.NET End application in Form2

Here's my scenario:
I have two forms named Form1 and Form2.
Form1 has a button.
When the button is pressed, Form1 will become hidden and Form2 will be shown.
If I close Form2 by pressing [x] button on the top right of the form, the application is still running.
According what I get in my research, it seems I have to work with FormClosingEventArgs.
Anyone have any idea?
Find Shutdown Mode in your application properties. There you will see two options.
1. When start up form closes.
2. When last form closes.
If you choose no.1 then until you close your start up form your application will not close but you can apply force close. eg.
Private Sub Form2Closing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing
End
End Sub
And if no.2 then your application will close automatically when last active form closes.
Hope this is ok
Seems you are not closing your first form. You are just hiding the first form instead of closing it. You should also close the first form. But, if you are creating an object of Form2 in Form1 then you can't close the first form. if you close it then the Form2 will not be displayed because the object of Form2 will be disposed initially. So, you should handle the FormClosed event of Form2 in Form1 when the Form2 is being displayed.
'Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Me.Hide()
Dim form As New Form2
AddHandler form.FormClosed, AddressOf Me.form2_FormClosed
form.Show()
End Sub
Private Sub form2_FormClosed(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosedEventArgs)
Me.Close()
End Sub
Problem solved.
Private Sub Form2Closing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing
Form1.Close()
End Sub
Please give more suggestion to me if there is another nice way :)

Code linking a form back to previous form in Windows Forms

Having a problem linking my form3 back to form2 in windows forms. I want the "back" button to take me back to form 2 but it doesnt do so. I am trying form2.show() but it doesnt work.
My current form3 code:
Public Class Form3
Private Sub CheckedListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles CheckedListBox1.SelectedIndexChanged
MessageBox.Show("Developer Succsessfully Added to Sprint", "Developer Added")
End Sub
Private Sub Form3_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
**Form2.Show()**
End Sub
End Class
What happening is most likely you already closed your form and can't open it again.
Assuming, you have both instances well and alive
In Form2 you should have
me.Hide()
Form3.Show()
And In Form3 you should have
me.Hide()
Form2.Show()
It can be something like this
shared sub Main
dim f2 as new Form2()
dim f3 as new Form3()
f2.Next = f3
f3.previous = f2
end sub
To link forms you creating properties, Next and Previous
And then use that as way to operate the form that should open
In form code do
private sub BtnNext_Click(....).....
Me.Hide()
Me.Next.Show()
End Sub
and the same way for the previous. If you have wizard, you could chain all your forms this way.
and of course, to accomplish this, minimum, you need an interface that contracts your forms to implement Properties Next and Previous or you can have a base class with implementation of the buttons and properties and then it will all work.
simple code redirect one form1 to form2 Using C#
Form2 f2=new Form2();
f2.show() OR f2.ShowDialog();

VB.net opening and closing forms

I have a VB program that has two forms, i have coded the form load of each forms.
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
MessageBox.Show("I AM FORM 1")
End Sub
Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
MessageBox.Show("I AM FORM 2")
End Sub
Here is how i switch through Form1 and Form2, i made use of a button.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Me.Hide()
Form1.Show()
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Me.Hide()
Form2.Show()
End Sub
But everytime i switch forms the form load event will only trigger once. Is there something wrong with my code? I am guesing the Me.Hide() will only hide the previous form and not totally close it. I want to be able to close the previous form so that when i will open it again, the form load event will trigger again.
But everytime i switch forms the form load event will only trigger once. Is there something wrong with my code? I am guesing the Me.Hide() will only hide the previous form and not totally close it.
This is exactly what is happening. The Hide method just hides the form from the user, effectively making it invisible.
What you're looking for is the Close method, which actually closes the form. (Since you are displaying the form using the Show method, you do not need to call Dispose.)
You will, however, not be able to close a form and continue to run code in its methods. So you'll need to reverse the order of the statements in your event handler functions, displaying the other form first and then closing itself. Make them look like this:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Form1.Show()
Me.Close()
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Form2.Show()
Me.Close()
End Sub
That will do what you want. The Load event will be triggered each time you call the Show method, because you're creating and showing a new form.
It is worth pointing out, though, that you're relying on an unusual characteristic of VB.NET, one that it retains from the older VB languages for backwards compatibility reasons. Instead of referring to an object of your form class (like you would have to do with all other class objects), you are referring to it by the type name (the name of the class itself). You really shouldn't do that, it causes all sorts of headaches and will confuse people reading your code. It is better to just instantiate a new form object, like this:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim frm As New Form1 ' create a new Form1 object
frm.Show() ' ... and display it
Me.Close()
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim frm As New Form1 ' create a new Form2 object
frm.Show() ' ... and display it
Me.Close()
End Sub
When you run this code, you will likely run immediately into another problem: the first time you close Form1, your entire application will quit. This is because, by default for a new project, Form1 is designated as the "Startup form" in your project's properties ("My Project" in the Solution Explorer). You will either have to:
create a third form to use as the "main" form, and set the "Startup form" to this third form, or
change the "Shutdown mode" (also in "My Project") from "When startup form closes" to "When last form closes".
I am guesing the Me.Hide() will only hide the previous form and not totally close it
Yes, it does what it says. If you want to close the form then use Me.Close() instead. The Load event will fire again when you create the new instance.
You'll have to change a setting to ensure that doesn't also close your application. Project + Properties, Application tab, change the Shutdown mode setting to "When last form closes". And put the Me.Close() call after the Show() call.
I also had a similar question. When u .Hide() you are just storing it away in memory somewhere such that when it is re-opened it doesnt have to make a new form just recalls the one from memory hence that method is not called again. You have to destroy the form. So what you can do when navigating to another form is go to that form first and then destroy the current form like so Form2.Show()Me.Close(). Look at my question and my accepted answer. If that works please dont forget to tick this as your accepted answer.
When my form is hidden and reloaded from another form it is not executing the code in the Load event
If MessageBox.Show("Are you sure to close this application?", "Close",
MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
frmIndex.Show() //the main form
Else
e.Cancel = True
Me.Show() // The form open
End If
The form open going closing and going back to the main/index form. hope it help :) just play with the .show, .hide and e.cancel
I think you using a silly construction, but you should;
Private Sub Form2_Shown(sender As Object, e As EventArgs) Handles Me.Shown
Form1.close()
End Sub
Use the Shown event.
And use ShowDialog()
Form1.ShowDialog()
Yes. What you are doing is closing the form before it could open up form2.
Instead Of:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Me.Hide()
Form1.Show()
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Me.Hide()
Form2.Show()
End Sub
You Need To Put:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Form1.show
Me.hide
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Form2.show
Me.hide
End Sub
If This Helps, Please Reply.