VB using me.close before opening new form - vb.net

Hi Guys i'm new to Visual Basic Coding, and i can't seem to get where's my mistake on my coding, i'm trying to create a button that opens a new form while closing the current form.
i have two forms, form 1 is MainForm, form 2 is SearchForm
Whenever i use this code:
Private Sub SearchMButton_Click(sender As Object, e As EventArgs) Handles SearchMButton.Click
MainForm.Close()
SearchForm.Show()
End Sub
End Class
it will generate an error and says i need to replace MainForm.Close() into Me.Close()
When i Use this
Private Sub SearchMButton_Click(sender As Object, e As EventArgs) Handles SearchMButton.Click
Me.Close()
SearchForm.Show()
End Sub
End Class
It closes both Forms and it doesn't leave any Form Open. Kindly direct me to the proper path, thanks in advance.

You need to Hide the form rather than closing it. Since it's your main form, when it closes, the application exits.

Standard UI guidelines are to leave the main form open, and open search form on top of that. If you need to block the main form, while search criteria are selected, use .ShowDialog, instead of just .Show.
.NET WinForms programming pattern kind of implies that you never close your main form. If you deviate from this approach, you are guaranteed to encounter all sorts of layout and display issues. So please don't. You can .Hide the main form, if it needs to go to system tray or run in background.

Related

Showing a form in the position where last form was hidden (VB.net)

For a school project, I am developing software that requires the use of multiple forms.
At the moment, when a button is clicked to show a form and hide the current one, it opens in the default location that is set in the properties.
I would like it to know where the last form was hidden, so that it can open the new form in the same location. This is vital as all the forms are the same shape/size and it'll make for a better user experience if the software retains the position that it is moved into.
Thank you for any help! If you have anymore questions, please ask as this is my first time asking a question on here and don't know if I included all relevant information!
Current code for hiding, and showing the form,
This is opening the main form from another form when a button is pressed:
Private Sub btnHome_Click(sender As Object, e As EventArgs) Handles btnHome.Click
Dim Home As Form
Home = frmHome
Me.Hide()
Home.Show()
End Sub
This was fixed by using the line: frmHome.Location = Me.Location and changing the property of the StartPosition (of the form) to Manual
Thanks to this user!
https://stackoverflow.com/users/8967612/41686d6564

Prevent old form closing until new form opens

A very basic/simple question with I'm sure, an even simpler answer, but I just cannot figure it out. I have two forms that a user can switch between using the corresponding menu link on each form. I want to be able to keep the previous form visible on screen until the new form is displayed. In it's current state, the form disappears off screen for around 3/4 of a second before the new one is shown and from a UI/design perspective, I'd like this to stay on screen.
I'm currently using the below code to close and open the forms:
form1.Show()
Me.Close()
form2.Show()
Me.Close
I have tried experimenting with ShowDialog() which does seem to keep it on screen on first run, but clicking back into the form a second time says an error message:
System.InvalidOperationException: 'Form that is already visible cannot be displayed as a modal dialog box. Set the form's visible property to false before calling showDialog.'
Is there a simple line of code to achieve what I want here?
If there is some time consuming code in the Form.Load event (for example, if data is being retrieved from a database) then the following code might help.
Private Sub Form2_Shown(sender As Object, e As EventArgs) Handles Me.Shown
'Assuming default instances
Form1.Close()
End Sub
Posting an answer incase there's an inherit reason this is not best practice or to avoid doing this, but using the below sped it up:
Private Sub menu1_Click(sender As Object, e As EventArgs) Handles menu1.Click
form1.Show()
form1.Refresh()
Me.Close()
End Sub
This was inspired by adding in a Application.DoEvents working, so adjusted the code to avoid that dreaded line.

Hard Time Closing Forms

Alright, so I am writing a program in VB that has 2 forms. These forms are to rotate via button click and be able to close the entire program no matter what form is being shown. As of now, what is happening is, that if I launch the program and click X to close, it will close. However, if I click the button to show Form2, the close button X doesn't close the program and the program keeps running. Also consider that if I change from Form1 to Form2, then back to Form1, Form1 no longer has the ability to close the program like it does before the button click. Any help? This the code I am using in each for, minus the close commands. I feel like the command for closing the forms needs to be outside of the Private Sub for the Button.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
'Switches to Daily
Dim FirstForm As New Form1
FirstForm.Show()
Me.Hide()
'TODO: Close Program on X
End Sub
me.Close() ' Will close the form.
application.Exit ' Will close the application.
Read through .NET End vs Form.Close() vs Application.Exit Cleaner way to close one's app for a better description for this. Some good learning.

Sharing Controls from multiple forms/classes

I'm currently stuck in how to share controls between two forms.
This is what i want to do:
- I have several forms that have their own function.
- Now i want to bring them all to gather on one mainform.
Normally i do this by creating the following code:
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim f As New Form2
GroupBox1.Controls.Add(f.Get_Groupbox_controls)
End Sub
End Class
This add all the control containing in Form2.Groupbox1 and adds it to Form1.Groupbox1.
Normally this works just fine for me.
Where "Get_Groupbox_controls" is a function that returns Form2.Groupbox1
Or if this doesn't works i display the entire form2 over form1 with a transparancykey. (this isn't a need solution, you have to do a lot of location calculating, but it works)
However now i got a form that has a lot going on. For the mainform i only want to display one Container(groupbox) but when the user clicks "More setting.." then it has to show the entire form with all the control on it.
The problem is when i use the container.controls.add() function i get an exception "Invoke or BeginInvoke cannot be called on a control until the window handle has been created "
The Error is logical because the entire form isn't made yet. But in order for the form to work properly it has to update the "hidden" UI controls (labels/buttons/etc)
So the main question is, how can i "port" a selection of controls from Form1 to form2 and keep all the handlers etc.. on Form1
If you have two forms where you want to place identical looking/functioning GroupBox sections, you may want to consider creating a custom user control that contains the shared functionality and can be placed in each form. User controls can even be added to a form through the form designer in Visual Studio.

close form when application loses focus

My form is displayed as TopMost on my application. The problem I have is that whenever I minimize my application or it loses focus, the form remains displaying. I want to be able to minimize my application or move to another and also hide or close my form. Once the application regains the focus, then unhide or open the form again.
Here is what I worked out on the form's closing event:
Private Sub frmNavigation_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
Static Minimize As Boolean
If Minimize = True Then
e.Cancel = True
Me.Hide()
End If
End Sub
I tried using the same code in the applications WindowDeactivate event but nothing happens.
You do not show how you create the instance of your frmNavigation. I am assuming that you are using the Show Method, so just use the version of Show that you pass in the top level window. That will assign the owner of the form, it will then stay on top of your Main Form and minimize and restore with it also. If this doesn't work please show how you are creating and showing your form.
frmNavigation.Show(Me)
I was able to find an answer to the question. MSDN had an article on this very issue.
it can be found here: http://support.microsoft.com/kb/186908#appliesto