Click here to Skip to main content
15,888,908 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi,
Hope all of you are well and settling into 2018 :)

I'll break this down into some simple terms as the project is quite complicated to describe, so here's the basics.

Form1 creates a new instance of Form2 and shows it as showdialog - I cannot access the List declaration from Form2, into Form1.

I cannot have global declarations or variables - explained further, below.

What I have tried:

I have two forms, both created in the designer.
Form1 creates a new instance/copy of Form 2 via

Dim NewForm As New Form2
                NewForm.ShowDialog()


I am using showdialog but I am happy to use just show but that gives me a new set of problems, which is to find out from Form2, that calling Form, that is creating a new instance/copy of Form2, has been closed.

The project will contain many more forms i.e. Form3, Form4, Form5....Form20, all creating a new copy of Form2 and need to access "that copy of List(of String)" from Form2, so a global declaration of the List(of String) is not possible.

Those calling forms, to Form2 - may also be created via a new form command, from other forms. So keeping track via variables of whether that form has been closed, is no good to me.

On Form2, I have some data that I am storing in a
List(Of String)


In Form2, I have declared at the top of the class:

Dim _Keywords As New List(Of String)

   Friend ReadOnly Property Keywords() As List(Of String)
       Get
           Return _Keywords
       End Get

   End Property



I need to access (from Form1 ,3,4,5,6....20) the List(Of String) on Form2 before it gets disposed/closed/set to nothing and not any sooner.

At the moment I do this in Form1 after the showdialog:

 Dim Kwords As List(Of String) = GlobalFind.Keywords
NewFrm.Dispose
NewFrm=Nothing



in Form1, Kwords list its always empty, I get a count=0 when I look. Hope that makes sense and Thank you for giving me your time (and hopefully a solution I have overlooked).
Posted
Updated 5-Jan-18 21:15pm

1 solution

Expose a property on Form2, then get the results in Form1:

Form2
VB
Public Class Form2

    Private mResults As List(Of String)
    Public Property Results() As List(Of String)
        Get
            Return mResults
        End Get
        Set(ByVal value As List(Of String))
            mResults = value
        End Set
    End Property

    Private Sub butSave_Click(sender As Object, e As EventArgs) Handles butSave.Click

        ' set results here
        Results = New List(Of String) From {"111", "222", "333"}

        Close()

    End Sub

End Class


Form1
VB
Public Class Form1

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

        Dim NewForm As New Form2
        NewForm.ShowDialog()

        Dim results As List(Of String) = NewForm.Results

    End Sub

End Class
 
Share this answer
 
v2
Comments
Maciej Los 7-Jan-18 7:05am    
5ed!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900