Captain Sensible
Captain Sensible

Reputation: 3

Move a parent form from a sub in a child form VB.Net

I have a sub in a child form that resets settings for my application. One of which is the start location of my parent form. I want the parent form location to be updated by the reset_click event. Partly this is because the Parents closing event stores the forms last location, so I need to move it before it gets closed.

Child forms code:

Public Sub Reset_Click(sender As System.Object, e As System.EventArgs) Handles Reset.Click

    My.Settings.Reset()
    My.Settings.Save()

    TextBox2.Text = My.Settings.FilePath
    TextBox1.Text = My.Settings.VerticalExaggeration

    Dim frm As New Global.JJsGEControlPanel.Form1()
    frm.MoveFrm()

End Sub

The MoveForm Sub in the Parent:

Public Sub MoveFrm()
    Me.Location = My.Settings.MainFormLocation
    Me.Refresh()

End Sub

The MoveFrm Sub works if executed from within the Parent Form but wont if executed from the child click event? I'm stumped!

Upvotes: 0

Views: 424

Answers (1)

Steve
Steve

Reputation: 216342

This line creates a new instance of the first form. An instance that is never shown. The command is executed by this hidden instance and thus you don't see your original form move anywhere.

Dim frm As New Global.JJsGEControlPanel.Form1()
frm.MoveFrm()

To simplest way to solve your problem is passing the original instance of the Form1 to the child form, save this passed instance and then use it when you need to move the original form

In your Form1 (when you create the instance of the child form) you write

Dim child = new frmChild(Me)

in your child form constructor

Private callerForm as Global.JJsGEControlPanel.Form1
Public Sub New(ByVal callerInstance as Global.JJsGEControlPanel.Form1)
   callerForm = callerInstance
   InitializeComponent()
End sub

and finally when you need to move the main form

callerForm.MoveFrm()

Upvotes: 1

Related Questions