0

I know this is very discussed issue, but I didn't find good answers. I have a MainForm containing an instance(s) of Form1 (DataGridView list), which has a subform Form2 (containign details of selected record from Form1). Form1 can have multiple instances (can be loaded into different independent tabs), Form2 can be loaded as independent form into a tab or (and this is the case) into Form1 as detail of selected record.

I can easily refresh the Form2 from Form1 when selecting a record (by defining "Protected f2 As New Form2" I can run subs from Form2). But how do I run a sub in Form1 from Form2? I googled:

  1. Get Parent reference like

    Dim theParentForm1 As Form = TryCast(Me.Owner, Form)
    theParentForm1.MyRefreshSub()
    

    ...this makes sense, because the instances are always paired. But code above doesn't work for me, I cannot access "MyRefreshSub()" from theParentForm1. Perhaps i declared something incorrectly...

  2. Interface - I read a lot of recommendation to use interface, but I didn't find a single example of using interface to refresh a parent form!

Could someone point me toward a solution?

Regards,

Libor

Oak_3260548
  • 1,882
  • 3
  • 23
  • 41
  • your question is very confusing. the title asks one thing, then `how do I run a sub in Form1 from Form2` seems to want the reverse. That said, `Dim theParentForm1 As Form = TryCast(Me.Owner, Form)` wont help much because there is no `MyRefreshSub` defined in `System.Windows.Forms.Form`. Try casting as `Form1` or whatever class actually has that method. – Ňɏssa Pøngjǣrdenlarp Oct 20 '14 at 12:04
  • Thanks, but this doesn't work, I'll write describe it under the answer bellow – Oak_3260548 Oct 20 '14 at 13:18
  • I suspect this is an XY problem to begin with. I strongly suspect your notion of a subform is to embed a form into a TabPage, which is something you might mention if that is the case, because it is a rather odd thing to do. Otherwise, you'll have to expand on "this doesnt work". If `MyrefreshSub` is Public and `thisForm` is cast as an instance of the form class containing it, then `thisForm.MyRefreshSub` should work. – Ňɏssa Pøngjǣrdenlarp Oct 20 '14 at 13:46
  • Sorry, it took me some time to post detailed description bellow. It is as you say, the form is loaded into a TabControl1.tabpage1. I'm using this simple "frame work" for few years and works fine, but I didn't have to do backward cross-references up until now. In my case, after compiling, referencing .parent from Form2 dosen't return reference to Form1, but to MainForm. I think this is the key problem. – Oak_3260548 Oct 20 '14 at 14:39

2 Answers2

1

You can do as @Plutonix posted and cast the parent form to Form1 and call the sub

Dim myParentForm As Form1 = TryCast(Me.Owner, Form1)
myParentForm.nameOfSubToRun()
F0r3v3r-A-N00b
  • 2,903
  • 4
  • 26
  • 36
  • Thank you for your suggestion, but this only works against MainForm, not against Form1. Actually, regardles if you use .Owner or .ParentForm or .ParentForm.ParentForm, it always references MainForm as parent, not Form1. I can reference Form1 from the main form as myParentForm.Form1.nameofSubToRun(), but then it (obviously) throw an errror "Object reference not set to an instance of an object."... – Oak_3260548 Oct 20 '14 at 14:13
  • you get the NRE because TryCast failed. that means that Owner is not an instance of `Form1` your trycast would change for each form type. A more explicit parent reference rather than Owner would help too. @user3260548 – Ňɏssa Pøngjǣrdenlarp Oct 20 '14 at 14:46
  • I agree, but I don't know what move I should do, I doon't know any other way of referencing parent form and I don't really see any examples applicable to this case on the internet... – Oak_3260548 Oct 20 '14 at 15:28
  • ...what if I updated ALL instances of Form1, that should be simple enough, isn't it? And it makes sense too... – Oak_3260548 Oct 20 '14 at 15:33
1

OK, based on the suggestion which Plutonix brought up, I went other direction and decided for a workaround - to update ALL instances of Form1. It works this way:

    For Each CurrentForm As Form In Application.OpenForms
        If CurrentForm.Name = "Form1" Then
            Dim Form1Instance As Form1 = DirectCast(CurrentForm, Form1)
            Form1Instance.nameOfSubToRun()
        End If
    Next
Oak_3260548
  • 1,882
  • 3
  • 23
  • 41
  • This worked perfectly. Thank you! It might not work if multiple forms with the same name are open, though. – pianocomposer Jan 22 '21 at 15:31
  • @user1000008 That could happen indeed. I finally used it in a different way, so thtat instances can't be mixed, see my answer https://stackoverflow.com/questions/45466186/send-data-from-form-1-to-form-2/45466668#45466668. It has been battle proven in many many forms over the years, it's very simple and reliable solution. – Oak_3260548 Jan 22 '21 at 16:42