Passing a complete array inbetween two forms.

muckmail

New member
Joined
Aug 31, 2014
Messages
4
Programming Experience
Beginner
I am trying to find out how to create a complete array in form 1 and then transfer & use it in form 2.

Does anyone have any code showing that? Please note I am a beginner so please be complete.

I would like to know the proper way of doing this in which I guess properties get/Set statement
may be required. I am not good with properties get/Let statement but I will try.

I would also like to know the easiest way to accomplish this task if I get overwhelmed
with Properties Get/Set statements.

Thank you,
 
Passing variables and arrays between forms

Declare the array as Public in the first form.

VB.NET:
Public Class Form1

    Public myarray() As String
    Public mynum As Integer

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        myarray = {"one", "two", "three", "four"}
        mynum = 5
        Form2.Show()
    End Sub

 End Class


Public Class Form2

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        MessageBox.Show(Form1.mynum.ToString)
        For Each itm As String In Form1.myarray
            ListBox1.Items.Add(itm)
        Next itm
    End Sub

End Class
 
While what Solitaire has shown will work, it's not really the "proper" way. I would suggest that you follow the Blog link in my signature below and check out my three-part post Data With Multiple Forms. Part 3 is the most relevant but it's good to get the introduction to the less-correct alternatives in the first two parts.

On the subject of the array, an array is just an object like any other, so you treat it like any other object. If you know how to move a String around then you know how to move an array around.
 
So I should be able to move an array from one form to another as an object.
Ok, so here is my code.
Form 1:
VB.NET:
Public Class Form1
    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        Dim Myform2 As New Form2
        Form2.Show()
    End Sub
End Class
Form 2:
VB.NET:
Public Class Form2
    Private Shared Filelist As New ArrayList() 'FileList Array set as private

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        Dim file As String
        OpenFileDialog1.Filter = "Text Files (*.txt)|*.txt|All Files (*.*)|*.*"
        OpenFileDialog1.FilterIndex = 1
        OpenFileDialog1.Multiselect = True
        If OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
            For Each file In OpenFileDialog1.FileNames
                ListBox1.Items.Add(file)
                Filelist.Add(file)
            Next
        End If
    End Sub
    Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
        Dim x As Integer
        Dim s As String
        x = 0
        For Each s In Filelist
            MsgBox(Filelist(x))
            x = x + 1
        Next
    End Sub

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

    End Sub

    Private Sub Form2_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load

    End Sub
End Class

So in form 2 I made an array called Filelist. So how can I move that compete array to form 1 and use it
in other form 1 subroutines? So Filelist can be properly move to form 1 as an single object?

Thank you,
 
This code:
VB.NET:
Public Class Form1
    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        Dim Myform2 As New Form2
        Form2.Show()
    End Sub
End Class
is wrong. You declare a variable of type Form2, create a new Form2 instance and assign it to that variable, then you never use that instance but instead display the default instance. Nothing good can come of that. This is a perfect example of why many people, myself included, think that default form instances should never have been added as a feature to VB.NET. They create as many problems as they solve.

I suggest that you read a couple of my blog posts. Start with the one on Default Form Instances to get an understanding of what they are and then all three parts of the Data Among Multiple Forms post. If I remember correctly, the first part deals with using default instances. It's the third part that shows you the "proper" way to do it though.
 
Where is "Default Form Instances"? Is it in your blog archive by year and month. I did not find it.
Thank you,

Yes, it's there. I could provide a link but I'd just have to go follow the link in my signature and then expand each year and month until I found it. You're just as capable of doing that as I am.
 
Back
Top