1

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
Timmy109
  • 11
  • 1
  • What is `frmHome`? Is it the form class or is it an instance of the form? Anyway, try `Home.Location = Me.Location`. – 41686d6564 stands w. Palestine Aug 17 '20 at 03:24
  • `frmHome` is the name of the home form (Main form). But yes I'll try that now. It works if I use the design name of the form instead of the variable. Not sure why that variable is even there or what the point of it is. Thanks for the quick response! – Timmy109 Aug 17 '20 at 03:29
  • What you're doing is that you're using the [default instance](https://stackoverflow.com/q/4698538/8967612) of the form. Example: `form1.Show()`. Although this works, it's only there for backward compatibility. It only works in VB (not C#), only works with Form classes (not other classes), and it has other limitations. Save yourself a lot of trouble and get used to creating new instances moving forward. Example: `Dim frm As New Form1() : frm.Show()` instead of directly calling `frm.Show()`. – 41686d6564 stands w. Palestine Aug 17 '20 at 03:34
  • Gothcha, thanks for that! This is just for a school project though but I will keep that in mind for when I do things that are actually important ;) – Timmy109 Aug 17 '20 at 03:38
  • It seems as though on the first opening of the form, it still goes to the default position stated in the properties. However after being opened once, I can switch between them no problem in any position and it retains said position. Any way I can fix this? EDIT: Fixed this by changing the startPosition property to `manual`. Thanks for the help :) – Timmy109 Aug 17 '20 at 03:41
  • The last time I was in this situation, I used custom controls instead of forms, where I had ONE base form and made the correct control visible/enabled on the one form. – Joel Coehoorn Aug 18 '20 at 01:36

1 Answers1

0

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

Timmy109
  • 11
  • 1