Solved

Passing data between forms /  Accessing data from another form

Posted on 2004-08-10
4
228 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

728 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now