Passing data between forms / Accessing data from another form

Hi,

I am rather new t the .net platform.

I have a form (frmSelect1)  with 2 listboxes on (List1 and List2).  A default list of items is in List1 and the user can then select/drag items from List1 to List2.

The user then clicks on button Next.  HOw can i access the items in List2 from the next form (or anything for that matter).  I dont want to create a new object( Dim frm1 As New frmSelect1) because then it creates a new instance of the form.

I dont believe that this can be difficult because it has to be used in alot of other programs.

Thanks in advance!!
ChappsAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Daniellus83Commented:
Hi Chapps,

I posted once a program with 3 forms, wich would be able to communicate. In the Old VB6 this goes quite easy but now with the whole net .Net env. this is quite difficult.

We now need a so-called 2-way communication. Try to understand this example, this should do the trick:

(With this code you are able to acces other components on other forms by using e.g. Form1.ListBox1..... while you are on Form2 or so...


The Contents of the Forms:
--------------------------------------------------------------------
3 forms;
  - Form_Main
  - Form1
  - Form2

- Form_Main, with 4 buttons;

  - one button to SAVE all setting,
  - one button to LOAD all settings,

  - one button to OPEN Form1 with 3 items;
     - 2 checkboxes
     - 1 button to SAVE the settings of this form1 to Form_Main

  - one button to OPEN Form2 with 3 items;
     - 2 textboxes
     - 1 button to SAVE the settings of this form2 to Form_Main
--------------------------------------------------------------------


The extra code needed in Form1.vb:
------------------------------------------------------------------------------------------------------------------
    Public myCaller As Form_Main

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        myCaller.Checked1 = CheckBox1.Checked
        myCaller.Checked2 = CheckBox2.Checked
    End Sub
------------------------------------------------------------------------------------------------------------------


The extra code needed in Form2.vb:
------------------------------------------------------------------------------------------------------------------
    Public myCaller As Form_Main

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        myCaller.Checked1 = CheckBox1.Checked
        myCaller.Checked2 = CheckBox2.Checked
    End Sub
------------------------------------------------------------------------------------------------------------------


The code needed in Form_Main
------------------------------------------------------------------------------------------------------------------

    'Making a clone of the designed forms to be able to use/access them
    'Form1 with checkbox1 and checkbox2 && Form2 with textbox1 and textbox2
    Public Form1Clone As Form1
    Public Form2Clone As Form2

    'Now we define a plavce to store the setting. This is needed because after close a form (e.g. form1) the setting are destroyed.
    'Make sure these variables are NOT set private, else they can't be reached!!
    Public Checked1 As Boolean
    Public Checked2 As Boolean
    Public Text1 As String
    Public Text2 As String


    'Buttons to make Form1&Form2 visible AND load the settings
    Private Sub Show_Form1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Show_Form1.Click
        Form1Clone = New Form1
        Form1Clone.myCaller = Me
        Form1Clone.CheckBox1.Checked = Checked1
        Form1Clone.CheckBox2.Checked = Checked2
        Form1Clone.Show()
    End Sub
    Private Sub Show_Form2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Show_Form2.Click
        Form2Clone = New Form2
        Form2Clone.myCaller = Me
        Form2Clone.TextBox1.Text = Text1
        Form2Clone.TextBox2.Text = Text2
        Form2Clone.Show()
    End Sub

    Private Sub Button_Save_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_Save.Click

        Dim nr As Integer = FreeFile()
        Dim FileName As String

        If SaveParameterFile.ShowDialog() = DialogResult.OK Then

            'Opening file
            FileName = SaveParameterFile.FileName & ".cnf"
            FileOpen(nr, FileName, OpenMode.Output)

            'Write all settings to file
            WriteLine(nr, Checked1) 'Checkbox1
            WriteLine(nr, Checked2) 'Checkbox2
            WriteLine(nr, Text1) 'Textbox1
            WriteLine(nr, Text2) 'Textbox2

            'Close file
            FileClose(nr)
        End If
    End Sub


    Private Sub Button_Load_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_Load.Click

        Dim nr As Integer = FreeFile()
        Dim value As Boolean
        Dim FileName, text As String

        If LoadParameterFile.ShowDialog() = DialogResult.OK Then

            'Opening file
            FileName = LoadParameterFile.FileName()
            FileOpen(nr, FileName, OpenMode.Input)  'Open file for output.

            'Read piece-by-piece all data from file:

            'Form1Clone:Checkbox1
            Input(nr, value)
            Debug.WriteLine(value)
            Checked1 = value

            'Form1Clone:Checkbox2
            Input(nr, value)
            Debug.WriteLine(value)
            Checked2 = value

            'Form2Clone:Textbox1
            Input(nr, text)
            Debug.WriteLine(text)
            Text1 = text

            'Form2Clone:Textbox2
            Input(nr, text)
            Debug.WriteLine(text)
            Text2 = text

            'Close file
            FileClose(nr)

        End If

    End Sub
------------------------------------------------------------------------------------------------------------------
0
ChappsAuthor Commented:
What if you only hide the previous form and not close it.  Is there an easier way to do it then?
0
Daniellus83Commented:
Sorry for the huge amount of code,... There is no easier way to do this... (sorry)


I try to explain it as simple as possible:


>>>>> This is an app with a main form (Main_Form) and one Form (Form_DoNotClose) with you ListBox (ListBox1)

> Create your Main Form, lets call it Form_main
> Create a template Form (in design mode, lets call it Form_DoNotClose) AND (!) put this code in the Form_DoNotClose_LOAD(!) event:

----------------------------------------------------------------------------------------

    Public myCaller As Form_Main                                                          << Takes care of one way of the communication (look below for the other half)

----------------------------------------------------------------------------------------



> Then put this code somewhere into your main form :

----------------------------------------------------------------------------------------

        Public FormClone As Form_DoNotClose= New Form_DoNotClose        << makes copy of your made form, this way you can acces it from you Form_main
 
        FormClone.myCaller = Me                                                               << Takes care of one way of the communication
 
        FormClone.Show()                                                                          << Shows the form
        FormClone.Hide()                                                                            << Hides the form

----------------------------------------------------------------------------------------


Now you will be able to access from both Form_Main > FormClone (Form_DoNotClose with Listbox)
as you will be able to access FormClone > Form_main

Try it!

Daniël
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
wguerramCommented:
Add a module a declare frmSelect1 Public

Module MyVars
   public frm1 as new frmSelect1
End Module

'and use frm1 to refer the form during the program is running.

'And just hide it as Chapps says.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.