VB.net opening and closing forms - vb.net

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.

Related

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

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()

Problems loading more than 1 form - VB.net

I searched over the Stackoverflow and Google about solution for my problem but no way! so it's the time to ask my own question.
I'm making a big project using C# and VB.net (not our topic -_-)
I opened this question to ask about VB.net problem
I'm trying to load a lot of forms but in the same time, hide the previous form!
Firstly, look here on Form1:
Private Sub Timer4_Tick(sender As Object, e As EventArgs) Handles Timer4.Tick
If Label1.Text = "Setting profile.dat=Roblox.AuthAccess ..." Then
Form2.Show()
Me.Hide()
End If
End Sub
This is a timer, which detect if Label1's Text is "Setting profile.dat=Roblox.AuthAccess ..." it must show Form2 and then close Form1 (THIS IS WORKING PERFECTLY) :-)
Form2:
Private Sub WebBrowser1_DocumentCompleted(sender As Object, e As WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
If WebBrowser1.Url.ToString().Contains("home") Then
Me.Hide()
Form3.Show()
End If
End Sub
This will detect if "WebBrowser1"'s URL does contain "home" then it will hide Form2 and show Form3 (AND HERE THE PROBLEM STARTED!!!)
Problem is: It shows Form3 then after 2 seconds, Form2 come back again (now both are opened once) and they're opening and closing forever (I close them by going to Task Manager and ending "vshost32.exe")
Form3:
Public Class Form3
Private Sub Form3_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Form2.Close()
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
Form4.Show()
End Sub
End Class
Actually, stopping timers is still not a stable solution and disabling them are more better.
Just replace Timer4.Stop() with Timer4.Enabled = False .
Thank you very much #jmcilhinney :)
I never thought about stopping every "Timer" after it's job had finished!
Just replaced Timer4_Tick with:
Private Sub Timer4_Tick(sender As Object, e As EventArgs) Handles Timer4.Tick
If Label1.Text = "Setting profile.dat=Roblox.AuthAccess ..." Then
Me.Hide()
Form2.Show()
Timer4.Stop()
End If
End Sub
So at last, I fixed that by adding Timer4.Stop() .

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 :)

How to make a timer trigger when you click a button?

I am working on a Windows Form Application, making a button to show another form and I am trying to make the user wait some time after he clicks the button and after the time passes then the form will open.
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Timer1.Start()
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
Timer1.Stop()
Form3.Show()
Me.Hide()
End Sub
When I use the code above, Form3 is shown before I even click the button.
You need to make sure that you set the Enabled property on the Timer1 component to False in the form designer.

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();