Solved

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

Posted on 2007-03-18
3
206 Views
Last Modified: 2008-03-06
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
Comment
Question by:Crazy_Penguins
3 Comments
 
LVL 47

Accepted Solution

by:
Wayne Taylor (webtubbs) earned 500 total points
Comment Utility
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
 
LVL 27

Expert Comment

by:planocz
Comment Utility
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
 

Author Comment

by:Crazy_Penguins
Comment Utility
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

743 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

16 Experts available now in Live!

Get 1:1 Help Now