Solved

Passing data between forms /  Accessing data from another form

Posted on 2004-08-10
4
229 Views
Last Modified: 2010-04-23
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!!
0
Comment
Question by:Chapps
  • 2
4 Comments
 
LVL 4

Expert Comment

by:Daniellus83
ID: 11760370
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
 

Author Comment

by:Chapps
ID: 11760790
What if you only hide the previous form and not close it.  Is there an easier way to do it then?
0
 
LVL 4

Accepted Solution

by:
Daniellus83 earned 125 total points
ID: 11761164
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
 
LVL 8

Expert Comment

by:wguerram
ID: 11764042
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

Featured Post

ScreenConnect 6.0 Free Trial

At ScreenConnect, partner feedback doesn't fall on deaf ears. We collected partner suggestions off of their virtual wish list and transformed them into one game-changing release: ScreenConnect 6.0. Explore all of the extras and enhancements for yourself!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question