-1

I designed two forms in VB. The main form sets the height and width of the 2nd form and opens it with .ShowDialog(), everything is fine.

Now I need to open the 2nd form non-modal with .Show(), but some things don't work now:

  1. The height and width properties of the 2nd form cannot be get or set in the main form. I set all properties after loading the main and get them before closing the main.
  2. When I change the form size, close the form and open it again, the size always goes back to default.
  3. The main form stays always in background. It's not possible to bring it to front. The 2nd form always hovers above.

Any ideas? I use .NET 4.5 and Visual Studio 2019.

Here's some code:

Public Class FormMain

    Private Sub FormMain_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        LoadWindowPos()
    End Sub

    Private Sub FormMain_FormClosing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing
        SaveWindowPos()
    End Sub

    Private Sub Button_Click(sender As Object, e As EventArgs) Handles Button.Click
        Form2.Show()
    End Sub

End Class

Module ModuleGlobal
    Public Sub LoadWindowPos()
        'Get integers from file
        Form2.Height = 100
        Form2.Width = 200
    End Sub

    Public Sub SaveWindowPos()
        Dim sLine as String = Form2.Height.ToString & ";" & Form2.Width.ToString
        System.IO.File.WriteAllLines("filename.ini", sLine, System.Text.Encoding.Default)        
    End Sub 

End Module
Trevor
  • 7,777
  • 6
  • 31
  • 50
Stefan L.
  • 65
  • 10
  • 2
    Please read how to create a minimal, reproducible example: https://stackoverflow.com/help/minimal-reproducible-example. – daShier Sep 04 '19 at 13:15
  • 1. Those properties most certainly **can** be get and set from the main form, but since you have shown no code, we cannot give any solid suggestions to help you. 2. That is to be expected. When a form is shown using the `Show` method and then closed, it is automatically disposed. Any new instances of that form would have the values that were set at design time. 3. We would need to see the code you used to show the respective forms to help you. – Chris Dunaway Sep 04 '19 at 13:25
  • I added some code in my question. – Stefan L. Sep 04 '19 at 13:36
  • If you want to set the form's properties, you need a reference to it. I see that you use default instance of the form when you set its properties (Form2.Height). – Han Sep 04 '19 at 13:43
  • But this works if I use ShowDialog(). What kind of reference do you mean? – Stefan L. Sep 04 '19 at 13:56
  • https://stackoverflow.com/questions/4698538/why-is-there-a-default-instance-of-every-form-in-vb-net-but-not-in-c – Steve Sep 04 '19 at 14:26
  • I don't think `File.WriteAllLines("filename.ini", sLine, System.Text.Encoding.Default)` will compile. No overload matches your arguments. – Mary Sep 05 '19 at 01:13
  • 1
    Reading the names of your methods `SaveWindowPos()` and `LoadWindowPos()` I would expect you were referring to the window position. Height and Width properties refer to Size not position. – Mary Sep 05 '19 at 01:21

1 Answers1

0

After I fix

Dim sLine as String = Form2.Height.ToString & ";" & Form2.Width.ToString

To

Dim sLine() As String = {Form5.Height.ToString & ";" & Form5.Width.ToString}

The code compiles and behaves as expected.

Mary
  • 14,926
  • 3
  • 18
  • 27