I have two form A and B. When I close A, I need to close B, and viceversa when I close B, I need to close A. Obviously I can't do it, because I call twice the close method at the same form.
I'm thinking to add the variable to this FormClosed Handles.
Public Sub a_FormClosed(sender As Object, e As FormClosedEventArgs) Handles MyBase.FormClosed
End Sub
in this way:
Public Sub a_FormClosed(sender As Object, e As FormClosedEventArgs, flag as Boolean) Handles MyBase.FormClosed
if flag > true
a.close()
else
a.close()
b.close()
end if
End Sub
and it doesn't work because it isn't correct.
where i mistake?
The Closing event is more appropriate. "... as this form is closing, close the other one..."
Check if an instance of the other form type is open, and close it. Essentially the same code could run for both form's Closing event.
Private Sub Form1_Closing(sender As Object, e As CancelEventArgs) Handles Me.Closing
For Each frm As Form In Application.OpenForms
If TypeOf frm Is Form2 Then
frm.Close()
Exit For
End If
Next
End Sub
(A flag is not necessary.)
Note that attempting to Close a form in its Closed event is redundant.
Related
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 would like to reset my form using vb.net i would try to below code but form is close can't open new form.
Private Sub resert_button_Click(sender As Object, e As EventArgs) Handles resert_button.Click
Dim client = New client_entry
client.Show()
Me.Close()
End Sub
Try Me.Hide and swap the order
Me.Hide()
client.Show()
There could be multiple ways to handle this one but myapproach would be like calling form_shown even where I will load required data and during reset i will call this event.
Private Sub Me_Shown(sender As Object, e As EventArgs) Handles Me.Shown
LoadData() 'This function/sub will load data for this form
End Sub
Private Sub resert_button_Click(sender As Object, e As EventArgs) Handles resert_button.Click
Me_Shown(sender,e)
End Sub
Use Me.Dispose() instead of Me.Close() it will dispose the form then when you call it again Yourform.Show(), It will Generate a new one.
One way of doing this would be to add a property to Form2. I'm assuming that you have two forms Lets call them Form1 and Form2. Somewhere in the code for Form1, you declare an instance of Form2 ..
Dim frm2 As New Form2
and at some point you want to show Form2 as a modal window ..
frm2.ShowDialog()
For the moment lets look at the Form2 code
I'm assuming here that you have a button to reset the form and to close the form, you just click on the form close button in the top right hand corner and maybe you have a button to close the form as well. Consider the following code for Form2
Public Class Form2
Friend Property resetOnClose As Boolean = False
Private Sub btnReset_Click(sender As Object, e As EventArgs) Handles btnReset.Click
resetOnClose = True
Me.Hide()
End Sub
Private Sub btnclose_Click(sender As Object, e As EventArgs) Handles btnclose.Click
resetOnClose = False
Me.Hide()
End Sub
End Class
There is a property called resetOnclose which is a Boolean type. If you click on the reset button, this property is set to True If you click on the close button, the resetOnClose property is set to false.
In all these bits of code, frm2 is Hidden - not closed. This means that the form and its resetOnclose property is still available to Form1. OK now to look at the Form1 code ..
Public Class Form1
Dim frm2 As New Form2
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Shown
frm2.ShowDialog()
Do
If frm2.resetOnClose Then
frm2.Close()
frm2 = New Form2
frm2.ShowDialog()
Else
frm2.Close()
End If
Loop Until frm2.resetOnClose = False
End Sub
End Class
For this example, frm2 is opened as soon as Form1 is shown, but you can put the relevant code anywhere you need
In the Form1.Shown code you will see a loop. The loop will continue looping as long as resetOnClose is True. When frm1 is shown modally, execution in this Form1 code waits until frm2 is hidden or closed. Next, the Form1 code checks to see if the resetOnClose property is true or false. If it's false, frm2 is closed and the loop terminates. If the property is true, frm2 is closed and reassigned a new instance of Form2 in its default state.
Voila!
Currently, I have this, on my Form.vb:
Private Sub txtBox1_Leave(sender As Control, e As EventArgs) Handles txtBox1.Leave
'Some code
End Sub
...
Private Sub txtBox10_Leave(sender As Control, e As EventArgs) Handles txtBox10.Leave
'Some code
End Sub
The thing that bothers me is: All those events are doing the same thing. Is it possible to programatically have a list of the relevant controls and iterate through them, adding such events? This would allow me to reduce the amount of code in my application / coding effort. Something like:
For Each c As Control in listOfControls
'Add event for c here which calls method
Next
I really think there is a simple way of doing that but everything I've tried so far (such as AddHandler) did not work. Any ideas?
Thank you
Yes it is possible and quite simple, create a method that will add the requested event to the Control, pass an array of controls to that method before the form loads:
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
' add events to all requested controls
AddEvent(New Control() {TextBox1, TextBox2, TextBox3, Button1})
End Sub
Public Sub AddEvent(ByVal myControls() As Control)
For Each c As Control In myControls
AddHandler c.Leave, AddressOf Control_Leave
Next
End Sub
Private Sub Control_Leave(sender As Object, e As EventArgs)
MsgBox("Control is not in focus")
End Sub
End Class
So putting it as simply as possible, I have one class which opens a form from inside sub like so:
Public Sub LoadExtension()
'various code...
Dim form as new frmMain
frmMain.ShowDialog()
'various code...
End Sub
And inside this form I have two buttons, one will just close the form, so the LoadExtension() sub will continue. The other button I want to use to 'exit' the LoadExtension() sub inside the main class so that loading stops completely. The button event inside the form module is like so:
Private Sub btnStopLoad_click(sender as object, e as eventargs) handles btnStopLoad.click
'exit the LoadExtension sub somehow
End sub
What is the simplest way to achieve this? I thought I could do something like this in the LoadExtension() sub (after the .ShowDialog):
If frmMain.btnStopLoad.clicked then
Exit Sub
End If
But it won't let me do that from the class module, apparently I need to use 'raise event' or something? I'm not very familiar with the way that events work. Can someone offer me an easy solution? I'd be very grateful. I've been looking around the web for a solution but haven't had any success. Thanks for your time.
You can achieve this by setting the DialogResult of the frmMain.
Public Class frmMain
Inherits Form
Private Sub _ButtonContinueClick(sender As Object, e As EventArgs) Handles ButtonContinue.Click
Me.DialogResult = Windows.Forms.DialogResult.OK
Me.Close()
End Sub
Private Sub ButtonExitClick(sender As Object, e As EventArgs) Handles ButtonExit.Click
Me.DialogResult = Windows.Forms.DialogResult.Cancel
Me.Close()
End Sub
End Class
Then change the LoadExtension method to something like this:
Public Sub LoadExtension()
'various code...
Using form As New frmMain()
If (form.ShowDialog() <> Windows.Forms.DialogResult.OK) Then
Exit Sub
End If
End Using
'various code...
End Sub
The simplest way is to use dialogresult to let the LoadExtension function know which button was pressed.
DialogResult is what you are looking for - the standard way to do it.
You can, however, subscribe to events of another form's controls, like this:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim frm As New Form2
AddHandler frm.Button1.Click, AddressOf OtherFormButton_Click
frm.ShowDialog()
End Sub
Private Sub OtherFormButton_Click(sender As System.Object, e As System.EventArgs)
MessageBox.Show("hello")
End Sub
This is assuming Form2 has a button named Button1.
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.