?
Solved

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

Posted on 2007-03-18
3
Medium Priority
?
228 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 47

Accepted Solution

by:
Wayne Taylor (webtubbs) earned 2000 total points
ID: 18745288
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
ID: 18745706
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
ID: 18745852
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

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…
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses
Course of the Month10 days, 8 hours left to enroll

765 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