• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 234
  • Last Modified:

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.
0
Crazy_Penguins
Asked:
Crazy_Penguins
1 Solution
 
Wayne Taylor (webtubbs)Commented:
Hi Crazy_Penguins,

try something like this....

## Form1 Code....

    Public Class Form1
        Public test As Integer = 5
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            My.Forms.Form2.Show()
        End Sub
    End Class

## Form2 Code

    Public Class Form2
        Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            MsgBox(My.Forms.Form1.test)
        End Sub
    End Class

The thing that enables Form2 to see the variable from Form1 is that the variable "test" has been declared as Public.

Regards,

Wayne



0
 
planoczCommented:
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


0
 
Crazy_PenguinsAuthor Commented:
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!
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now