Link to home
Start Free TrialLog in
Avatar of Crazy_Penguins
Crazy_Penguins

asked on

VB.NET passing variable from one form to another (Basic Stuff Here)

On form abc I am calling form xyz like so:

    Dim test As Integer = 5
    my.forms.xyz.show()

and want to get the 'test' value over to the form xyz.
ASKER CERTIFIED SOLUTION
Avatar of Wayne Taylor (webtubbs)
Wayne Taylor (webtubbs)
Flag of Australia 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
Here is a sample for you....

'FORM 1
Option Strict On
Imports System
Imports System.Globalization
Public Class Form1
    Inherits ControlledForm

#Region " Windows Form Designer generated code "

    Public Sub New(ByVal Controller As FormController)
        MyBase.New(Controller)

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents Label1 As System.Windows.Forms.Label
    Friend WithEvents Button1 As System.Windows.Forms.Button
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.Label1 = New System.Windows.Forms.Label
        Me.Button1 = New System.Windows.Forms.Button
        Me.SuspendLayout()
        '
        'Label1
        '
        Me.Label1.Location = New System.Drawing.Point(20, 28)
        Me.Label1.Name = "Label1"
        Me.Label1.Size = New System.Drawing.Size(108, 16)
        Me.Label1.TabIndex = 0
        Me.Label1.Text = "Hello"
        '
        'Button1
        '
        Me.Button1.Location = New System.Drawing.Point(176, 16)
        Me.Button1.Name = "Button1"
        Me.Button1.Size = New System.Drawing.Size(68, 32)
        Me.Button1.TabIndex = 1
        Me.Button1.Text = "Button1"
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(292, 266)
        Me.Controls.Add(Me.Button1)
        Me.Controls.Add(Me.Label1)
        Me.Name = "Form1"
        Me.Text = "Form1"
        Me.ResumeLayout(False)

    End Sub

#End Region

    Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim frm2 As New Form2(Controller)
        frm2.Location = New Point(Me.Left + 200, Me.Top + 100)
        frm2.Show()
    End Sub
End Class

'FORM 2
Option Strict On
Public Class Form2
   Inherits ControlledForm
#Region " Windows Form Designer generated code "

   Public Sub New(ByVal Controller As FormController)
      MyBase.New(Controller)

      'This call is required by the Windows Form Designer.
      InitializeComponent()

      'Add any initialization after the InitializeComponent() call

   End Sub

   'Form overrides dispose to clean up the component list.
   Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
      If disposing Then
            Dim frm As Form = Me.Controller.Form("Form1")
            Dim lbl As Label = CType(Me.FindControl(frm, "Label1"), Label)
            lbl.Text = ""
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
      MyBase.Dispose(disposing)
   End Sub

   'Required by the Windows Form Designer
   Private components As System.ComponentModel.IContainer

   'NOTE: The following procedure is required by the Windows Form Designer
   'It can be modified using the Windows Form Designer.  
   'Do not modify it using the code editor.
   Friend WithEvents Button1 As System.Windows.Forms.Button
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.Button1 = New System.Windows.Forms.Button
        Me.SuspendLayout()
        '
        'Button1
        '
        Me.Button1.Location = New System.Drawing.Point(300, 12)
        Me.Button1.Name = "Button1"
        Me.Button1.Size = New System.Drawing.Size(136, 24)
        Me.Button1.TabIndex = 0
        Me.Button1.Text = "Button1"
        '
        'Form2
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(448, 362)
        Me.Controls.Add(Me.Button1)
        Me.Name = "Form2"
        Me.StartPosition = System.Windows.Forms.FormStartPosition.Manual
        Me.Text = "Form2"
        Me.ResumeLayout(False)

    End Sub

#End Region

   Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
      Dim frm As Form = Me.Controller.Form("Form1")
      If Not frm Is Nothing Then
         Dim lbl As Label = CType(Me.FindControl(frm, "Label1"), Label)
         If Not lbl Is Nothing Then
            lbl.Text = "Got here"
         End If
      End If
   End Sub
    Private Function FindControl(ByVal frm As Form, ByVal ctrlName As String) As Control
        Dim ctrl As Control
        For Each ctrl In frm.Controls
            If ctrl.Name = ctrlName Then
                Return ctrl
            End If
        Next
        Return Nothing
    End Function

End Class

'FORM 3
Public Class ControlledForm
   Inherits System.Windows.Forms.Form
   Private m_Controller As FormController
   Public Sub New()
   End Sub
   Public Sub New(ByVal controller As FormController)
      m_Controller = controller
      controller.Add(Me)
   End Sub
   Public ReadOnly Property Controller() As FormController
      Get
         Return m_Controller
      End Get
   End Property

   Private Sub InitializeComponent()
      '
      'ControlledForm
      '
      Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
      Me.ClientSize = New System.Drawing.Size(288, 229)
      Me.Name = "ControlledForm"

   End Sub

   Private Sub ControlledForm_Closed(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Closed
      Me.Controller.Remove(Me)
   End Sub

End Class

'CLASS

Imports System.Collections
Public Class FormController
   Dim forms As New ArrayList()

   Public Sub Add(ByVal frm As ControlledForm)
      If Not forms.Contains(frm) Then
         forms.Add(frm)
      End If
   End Sub
   Public Sub Remove(ByVal frm As Form)
      If forms.Contains(frm) Then
         forms.Remove(frm)
      End If
   End Sub
   Public Function Form(ByVal frmName As String) As ControlledForm
      Dim o As Object
      For Each o In forms
         If CType(o, Form).Name = frmName Then
            Return CType(o, Form)
         End If
      Next
      Return Nothing
   End Function
   Public ReadOnly Property AllForms() As ArrayList
      Get
         Return forms.Clone()
      End Get
   End Property
End Class

'MODULE
Module Module1
   Public Sub main()
      Dim Controller As New FormController()
      Dim frm1 As New Form1(Controller)
      frm1.ShowDialog()
   End Sub
End Module


Avatar of Crazy_Penguins
Crazy_Penguins

ASKER

Just what I was looking for, I should have guessed the Public would have been the way to go, and BEFORE the private sub.  That was my main issue.

Thanks Wayne!