Link to home
Start Free TrialLog in
Avatar of Chapps
Chapps

asked on

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!!
Avatar of Daniellus83
Daniellus83
Flag of Netherlands image

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
------------------------------------------------------------------------------------------------------------------
Avatar of Chapps
Chapps

ASKER

What if you only hide the previous form and not close it.  Is there an easier way to do it then?
ASKER CERTIFIED SOLUTION
Avatar of Daniellus83
Daniellus83
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.