VB.NET End application in Form2 - vb.net

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

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)

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

Two items maintaining focus

I am needing to find away to have two different items to maintain focus to fire off the textbox.KeyPress event. I have a textbox that when gains focus it pops up a form for a numeric keypad that fires off sendkey commands that I need to get read by the textbox.KeyPress event to fill the textbox
This is on a popup form that has buttons to click
Private Sub Number1_Click(sender As System.Object, e As System.EventArgs) Handles Number1.Click
SendKeys.Send("1")
End Sub
This is on a different form that I need the sendkeys.send to populate
Private Sub TextBox_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox.KeyPress
'Code goes here
End Sub
Before calling SendKeys, set the focus on the textbox that will get the key. You will need an instance variable of the form that the textbox is on. This variable must be in scope of the form that you are sending the key from. If the other form is launched from the same form that the SendKeys statement is executed, then a class member-level variable can be used. Otherwise, use a global variable declared in a module.
'Declaration
Public frm2 as Form2
'Show Form2
frm2 = new Form2()
frm2.Show()
'Click handler
Private Sub Number1_Click(sender As System.Object, e As System.EventArgs) Handles Number1.Click
frm2.TextBox.Focus()
Application.DoEvents() 'Sometimes needed
SendKeys.Send("1")
End Sub

Project level conception of modal and nonmodal forms

I have project which consist of several forms and want to open it in certain modality rules which I can't achieve.
First, here is main form "Form1", then "Form11" and "Form111", "Form12" and "form121"
From main form "Form1" I can start only forms "Form11" and "Form12" like this:
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Form11.Show()
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Form12.Show()
End Sub
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
Me.Close()
End Sub
End Class
In this situation, when "Form11" and "Form12" are showed I can easily exit application by pressing Button4 on "Form1" what will close all forms.
Now, here is another form, "Form111" which I open modally by clicking a button on "Form11"...
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim f As New Form111
f.ShowDialog(Me)
f = Nothing
End Sub
And here I have some misunderstanding or misconception of my project.
When "Form111" is opened I like it to block "Form11" but not "Form1" where I would like to (say) open "Form2" or exit application where modal form "Form111" on nonmodal form "Form11" is opened.
Is it possible to achieve such functionality with described project configuration and how?
First, the code for the button clicks in the first block may not be right. If the forms are named Form11 and Form12 that is their class name. They should be instanced as you do with Form111.
The reason the application closes is because that form (Me) is set as the startup form. If/when that closes, the app ends. You can change the app to exit when the last form closes in project properties.
As for your question, to have a dialog "block" "Form11" but not "Form1", the answer is no. Forms are either Modal (what you are calling "blocking") or Modeless. You could tell Form111 to stay on top, but it would not be "blocking" any other form.
What you are trying to do suggests that the operations on these forms may not be as well organized or planned as they need to be.

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.