How to dispose of a form within a panel upon closing the main form or upon another button click event? - vb.net

Disclaimer: I only have beginner to slightly intermediate experience with VB.net
Hello, I was tinkering around with some design ideas for a project and I ran into a problem that I haven't found a solution to. I have a win form with some buttons and a panel. When the user presses a button, a border-less form is loaded into the panel. The problem is this: when the main form is closed, Visual Studio does not stop debugging, presumably because the forms in the panel are not disposed of.
Win Form image
The instance of the panel form is declared in the button click event. How can I destroy that instance from another sub? If I click another button, the first panel form doesn't go away. Is there a better way to accomplish this? I'm still learning, so I'm often not aware of all the different ways to solve a problem. Thanks everyone!
Public Class frm_Clients
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim search As New Search
search.TopLevel = False
search.Dock = DockStyle.Fill
Panel1.Controls.Add(search)
search.Show()
End Sub
Private Sub frm_Clients_FormClosed(sender As Object, e As FormClosedEventArgs) Handles Me.FormClosed
' What should I write here?
End Sub
End Class

Here's a snippet of what to do to close the windows when another button is pressed.
For Each form In Panel1.Controls.OfType(Of Form).ToList()
form.Close()
Next
Then I would suggest that you set search.Owner to the Form holding the Panel. That means that when the Owner is closed, so are the children.
search.Owner = Me

Related

VB.net How to hide dialogue without close the application

I have problem about close() or dispose() function with my barcode reader (Windows Embedded Compact 7). In this case I can only hide() form.
I tried to show Form2 as dialogue but after I clicked the close button (to hide this form and go back to Form1) It made all of my application close
In Form1 (Main):
Public Sub showForm2()
Dim secForm As New Form2
secForm.ShowDialog()
End Sub
In Form2:
'close button
Private Sub closebt_Click(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles closebt.Click
Me.Hide()
End Sub
Form can't be hidden if it is shown as Dialog. If you want to hide the form then use form.show() rather than form.ShowDialog(). Also here is a link
http://www.vbforums.com/showthread.php?759061-How-can-i-hide-my-second-form-dialog-without-bliking-form-not-closing-my-first-form
Go to properties page of the project. In the Application tab, there is a setting:
Shutdown mode
When startup form closes
When last form closes
Choose "When last form closes" to prevent closing the application when your main form closes.
in the index form add in form closing the fallow code:
Form1.Dispose()

Why does my new form move behind the opener?

I'm not going to post a bunch of code here since I do not think it is a code issue.
Here is a link to my original question where I have shown the code if you are interested. Code
Just as a test I created a blank form window (Form1.vb) and no code gets passed to it and no code runs when it opens. If I do Form1.Show() from a MenuStrip Control or a Button Control, the window opens and stays on top. Now if I do Form1.Show() from a TreeView Control, the window opens and goes behind the window with the TreeView Control.
So my question is, what is different about the TreeView opening a form vs a button or other control?
I am using the basic VB TreeView Controll, and the new form is being called in AfterSelect method for the TreeView.
The AfterSelect works if you use your keyboard navigation to select a node, but it doesn't work when you use the mouse because the mouse capture is forcing the parent form to remain in focus. You would have to run your code after the AfterSelect event:
Private Sub TreeView1_AfterSelect(sender As Object, e As TreeViewEventArgs) _
Handles TreeView1.AfterSelect
Me.BeginInvoke(New Action(Sub()
Dim f2 As New Form2
f2.Show(Me)
End Sub))
End Sub
Use the Form.Show(parentForm) option, this will always put the new form on top of the old one.
have you tried Form1.ShowDialog() ? or if you don't want to show it as a dialog you should use:
Form1.Show()
Form1.BringToFront()

Need To Close A Panel When Clicked Outside (Lost Focus)

I have an issue that I cannot seem to overcome.
In my application, I have a custom class that loads a form into a panel upon startup. Then when I click a button on my main form I show the panel as visible revealing the form to the user.
My problem is that I want to be able to hide the panel when a user clicks outside of it (back onto the main form).
so far I have tried Form_Deactivate, Form_Leave, Form_LostFocus, Panel_Leave and Panel_LostFocus events but nothing will seem to trigger an event consistently to hide the panel. The only thing that works is if the user clicks inside the form (on a listview control) once the form is visible and then clicks outside of the form.
Is there anyway I can ensure this event gets called everytime whether the user clicks the form or not?
So far my code looks something like:
Public Class cls_UserObjects
Private frm As frmUsers
Public pnl As Panel
Public Sub ShowUserPanel()
Try
frm = New frmUsers
frm.TopLevel = False
pnl.Controls.Add(frm)
frm.Show()
frm.Focus()
....
End Class
Then in my main form I call the code below to build the form into the panel:
class_Users.pnl = pnlUsers
class_Users.ShowUserPanel()
And pnlUsers.Visible = True to show it to the user
I just can't seem to close it. I understand that Panels don't support the LostFocus properly, however, I can't find away around this. Maybe it has something to do with how I am opening my form/panel but I was advised to use classes to open forms so I can have more control over the controls within my forms from outside calls.
Any help appreciated. Thanks
MouseLeave event works, the panel hides immediately once it leaves the panel.
Private Sub Panel1_MouseLeave(ByVal sender As Object, ByVal e As System.EventArgs) Handles Panel1.MouseLeave
Panel1.Visible = False
End Sub

Open Form as ShowDialog But Close Initation Form

Is there any way to do the following, other than hiding then closing the hidden form later?
Mainform opens SecondForm as show dialog, i need to Open ThirdForm from SecondForm while closing SecondForm while keeping third form acting as "showdialog" on the MainForm?
When you show SecondForm(), pass in MainForm() as the owner to ShowDialog():
Public Class MainForm
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim sf As New SecondForm
If sf.ShowDialog(Me) = Windows.Forms.DialogResult.OK Then
' ... do some processing in here ...
End If
End Sub
End Class
Now, in SecondForm(), you can then set the owner of ThirdForm() to that of SecondForm():
Public Class SecondForm
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Me.Hide()
Dim tf As New ThirdForm
tf.ShowDialog(Me.Owner)
Me.DialogResult = Windows.Forms.DialogResult.OK
End Sub
End Class
You could simply open the third form from the main form as soon as the second form returns a dialog result
You also might want to look at MDI this gives you more control over what the user can and can't do.
After trying Idle_mind suggestion it was still giving me problems going back and forth between forms continously showing them as .showdialog. I solved my problem just like tinstaafl suggested. I wish i would have got his message before a couple hours of trying different methods before coming up with this one.
When i close each form, i set a boolean flag in the main form. then i call a sub that is in the main form to show the next form as showdialog from the main form. i use the flag which triggers logic in the form im loading whether or not to bind data from datatable so i can edit it.
sorry with all those forms i know it gets confusing. to sum it up, close the dialog form (me.close), set flag so calling code knows what to do once the showdialog code is satisfied.

How to open a new form but closing the old one in VB

I have a welcome to my application as it loads up, but then need to have that form close and login form open when the continue button is hit.
My code:
Me.Close()
Dim Login As New Form
Login.Show()
When I click the button it only closes the welcome form and then ends the application. If you can help thanks! :)
You can set the properties of the project to select "When last form closes" in the shutdown mode dropdown
Update:-
"Project" menu -> 'YourApp' Properties... -> Application Tab
find : "Shutdown Mode"
Change from
"When startup form closes" --> "When last form closes"
show the form before the close.
Dim Login As New Form
Login.Show()
Me.Close()
Better is if you use Me.Hide()
There is a shutdown mode project property. This controls the application lifecycle.
Make sure you set this to "When last form closes"
Then your code should work as you expect.
What is happening is that you have that setting set to shutdown "when startup form closes", so by doing Me.Close on the startup form, this shuts down the application, all code after this line is effectively ignored.
If your Welcome Form isn't your main form, you just need to put your Me.Close after your Login.Show()
Dim Login As New Form
Login.Show()
Me.Close()
Try this..
On your welcome form when closing:
Me.hide()
Dim Login As New Form
Login.Show()
On your login form when in loading event:
Private Sub Login_Load(sender As Object, e As EventArgs) Handles MyBase.Load
WelcomeForm.Close()
End Sub
This will try to hide the first form and load the second form. And when second form is completely loaded it will try to close the first form.
Make sure that on your Application Tab under your Project properties the option is set to "When last form closes".
If you close sub main form from application, your application will close. However, you can close and open other forms if they are not the sub main form. Maybe you can just hide it instead.
You just need to put Hide() instead of Close :)
So for example, in the project im doing right now...
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click // Button1.Click is your "continue" button
Hide()
LogInFrom.Show()
End Sub